The String class implements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.