创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要
加入自己的成员变量或成员函数.
#include<iostream>
#include<cmath>
//#include<algorithm>
using namespace std;
class CPoint
{
double x,y;
public:
CPoint(double xx,double yy){x=xx;y=yy;}
double operator- (CPoint c);
};
double CPoint::operator- (CPoint c)
{
return sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y));
}
int main()
{
while(1)
{
double x1,x2,y1,y2;
cout<<"请输入第一个点的坐标:"<<endl;
cin>>x1>>y1;
CPoint a(x1,y1);
cout<<"请输入第二个点的坐标:"<<endl;
cin>>x2>>y2;
CPoint b(x2,y2);
cout<<"两个点的距离为: "<<a-b<<endl;
}
return 0;
}