C ++ implements heavy-load ostream and heavy-load ostream
#include <iostream>using namespace std;class Point{public:Point(int _x = 0, int _y = 0, int _z = 0):x(_x), y(_y), z(_z){}Point(){}~Point(){}friend ostream& operator<<(ostream &os, const Point &pd);private:int x;int y;int z;};ostream& operator<<(ostream &os, const Point &pd){os << pd.x<<" "<<pd.y<<" "<<pd.z;return os;} int main() {Point pd(1,2,3);cout<<pd;return 0;}
The main note is to define the function as a friend function of the class Point outside the class. In addition, pay attention to the return OS to achieve the chain function.
Heavy Load of C ++ into ostream class
Ostream has a char * overload,
Const char * and const void *. Other pointers are output by const void *
In c ++, the operator (\ "<\") reloads ostream
Operator Overloading is an extension of the existing operators used to customize user types, but the specific nature cannot be changed. For example, <indicates that the object on the left of the stream operator cannot be changed as the essence of the stream, + The essence of the two-element operator cannot be changed. For example, for the-> overload operator, the pointer type must be returned and cannot be changed!
That is, even if operators are overloaded, they are limited!
Friend ostream & operator <(ostream & OS, const CPolynomial &)
Operator is a keyword for operator overloading. It is followed by an overloaded operator, with a stream on the left and a stream processing object on the right. In this way, you can process how to input or output the object.