Simulate the string class by reloading the member operator of the class

Source: Internet
Author: User

/* THIS Program simulates the string class by reloading the member operators of the class */
# include
# include
# include
using namespace STD;
class string;
ostream & operator <(ostream &, string &);
istream & operator> (istream &, string &);
class string
{< br> friend ostream & operator <(ostream &, string &);
friend istream & operator> (istream &, string &);
Public:
string (): _ string (0), _ SIZE (0)
{cout <"constructor 1 called" string (char * P ): _ SIZE (strlen (p) // constructor 2
{< br> _ string = new char (_ SIZE + 1);
strcpy (_ string, p);
}

String (const string & P); // copy the constructor
String & operator = (const string & P); // reload 1 of the value assignment operator =
String & operator = (char * P); // reload 2 of the value assignment operator =
String & operator + = (string & P); // reload 1 of the + = Operator
String & operator + = (char * P); // reload 2 of the + = Operator
Char & operator [] (int x) const; // reload the subscript operator. A left value must be returned because of her
// It can appear on the left of the value assignment expression or on the right of the value assignment expression.
Bool operator = (string & P); // reload 1 of equal Operators
Bool operator = (char * P); // reload 2 of equal Operators
~ String ()
{
Delete [] _ string;
Cout <"destructor called" <Endl;
}

PRIVATE:
Int _ size;
Char * _ string;
};

Inline string: string (const string & P)
{
If (P. _ size! = 0)
{
_ Size = P. _ size;
_ String = new char [_ SIZE + 1];
Strcpy (_ string, P. _ string );
}
Else
{
_ Size = 0;
_ String = 0;
}
}

Inline string & string: Operator = (const string & P)
{If (_ string)
Delete _ string;
_ Size = P. _ size;
If (P. _ SIZE)
{
_ String = new char [_ SIZE + 1];
Strcpy (_ string, P. _ string );
}
Else
_ String = 0;
Return * this;
}

Inline string & string: Operator = (char * P)
{
Delete _ string;
_ Size = strlen (P );
If (_ SIZE)
{
_ String = new char [_ SIZE + 1];
Strcmp (_ string, P );
}
Else
_ String = 0;
Return * this;
}

Inline string & string: Operator + = (string & P)
{
If (P. _ SIZE)
{
String TMP (* This );
_ SIZE + = P. _ size;
Delete _ string;
_ String = new char [_ SIZE + 1];
Strcpy (_ string, TMP. _ string );
Strcpy (_ string + TMP. _ size, P. _ string );
}
Return * this;
}

Inline string & string: Operator + = (char * P)
{
If (P)
{
String TMP (* This );
_ SIZE + = strlen (P );
Delete _ string;
_ String = new char [_ SIZE + 1];
Strcpy (_ string, TMP. _ string );
Strcpy (_ string + TMP. _ size, P );
}
Return * this;
}

Inline char & string: operator [] (int x) const
{
Assert (x> = 0 & x <_ size );
Return _ string [x];
}

Inline bool string: Operator = (string & P)
{
If (_ size! = P. _ SIZE)
Return false;
Return (strcmp (_ string, P. _ string )? False: True );
}

Inline bool string: Operator = (char * P)
{
_ Size = strlen (P );
If (_ SIZE)
Return false;
Return (strcmp (_ string, p )? False: True );
}

 

Ostream & operator <(ostream & output, string & P)
{
Output <p. _ string;
Return output;
}
Istream & operator> (istream & input, string & P)
{
Char Q [100];
Input> q;
P. _ size = strlen (Q );
P. _ string = new char [P. _ SIZE + 1];
Strcpy (P. _ string, q );
Return input;
}

 

Void main ()
{
String S1, S2, S3;
Cout <"Please input two strings consecutively" <Endl;
Cin> S1> S2;

Cout <"S1 = S2? "<Endl;
If (S1 = S2)
Cout <"equal to" <Endl;
Else cout <"not equal to" <Endl;

S1 + = S2;
Cout <"the value of S1 after addition is" <S1 <Endl;
}

/* Error 1: It indicates that the private data member of the class cannot be called in the function. It is known that the statement of youyuan has a problem,
later it was found that the class string must be prefixed with
ostream & operator <(ostream &, string &); and
istream & operator> (istream &, string &); declarations can be added to the class
friend ostream & operator <(ostream &, string &); and
friend istream & operator> (istream &, string &);
because these two friends are defined after the class definition, however, because the declaration of the class to youyuan involves the class, you have to add the class declaration before
youyuan Declaration: calss string;
Error 2: now there is no problem with compilation, but the program suddenly stops running. The root cause of the error is that the
definition of the> operator is completely incorrect, because S1 and S2 call the default constructor, therefore, their _ string pointers indicate that
memory is empty, while I directly: input> P. _ string; p. _ size = strlen (P. _ string); why not
memory error. _ string does not even allocate memory. How can I accept the input. Therefore, you need to define a temporary char q
[100] to cache the input data. Char Q [2, 100];
input> q;
P. _ size = strlen (Q);
P. _ string = new char [p. _ SIZE + 1];
strcpy (P. _ string, q);
return input;
however, this limitation is related to the size of the Q character array, if the input
string is greater than 100, an error occurs again. There may be other good methods
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.