Overloading of C + + stream insertion and stream extraction operators

Posted by christo16 on Mon, 02 Dec 2019 20:31:41 +0100

01 overload of stream insert < operator

The most common way for C + + to output content:

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

Question:

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

Reason:

  • In fact, cout is an object of the ostream class defined in the iostream header file.
  • The reason why "<" can be used in cout is that "< is overloaded in ostream class.

For STD:: cout < 1 < Hello, it is possible to overload the member functions of 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 example of stream insert < operator overload

If we want to print out the contents of an object, we can overload the stream of ostream class to insert the < operator.

Take the CStudent class as an example:

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 insert < operator function of overloaded ostream object
// The purpose is to make it possible to print out the information of 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, "Kobayashi coding");
    std::cout << stu ; // Output all information of std object
    
    return 0;
}

Output results:

1,20, Xiaolin coding

It should be noted that the ostream & operator < (ostream & O, const CStudent & S) function is global, so the first parameter of the function must be passed into the ostream object, and the CStudent class needs to declare this function as a friend function, so that the function can access the private member variables of the CStudent class.

03 example of overload of flow extraction > > operator

Let's take the CStudent class as an example. Suppose we want to initialize the object through the input of the keyboard, then 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 enable functions 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 insert < operator function of overloaded ostream object
// The purpose is to make it possible to print out the information of the CStudent object
ostream & operator<<(ostream & o, const CStudent & s)
{
    o << s.m_id << "," << s.m_age << "," << s.m_name;
    return o;
}

// Stream extraction of overloaded istream Object > > operator function
// 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 where the comma first appears
    string tmpStr = inputStr.substr(0, pos); // Intercept string from 0 to pos
    stu.id = atoi(tmpStr.c_str());           // atoi can convert content of char * type to int type
    
    int pos2 = inputStr.find(",", pos + 1);            // Find the location of the second comma
    tmpStr = inputStr.substr(pos + 1, pos2 - pos -1);  // Take the value of age
    stu.age = atoi(tmpStr.c_str());                    // atoi can convert content of char * type to int type
    
    tmpStr = inputStr.substr(pos2 + 1, inputStr.length() - pos2 - 1); // Take the value of name
    stu.name = tmpStr;
    
    return is;
}

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

Input content and output content:

//Input:
1,20, Xiaolin coding

//Output content:
1,20, Xiaolin coding

04 summary

In order to insert the < operator and extract the > > operator into a stream for a user-defined object, we need to overload the < operator of ostream class and the > > operator of istream class for the object, and only overload them into global functions. Then, in the CStudent class, we need to declare the above two overloaded functions as friend functions, so that the two overloaded functions can access Ask and assign private member functions in the CStudent class.

Topics: C++