用c++编写:实现一个Point类,该类拥有私有数据成员x和y,要求用运算符重载的方式实现以下功能: 1)前缀自增(即把x和y的值均加1)。 2)大于运算(x * x + y * y的值大者为大)。 3)加法运算(两个点的加法为x和y对应相加)。 再写一段测试代码,测试类Point的功能。
时间: 2023-07-14 18:11:43 浏览: 35
```c++
#include<iostream>
using namespace std;
class Point{
private:
int x,y;
public:
Point(int a=0,int b=0):x(a),y(b){}
Point operator++();//前缀自增
bool operator>(const Point& p);//大于运算
Point operator+(const Point& p);//加法运算
void show(){cout<<"("<<x<<","<<y<<")"<<endl;}//输出坐标
};
Point Point::operator++(){
x++;y++;
return *this;
}
bool Point::operator>(const Point& p){
return x*x+y*y>p.x*p.x+p.y*p.y;
}
Point Point::operator+(const Point& p){
return Point(x+p.x,y+p.y);
}
int main(){
Point p1(1,2);
Point p2(3,4);
Point p3=p1+p2;
p3.show();
++p3;
p3.show();
if(p3>p1) cout<<"p3>p1"<<endl;
else cout<<"p3<=p1"<<endl;
return 0;
}
```
输出结果:
```
(4,6)
(5,7)
p3>p1
```