定义点类( Point ),用以表示几何学点的概念,有属性 x 、 y 表示坐标,并重载"-"单目运算符和"=="双目运算符,要求"-"实现对象的成员变量的数值符号取反,而"=="实现判断两个 Point 类的对象坐标是否相同。
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int x0, int y0) :x(x0), y(y0)
{
}
void show()
{
cout << "(" << x << "," << y << ")" << endl;
}
Point operator-()
{
return Point (-x,-y);
}
bool operator== (const Point& c3)
{
if (this->x == c3.x&&this->y==c3.y)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
Point p1(3, 4);
Point p2 =-p1;
p2.show();
if (p1 == p2)
{
cout << "两个点坐标相同" << endl;
}
else
{
cout << "两个点坐标不相同" << endl;
}
return 0;
}
标签:p2,p1,return,Point,int,c++,运算符,重载
From: https://blog.csdn.net/2301_80790320/article/details/136854320