// CS2 Chung, Operator Overloading (II) // Overlaoding Output and Input Operators. // Goal: cin >> p1; cout << p2; #include class Point { private: double x; double y; public: Point() { x = 0; y = 0; } Point(double init_x, double init_y) { x = init_x; y = init_y; } friend istream& operator >> (istream& ins, Point& target) { cout << "Enter x and y for a point: "; ins >> target.x >> target.y; return ins; } friend /* ostream& */ void operator << (ostream& outs, Point& source) { outs << "(" << source.x << ", "; outs << source.y << ")" << endl; // return outs; } friend bool operator == (const Point& p1, const Point& p2); friend Point operator + (const Point& p1, const Point& p2); }; bool operator == (const Point& p1, const Point& p2) { return ((p1.x == p2.x) && (p1.y == p2.y)); } Point operator + (const Point& p1, const Point& p2) { double x_sum, y_sum; x_sum = p1.x + p2.x; y_sum = p1.y + p2.y; Point sum(x_sum, y_sum); return sum; } void main() { Point p1, p2; cin >> p1 >> p2; // cout << p1 << p2; Compile error! No cascading output op. cout << "1st point: " << p1; cout << "2nd point: " << p2; if(p1 == p2) cout << "Those points are equal." << endl; else cout << "Those points are NOT equal." << endl; Point p3(p1+p2); cout << p3; Point p4 = p1 + p2; // This works! cout << p4; }