Implement three replication control functions for the string class of C ++ pen questions

Source: Internet
Author: User

 

# Include <iostream>
Using namespace STD;

Class string
{
Friend ostream & operator <(ostream & out, const string & Str) // overload the output Operator
{
Return Str. Print (out );
}
Public:
String (const char * STR = 0); // common Constructor
String (const string & other); // copy the constructor
~ String (void) {Delete [] data _;} // destructor
String & operator = (const string & other); // value assignment function
Char * Data (void) const {return data _;}
PRIVATE:
Ostream & print (ostream & out) const;
Char * Data _; // used to save a string
};

// The Value assignment operator should first pay attention to whether it is assigned to itself. If so, do nothing and return it.

// Second, assign values to others. In this case, you must first throw the original values and open up a space based on the size of others.

// Prepare to store others' content. Do not forget to return your reference.

String & string: Operator = (const string & other)
{
If (this! = & Other)
{
Delete [] data _;
Size_t length = strlen (other. Data ());
Data _ = new char [Length + 1];
Strcpy_s (Data _, Length + 1, other. Data ());
}
Return * this;
}

// The replication constructor always occurs in the constructor stage. Therefore, the member data _ has no space to use.

// The size of the person space opens up the space, and then copies the content of others.

String: string (const string & other)
{
Size_t length = strlen (other. Data ());
Data _ = new char [Length + 1];
Strcpy_s (Data _, Length + 1, other. Data ());
}

// Because the output operator is usually written as a class's friend function, you can write a cout-like <s; otherwise

// Very strange. For example, it may be S. Print () and so on. It cannot be combined perfectly with the standard library like cout <S <S1 <Endl;, even if

// You wrote an ostream & operator <(ostream & out, const string & Str) and forgot to add a friend declaration. The Compiler

// You are considered to have overloaded the unary shift operator <, and the parameter is added more.

// The classical Writing of the output operator is like this article. Add a print member function to complete the work function.

// To return ostream & is also consistent with the built-in operator Mechanism in C ++, so that cout can be written <S <S1 <Endl; instead

// Cout <s; cout <S1; cout <Endl;

Ostream & string: Print (ostream & out) const
{
Out <data _;
Return out;
}

// This constructor supports implicit type conversion. For example, you can create a String object string S ("Hello world! "); This statement

// The constructor is called, and the string S = "Hello world! "; Will be interpreted as string S = sting (" Hello world! "); First

// Construct a temporary String object based on the character array (this object is destructed after this statement is executed), and then call the string value assignment.

// Operator overload Function

String: string (const char * Str) // 6 points
{
If (STR = NULL)
{
Data _ = new char [1]; // It is better if null can be added
* Data _ = '\ 0 ';
}
Else
{
Size_t length = strlen (STR );
Data _ = new char [Length + 1]; // It is better if null can be added
Strcpy_s (Data _, Length + 1, STR );
}
}

Void main ()
{
Char * P = "Hello world! ";
String s (P );
Cout <S <Endl;
String S1 ("how are you? ");
Cout <S1 <Endl;
S1 = s;
Cout <S <Endl <S1 <Endl;
S = s = S1;
Cout <S <Endl <S1 <Endl;
}

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.