# 重載 << 運算子

Point p1(10, 5);
cout << p1.x << ", " << p2.y << endl;

Point p1(10, 5);
cout << p1 << endl;

ostream &operator<<(ostream &s, class-name ob) {
// 實作
return s;
}

``#include <iostream> using namespace std; class Point {     int x, y; public:     Point() {         x = y = 0;     }     Point(int x, int y) {         this->x = x;         this->y = y;     }     friend ostream &operator<<(ostream &s, Point p); }; ostream &operator<<(ostream &s, Point p) {     s << "("<< p.x << ", " << p.y << ")";     return s; } int main() {     Point p1(1, 3), p2(10, 5);     cout << "p1: " << p1 << endl         << "p2: " << p2 << endl;         return 0; }``

p1: (1, 3)
p2: (10, 5)

``#include <iostream> using namespace std; class Point { public:     int x, y;     Point() {         x = y = 0;     }     Point(int x, int y) {         this->x = x;         this->y = y;     } }; ostream &operator<<(ostream &s, Point p) {     s << "("<< p.x << ", " << p.y << ")";     return s; } int main() {     Point p1(1, 3), p2(10, 5);     cout << "p1: " << p1 << endl          << "p2: " << p2 << endl;     return 0; }``

p1: (1, 3)
p2: (10, 5)