//定义一个Location类,重载运算符+和-实现平面类点的移动
#include<iostream>
using namespace std;
class Location
{
private:
int x,y;
public:
Location(int x=0,int y=0)
{
this->x=x;
this->y=y;
}
void Show()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
Location operator +(Location B)
{
return Location(x+B.x,y+B.y);
}
Location operator -(Location B)
{
return Location(x-B.x,y-B.y);
}
};
int main()
{
Location A(1,2),B(2,1),C;
A.Show();
C=A.operator +(B);
C.Show();
C=C.operator -(B);
C.Show();
return 0;
}
重载运算符的应用1
最新推荐文章于 2023-11-19 00:15:00 发布