64,636
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Point
{
private:
float x;
float y;
public:
Point()
{
x = 0;
y = 0;
}
Point (float a, float b):x(a), y(b){}
Point &operator-(const Point& other)
{
x = x - other.x ;
y = y - other.y;
return *this;
}
void disp()
{
cout<<"Point:";
cout<<"("<<x<<","<<y<<")"<<endl;
}
};
int main(int argc, char*argv[])
{
Point objone;
Point objtwo(1,1);
Point objthree;
objone.disp();
objtwo.disp();
//objthree = objtwo-objone;
objthree = objone-objtwo;
objthree.disp();
objone.disp();
objtwo.disp();
return 0;
}
Point &operator-(const Point& other)
{
x = x - other.x ;
y = y - other.y;
return *this;
}
改成
Point &operator-(const Point& other)
{
return point(x-other.x,y-other.y);
}