I was asked about the implementation of the string class during the interview. The results were not written...
Review it.
The following program does not implement all member methods of the string class. It only writes most important member functions according to the tutorial.
# Include <iostream> <br/> # include <iomanip> <br/> using namespace STD; </P> <p> class string {<br/> friend ostream & operator <(ostream &, string &); // overload <operator <br/> friend istream & operator> (istream &, string &); // overload> operator <br/> public: <br/> string (const char * STR = NULL); // value assignment constructor and default constructor (char) <br/> string (const string & other ); // value assignment Constructor (string) <br/> string & operator = (const string & other); // operator = <br/> strin G operator + (const string & Other) const; // operator + <br/> bool operator = (const string &); // operator ==< br/> char & operator [] (unsigned INT); // operator [] <br/> size_t size () {return strlen (m_data) ;}; <br/> ~ String (void) {Delete [] m_data ;}< br/> PRIVATE: <br/> char * m_data; // used to save the string <br/> }; </P> <p> inline string: string (const char * Str) <br/>{< br/> If (! Str) m_data = 0; // when declared as an inline function, the function is directly replaced by a statement when executed in the program, instead of being called <br/> else {<br/> m_data = new char [strlen (STR) + 1]; <br/> strcpy (m_data, STR ); <br/>}</P> <p> inline string: string (const string & Other) <br/>{< br/> If (! Other. m_data) m_data = 0; // you can access the Private Members of the same object in the member functions of the class (the same type is a friend relationship) <br/> else <br/>{< br/> m_data = new char [strlen (Other. m_data) + 1]; <br/> strcpy (m_data, other. m_data); <br/>}</P> <p> inline string & string: Operator = (const string & other) <br/>{< br/> If (this! = & Other) <br/>{< br/> Delete [] m_data; <br/> If (! Other. m_data) m_data = 0; <br/> else <br/>{< br/> m_data = new char [strlen (Other. m_data) + 1]; <br/> strcpy (m_data, other. m_data); <br/>}< br/> return * This; <br/>}< br/> inline string :: operator + (const string & Other) const <br/>{< br/> string newstring; <br/> If (! Other. m_data) <br/> newstring = * This; <br/> else if (! M_data) <br/> newstring = Other; <br/> else <br/>{< br/> newstring. m_data = new char [strlen (m_data) + strlen (Other. m_data) + 1]; <br/> strcpy (newstring. m_data, m_data); <br/> strcat (newstring. m_data, other. m_data); <br/>}< br/> return newstring; <br/>}</P> <p> inline bool string :: operator = (const string & S) <br/>{< br/> If (strlen (S. m_data )! = Strlen (m_data) <br/> return false; <br/> return strcmp (m_data, S. m_data )? False: true; <br/>}</P> <p> inline char & string: operator [] (unsigned int e) <br/>{< br/> If (E> = 0 & E <= strlen (m_data) <br/> return m_data [E]; <br/>}</P> <p> ostream & operator <(ostream & OS, string & Str) <br/>{< br/> OS <Str. m_data; <br/> return OS; <br/>}</P> <p> istream & operator> (istream & input, string & S) <br/>{< br/> char temp [255]; // used to store the input stream <br/> input> SETW (255)> temp; <br/> S = temp; // run the value assignment Operator <br/> return input; // continuous use of return is supported> operator <br/>}</P> <p> int main () <br/>{< br/> string str1 = "Aha! "; <Br/> string str2 =" my friend "; <br/> string str3 = str1 + str2; <br/> cout <str3 <"/N" <str3.size () <Endl; <br/> return 0; <br/>}</P> <p>
My source address: http://moxiaomomo.iteye.com