Overload of C++ stream insert''operator

Posted by nando on Thu, 05 Dec 2019 06:59:04 +0100

01 Stream Insert < Operator Overload

C++ is the most commonly used way to output content:

std::cout << 1 <<"hello";

Question:

  • Why does this statement hold?
  • What is cout?Can the'<'operator be used on couts?

Reason:

  • In fact, cout is an object of the ostream class defined in the iostream header file.
  • "<<" can be used on cout s because "<<" is overloaded in the ostream class.

For std::cout << 1 << "hello"; this statement may be overloaded as a member function of the ostream class as follows:

ostream & ostream::operator<<(int n)
{
    .... // Output n integer code
    return *this;
}

ostream & ostream::operator<<(const char * s)
{
    .... // Code to output s String
    return *this;
}
  • Std:: cout << 1; statement, equivalent to cout.operator << (1);
  • Std:: cout << "hello"; statement, equivalent to cout.operator < ("hello");
  • Std:: cout < < 1 < < "hello"; statement, equivalent to (cout.operator < < (1)).operator < ("hello");

02 stream insertion < operator overload example

Assuming that we want to print out the contents of an object, we can overload the stream insertion < operator of the ostream class.

The following is an example of a CStudent class:

class CStudent // Student Class
{
public:
    // Constructor
    CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
    
    // Declare the function as a friend function
    // The purpose is to allow functions to access private member variables of the CStudent class
    friend ostream & operator<<(ostream & o, const CStudent & s);
    
private:
    int m_age;      // Age
    int m_id;       // ID Number
    string m_name;  // Name
};

// Stream insertion of overloaded ostream objects < operator function
// The purpose is to make it possible to print out information about the CStudent object
ostream & operator<<(ostream & o, const CStudent & s)
{
    o << s.m_id << "," << s.m_age << "," << s.m_name;
    return o;
}

int main()
{
    CStudent stu(1, 20, "Xiaolin coding");
    std::cout << stu ; // Output all information of std object
    
    return 0;
}

Output results:

1,20, Xiaolin coding

It is important to note that the ostream & operator<< (ostream & o, const CStudent & s) function is global, s o the first parameter of the function must pass into the object of ostream, and the CStudent class needs to declare this function as a friend function s o that the function can access the private member variables of the CStudent class.

03 Stream Extraction >> Operator overload example

Or take the CStudent class as an example, and suppose you want to initialize the object with the input from the keyboard, we can overload the Stream Extraction > operator of the istream class.

class CStudent // Student Class
{
public:

    // Constructor
    CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
    
    // Declare the function as a friend function
    // The purpose is to allow functions to access private member variables of the CStudent class
    friend ostream & operator<<(ostream & o, const CStudent & s);
    
    // Declare the function as a friend function
    // The purpose is to allow the function to assign values to private member variables of the CStudent class
    friend istream & operator>>(istream & is,  CStudent & s);
    
private:
    int m_age;      // Age
    int m_id;       // ID Number
    string m_name;  // Name
};

// Stream insertion of overloaded ostream objects < operator function
// The purpose is to make it possible to print out information about the CStudent object
ostream & operator<<(ostream & o, const CStudent & s)
{
    o << s.m_id << "," << s.m_age << "," << s.m_name;
    return o;
}

// Stream Extraction > Operator Function for Overloaded istream Objects
// The purpose is to initialize the contents of the CStudent object
istream & operator>>(istream & is,  CStudent & stu)
{
    string inputStr;
    is >> inputStr;
    
    int pos = inputStr.find(",", 0);         // Find the location of the first comma
    string tmpStr = inputStr.substr(0, pos); // Intercept a string from 0 to pos
    stu.id = atoi(tmpStr.c_str());           // atoi can convert content of type char* to type int
    
    int pos2 = inputStr.find(",", pos + 1);            // Find the location of the second comma
    tmpStr = inputStr.substr(pos + 1, pos2 - pos -1);  // Remove the age value
    stu.age = atoi(tmpStr.c_str());                    // atoi can convert content of type char* to type int
    
    tmpStr = inputStr.substr(pos2 + 1, inputStr.length() - pos2 - 1); // Take out the value of name
    stu.name = tmpStr;
    
    return is;
}

int main()
{
    CStudent stu;
    
    // Initialize stu object with input information
    cin << stu;
    
    // Output std object information
    cout >> stu;
    
    return 0;
}

Input and output:

//Enter:
1,20, Xiaolin coding

//Output:
1,20, Xiaolin coding

Summary of 04

In order for the stream insertion < operator and stream extraction > operator to target a custom object, we need to overload the < operator and > operator of the ostream class for that object, and can only overload global functions, then in the CStudent class we need to declare the two overloaded functions as friend functions so that the two overloaded functions can be accessedAsk and assign a private member function in the CStudent class.

Topics: C++