标签:
以结构体point为例,成员变量x,y,定义加法和输出流方式。
1 #include<iostream> 2 using namespace std; 3 struct Point{ 4 int x, y; 5 // Point(int x=0, int y=0):x(x),y(y) {} // 简单的写法 6 Point(int x = 0,int y = 0) { this->x = x; this->y = y; } 7 }; 8 9 Point operator + (const Point& A,const Point& B) { 10 return Point(A.x + B.x,A.y + B.y); 11 } 12 13 ostream& operator << (ostream &out, const Point& p) { 14 out << "(" << p.x << "," << p.y << ")"; 15 return out; 16 } 17 18 int main() { 19 Point a,b(1,2); 20 a.x = 3; 21 cout << a + b << "\n"; 22 return 0; 23 }
标签:
原文地址:http://www.cnblogs.com/cyb123456/p/5798432.html