代码如下:
#include<iostream>
using namespace std;
class Point
{
public:
Point(){}
Point(int xx,int yy):x(xx),y(yy){}
friend Point operator +(Point&p1,Point &p2);
void display(){cout<<"("<<x<<","<<y<<")"<<endl;}
private:
int x;
int y;
};
Point operator +(Point &p1,Point &p2)
{
return Point(p1.x+p2.x,p1.y+p2.y);
}
int main()
{
Point p1(1,2),p2(2,3),p3;
p3=p1+p2;
cout<<"p1+p2=";p3.display();//若写为<<p3.display()则会出现错误提示
没有定义重载运算符<<
return 0;
}