编写一个Point类,计算任意两点之间的距离并输出

如题所述

#include<iostream>
#include<cmath>
using namespace std;
class Point{ //坐标点类
public:
const double x,y;
Point(double x=0.0, double y=0.0): x(x),y(y){}
double distanceTo(Point p)const{ //到指定点的距离
return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)) ;
}
};

int main(){
Point a(1.0, 8.0), b(5.0, 2.0);//两个点,数据可以自己定
cout<<"距离是:"<<a.distanceTo(b)<<endl;//调用函数计算a和b之间的距离
return 0;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答