[C ++] Operator Overloading in the String class, string Operator
Modular Design:
Header file:
<span style="font-size:18px;">
# Ifndef operator_operator_h # define operator_operator_h # include <iostream> # include <string> using namespace std; class MyString {public: // three overloaded constructors MyString (); myString (const char * str); MyString (const MyString & str); // destructor ~ MyString (); // overload operator MyString & operator = (const MyString & str); char & operator [] (int index ); // access subscript friend ostream & operator <(ostream & out, const MyString & str); // reload the output operator friend istream & operator> (istream & in, myString & str); // overload input operator friend MyString operator + (const MyString & str1, const MyString & str2); // overload the plus sign operator, note that the returned reference cannot be friend MyString operator + = (MyString & str1, const MyString & str2); // weight Carrier + = operator friend bool operator = (const MyString & str1, const MyString & str2); // overload the equal operator friend bool operator! = (Const MyString & str1, const MyString & str2); // The private: char * p; int len ;};# endif;
</span>
Function:
<span style="font-size:18px;">
# Include "operator. h "// default constructor, initialized as an empty string MyString: MyString () {len = 0; p = new char [len + 1]; p [0] = '\ 0';} // constructor. Use a string to initialize MyString: MyString (const char * str) {len = strlen (str ); p = new char [strlen (str) + 1]; strcpy_s (p, strlen (str) + 1, str) ;}// copy the constructor, use another string to initialize MyString: MyString (const MyString & str) {len = str. len; p = new char [strlen (str. p) + 4]; strcpy_s (p, strlen (str. p) + 1, str. p);} // destructor MyString ::~ MyString () {delete [] p;} // overload value assignment operator (=) MyString & MyString: operator = (const MyString & str) {if (this-> p = str. p) {return * this;} delete [] p; len = str. len; p = new char [len + 1]; strcpy_s (p, len + 1, str. p); return * this;} // overload output operator (<) ostream & operator <(ostream & out, const MyString & str) {out <str. p; return out;} // reload the input operator (>>) istream & operator >>( istream & in, MyString & str) {in> str. p; return in ;} // Overload the plus sign operator (+) MyString operator + (const MyString & str1, const MyString & str2) {MyString str; delete [] str. p; str. len = str1.len + str2.len; str. p = new char [str. len + 1]; strcpy_s (str. p, str. len + 1, str1.p); strcat_s (str. p, str. len + 1, str2.p); return str;} // overload addition and value assignment operator (+ =) MyString operator + = (MyString & str1, const MyString & str2) {str1 = str1 + str2; return str1 ;}// reload the equal operator bool operator == (const MyString & str1, const MyString & str2) {if (strcmp (str1.p, str2.p) = 0) {return true ;}return false ;}// overload the non-equal operator bool operator! = (Const MyString & str1, const MyString & str2) {if (strcmp (str1.p, str2.p) = 0) {return false;} return true ;} // overload subscript ([]) char & MyString: operator [] (int index) {return p [index] ;}</span>
Test procedure:
<Span style = "font-size: 18px;"> # include "operator. h "int main () {MyString mystr (" asfasgdhf "); // test the constructor and use a string to initialize cout <mystr [2] <endl; mystr [4] = 'D'; cout <mystr <endl; MyString mystr2 (mystr); // use another string to initialize cout <mystr2 <endl; myString mystr3; mystr3 = mystr + mystr2; // test the plus operator and test the value assignment operator cout <mystr + mystr2 <endl; mystr3 + = mystr; // test + = Operator cout <mystr3 <endl; cout <(mystr = mystr2) <Endl; // test = cout <(mystr! = Mystr3) <endl; // test! = MyString mystr4; cout <"Input a series characters, end of ctrl + z. "<endl; cin> mystr4; // test the overloaded input symbol (>) cout <mystr4 <endl; return 0 ;}</span>
Running result: