The prototype of the known class string is:
Class string <br/>{< br/> Public: <br/> string (const char * STR = NULL ); // normal constructor <br/> string (const string & other); // copy constructor <br/> ~ String (void); // destructor <br/> string & operate = (const string & other); // value assignment function <br/> PRIVATE: <br/> char * m_data; // used to save the string <br/> };
Write the above four functions of string
// String destructor <br/> string ::~ String (void) // 3 points <br/>{< br/> Delete [] m_data; <br/> // because m_data is an internal data type, you can also write it as delete m_data; <br/>}
// String constructor <br/> string: string (const char * Str) // 6 points <br/>{< br/> If (STR = NULL) <br/>{< br/> m_data = new char [1]; // It is better if null can be added <br/> * m_data = '/0 '; <br/>}< br/> else <br/>{< br/> int length = strlen (STR ); <br/> m_data = new char [Length + 1]; // It is better if null can be added <br/> strcpy (m_data, STR ); <br/>}< br/>
// Copy the constructor <br/> string: string (const string & Other) // 3 points <br/>{< br/> int length = strlen (Other. m_data); <br/> m_data = new char [Length + 1]; // It is better if null can be added <br/> strcpy (m_data, other. m_data); <br/>}
// value assignment function <br/> string & string: operate = (const string & other) // 13 points <br/>{< br/> // (1) Check the auto-assigned value // 4 points <br/> If (this = & other) <br/> return * This; <br/> // (2) release the original memory resource // 3 points <br/> Delete [] m_data; <br/> // (3) allocate new memory resources and copy the content. // 3 points <br/> int length = strlen (Other. m_data); <br/> m_data = new char [Length + 1]; // It is better if null can be added <br/> strcpy (m_data, other. m_data); <br/> // (4) returns the reference of this object // 3 points <br/> return * this; <br/>}