Implementation tutorial of the string class in C ++, and string tutorial
Implementation of the string class
# Include
# Include
# Include
Using namespace std;
Class String {
Friend ostream & operator <(ostream &, String &); // overload <operator
Friend istream & operator> (istream &, String &); // reload> operator
Public:
String (const char * str = NULL); // value assignment constructor and default constructor (char)
String (const String & other); // value assignment Constructor (String)
String & operator = (const String & other); // operator =
// String operator + (const String & other) const; // operator + member function version
Bool operator = (const String &); // operator =
Char & operator [] (unsigned int); // operator []
Size_t size () {return strlen (m_data );};
Friend const String operator + (const String & other1, const String & other2 );
// Note: if it is a friend const String operator + (const String & other1, const String & other2) const, it is represented as a const function, but it cannot be compiled. The prompt is: 'Cannot have cv-qualifier ',
// No CV limitation is allowed. In C ++, CV indicates const and volatile-1. Non-member functions cannot have CV limitation. 2. static member functions cannot have CV limitation.
// Because of the const function, it indicates that the function has the const this pointer, And the youyuan function is not a member function, so the conflict occurs. Note: friend const String operator + () indicates that the return value is const rather than the const function.
~ String (void) {delete [] m_data ;}
Private:
Char * m_data; // used to save strings
};
Inline String: String (const char * str)
{
If (! Str) m_data = 0; // if it is declared as an inline function, this function is directly replaced by statements when executed in the program, rather than called.
Else {
M_data = new char [strlen (str) + 1];
Strcpy (m_data, str );
}
}
Inline String: String (const String & other)
{
If (! Other. m_data) m_data = 0; // you can access the Private Members of the same object in the class member/friend function (the same type is a friend relationship)
Else
{
M_data = new char [strlen (other. m_data) + 1];
Strcpy (m_data, other. m_data );
}
}
Inline String & String: operator = (const String & other)
{
If (this! = & Other)
{
Delete [] m_data;
If (! Other. m_data) m_data = 0;
Else
{
M_data = new char [strlen (other. m_data) + 1];
Strcpy (m_data, other. m_data );
}
}
Return * this;
}
/***** Version of the member function
Inline const String: operator + (const String & other) const
{
String newString;
If (! Other. m_data)
NewString = * this;
Else if (! M_data)
NewString = other;
Else
{
NewString. m_data = new char [strlen (m_data) + strlen (other. m_data) + 1];
Strcpy (newString. m_data, m_data );
Strcat (newString. m_data, other. m_data );
}
Return newString;
}
****/
// The membership function version is as follows:
Const String operator + (const String & other1, const String & other2)/* Yes: operator + instead of: String: operator +, because the youyuan function is not a member function */
{
String newString;
If ((! Other1.m _ data )&&(! Other2.m _ data ))
NewString. m_data = NULL;
Else if ((! Other1.m _ data) & (other2.m _ data! = 0 ))
NewString = other2;
Else if (other1.m _ data! = 0) & (other2.m _ data = 0 ))
NewString = other1;
Else
{
NewString. m_data = new char [strlen (other1.m _ data) + strlen (other2.m _ data) + 1];
Strcpy (newString. m_data, other1.m _ data );
Strcat (newString. m_data, other2.m _ data );
}
Return newString;
}
Inline bool String: operator = (const String & s)
{
If (strlen (s. m_data )! = Strlen (m_data ))
Return false;
Return strcmp (m_data, s. m_data )? False: true;
}
Inline char & String: operator [] (unsigned int e)
{
If (e> = 0 & e <= strlen (m_data ))
Return m_data [e];
}
Ostream & operator <(ostream & OS, String & str)
{
OS <str. m_data;
Return OS;
}
Istream & operator> (istream & input, String & s)
{
Char temp [255]; // used to store the input stream
Input> setw (255)> temp;
S = temp; // use the value assignment operator
Return input; // use return to support continuous use> Operator
}
Int main ()
{
String str1 = "Aha! ";
String str2 = "My friend ";
String str3 = str1 + str2;
Str1 + str2 = str3;
Cout <