The string class is an indispensable class in the application framework
Overloaded operators to implement string manipulation
#idndef Ioteck_string_h_
#define Ioteck_string_h_
Namespace Iotek
{
Class String
{
Public
String (const char*=null);
~string ();
String (const string&); Copy constructor
String A; A=b;
string& operator= (const String &); Assignment operators
String A; A= "Hello";
string& operator= (const char*);
string& operator+= (const string&);
String operator+ (const string&) const;
string& operator+= (const char*);
String operator+ (const char*) const;
Inline const char* data () const
{
return m_data;
}
Private
Char *m_data;
}
}
#endif
. CPP file
#include "Iotekstring.h"
#include <iostream>
#include <string.h>
using namespace Std;
using namespace Iotek;
string::string (const char *STR)
{
if (NULL==STR)
{
M_data=new Char[1];
*m_data= ' + ';
}
else{
int Length=strlen (str);
M_data=new char[length+1];
strcpy (M_DATA,STR);
}
}
String::~string ()
{
delete [] m_data;
}
string::string (const String &other)
{
int Length=strlen (other.m_data);
M_data=new char[length+1];
strcpy (M_data,other.m_data);
}
string& string::operator= (const String &other)
{
if (This==&other)
return *this;
delete [] m_data;
int Length=strlen (other.m_data);
M_data=new char[length+1];
strcpy (M_data,other.m_data);
return *this;
}
string& string::operator= (const char *other)
{
Delete[] m_data;
if (other==null)
{
M_data=new Char[1];
*m_data= ' + ';
}
Else
{
int Length=strlen (other);
M_data=new char[length+1];
strcpy (M_data,other);
}
return *this;
}
string& string::operator+= (const string& Other)
{
char* Tmp=m_data;
int Length=strlen (m_data) +strlen (other.m_data);
M_data=new char[length+1];
strcpy (M_DATA,TMP);
strcat (M_data,other.m_data);
delete [] tmp;
return *this;
}
String string::operator+ (const string& Other) const
{
String result;
Result+=*this;
Result+=other;
return result;
}
string& string::operator+= (const char* Other)
{
String tmp (other);
*this+=tmp;
return *this;
}
String string::operator+ (const char* Other) const
{
String Result=*this;
Result+=other;
result result;
}
Main.cpp
#include "Iotekstring.h"
#include <iostream>
#include <string.h>
using namespace Std;
using namespace Iotek;
int main (int argc,const char *argv[])
{
String S1 ("Hello");
String s2=s1;
String s3= "World";
S1+=S3;
s3+= "!";
String s4=s1+s2;
s4=s1+ "Hello";
System ("pause");
return 0;
}
The String class implements