Implementation of the string class
Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & other); // copy the constructor
~ String (void); // destructor
String & operate = (const String & other); // value assignment function
String operator + (const String & other) const; // operator +
Bool operator = (const String &); // operator =
Char & operator [] (unsigned int); // operator []
Friendostream & operator <(ostream &, String &); // <
Friendostream & operator> (ostream &, String &);
Size_tsize () {return strlen (m_data );};
Private:
Char * m_data; // used to save strings
};
// Common Constructor
String: String (const char * str)
{
If (str = NULL)
{
M_data = new char [1]; // score point: automatically apply to store the ending sign ''for an empty string // additional points: Add NULL to m_data
* M_data = '';
}
Else
{
Int length = strlen (str );
M_data = new char [length + 1]; // It is better if NULL can be added.
Strcpy (m_data, str );
}
}
// String destructor
String ::~ String (void)
{
Delete [] m_data; // or delete m_data;
}
// Copy the constructor
String: String (const String & other) // score point: the input parameter is of the const type.
{// It is better to judge other. m_data = NULL because strlen (NULL) will crash.
Int length = strlen (other. m_data );
M_data = new char [length + 1]; // points for adding NULL to m_data
Strcpy (m_data, other. m_data );
}
// Value assignment function
String & String: operate = (const String & other) // score point: the input parameter is const.
Type
{
If (this = & other) // score point: Check auto-assigned values
Return * this;
Delete [] m_data; // score point: Release the original memory resource
Int length = strlen (other. m_data );
M_data = new char [length + 1]; // points for adding NULL to m_data
Strcpy (m_data, other. m_data );
Return * this; // score point: return the reference of this object
}
// A better way to write the value assignment function
String & String: operate = (const String & other) // score point: the input parameter is const.
Type
{
If (this! = & Other) // score point: Check the auto-assigned Value
{
String strTemp (other); // create a temporary object. After the if statement is executed, the object is automatically destroyed.
Char * pTemp = strTemp. m_data;
StrTemp. m_data = m_data;
M_data = pTemp
}
Return * this; // score point: return the reference of this object
}
// Operator +
InlineString String: operator + (const String & other) const
{
String newString;
If (! Other. m_data)
NewString = * this;
Else if (! M_data)
NewString = other;
Else
{
NewString. m_data = newchar [strlen (m_data) + strlen (other. m_data) + 1];
Strcpy (newString. m_data, m_data );
Strcat (newString. m_data, other. m_data );
}
Return newString;
}
// Operator =
BoolString: operator = (const String & str)
{
Return strcmp (m_data, str. m_data) = 0;
}
// Operator []
Inlinechar & String: operator [] (unsigned int e)
{
If (e> = 0 & e <= strlen (m_data ))
Return m_data [e];
}
// Operator <
Ostream & operator <(ostream & OS, String & str)
{
OS <str. m_data;
Return OS;
}
// Operator>
Istream & operator> (istream & input, String & s)
{
Chartemp [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
}