C++中结构体p105
注意:
- operator 固定,不可变
- ostream& operator <<(ostream& o,const point& p)
- point operator + (const point& A,const point& B)
#include<cstdio>
#include<iostream>
using namespace std;
struct point{
int x,y;
point(int x=0,int y=0):x(x),y(y) {}
};
point operator + (const point& A,const point& B)//调用结构体成员 const point& A
{
return point(A.x +B.x ,A.y +B.y );
}
ostream& operator <<(ostream& o,const point& p)//cout只能输出int double,输出特定格式,使用ostream& operator <<
{
o<<"("<<p.x <<","<<p.y <<")";
return o;
}
int main()
{
point a,b(1,2);
a.x =3;
cout<<a+b<<"\n";
return 0;
}