4.10. Providing Class Instances of the iostream Operators

Often, we wish to both read and write objects of a class. For example, to display our trian class object, we want to be able to write

cout << trian << endl; 

To support this, we must provide an overloaded instance of the output operator:

ostream& operator<<( ostream &os, const Triangular &rhs ) 
{ 
    os << "( " << rhs.beg_pos() << ", " 
       << rhs.length()       << " ) "; 

    rhs.display( rhs.length(), rhs.beg_pos(), os ); 
    return os; 
} 

We return the same ostream object passed into the function. This allows multiple output operators to be concatenated. Both objects are passed by reference. The ostream operand is not declared as const because each output operation modifies the internal state of the ostream ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.