Implementation of the string class operator overload + =) (=! = ))((
// MyString. h # pragma once # include
Using namespace std; class MyString {private: char * m_ptr; // memory space public: MyString (const char * str = NULL ); // const MyString & obj; // copy the constructor ~ MyString (); // destructor public: // + ==><=! >>> <
(Const MyString & obj); // compare bool operator <(const MyString & obj); // compare bool operator = (const MyString & obj ); // compare the bool operator! = (Const MyString & obj); // compare the size of friend istream & operator> (istream & in, MyString & obj ); // input stream friend ostream & operator <(ostream & out, const MyString & obj); // output stream };
// MyString. cpp # define _ CRT_SECURE_NO_WARNINGS # include "MyString. h "MyString: MyString (const char * str) // constructor {if (str = NULL) {this-> m_ptr = new char [1]; this-> m_ptr [0] = '\ 0';} else {this-> m_ptr = new char [strlen (str) + 1]; strcpy (this-> m_ptr, str) ;}} MyString: MyString (const MyString & obj) // copy the constructor {this-> m_ptr = new char [strlen (obj. m_ptr) + 1]; strcpy (this-> m_ptr, obj. m_ptr);} MyString ::~ MyString () // destructor {delete [] m_ptr; this-> m_ptr = NULL;} MyString: operator + (const MyString & obj) // connect {MyString tmp; delete [] tmp. m_ptr; tmp. m_ptr = new char [strlen (this-> m_ptr) + strlen (obj. m_ptr) + 1]; sprintf (tmp. m_ptr, "% s", this-> m_ptr, obj. m_ptr); return tmp;} MyString & MyString: operator = (const MyString & obj) // value {if (this-> m_ptr! = NULL) {delete [] m_ptr; this-> m_ptr = NULL;} this-> m_ptr = new char [strlen (obj. m_ptr) + 1]; strcpy (this-> m_ptr, obj. m_ptr); return * this;} bool MyString: operator> (const MyString & obj) // compare the size {if (-1 = strcmp (this-> m_ptr, obj. m_ptr) {return true;} else {return false;} bool MyString: operator <(const MyString & obj) // compare the size {if (1 = strcmp (this-> m_ptr, obj. m_ptr) {return true;} else {return false;} bool MyStrin G: operator = (const MyString & obj) // compare the size {if (0 = strcmp (this-> m_ptr, obj. m_ptr) {return true;} else {return false;} bool MyString: operator! = (Const MyString & obj) // compare the size {return! (* This = obj);} ostream & operator <(ostream & out, const MyString & obj) // output stream {out <obj. m_ptr; return out;} istream & operator> (istream & in, MyString & obj) // input stream {char tmp [1024*10]; memset (tmp, 0, sizeof (tmp); in> tmp; if (obj. m_ptr! = NULL) {delete [] obj. m_ptr; obj. m_ptr = NULL;} obj. m_ptr = new char [strlen (tmp) + 1]; strcpy (obj. m_ptr, tmp); return in ;}
// MyStringTest. cpp # include "MyString. h "using namespace std; void run () {MyString s1 (" abd "); MyString s2 (" hello "); MyString s3 = s2; MyString s4; // s1 = s2; s4 = s1 + s2; cout <s4 <endl; if (s1 = s2) {cout <"equal" <endl ;} else {cout <"not equal" <endl; if (s1 <s2) {cout <"s1
<< endl;}else if (s1>
S2) {cout <"s1> s2" <endl ;}} cout <s1 <"," <s2 <endl; cin> s4; cout <s4 <endl;} void main () {run (); system ("pause ");}