作业帮 > 综合 > 作业

计算2点的距离根据这个Point类,计算出距离.写个能运行的.class Point{public:Point(doub

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/08 21:50:46
计算2点的距离
根据这个Point类,计算出距离.写个能运行的.
class Point
{
public:
Point(double nx=0.0,double ny=0.0):x(nx),y(ny){}
Point(Point &np):x(np.x),y(np.y){}
double GetX(){return x;}
double GetY(){return y;}
void SetX(double nx){x=nx;}
void SetY(double ny){y=ny;}
void SetPoint(Point &np){x=np.x;y=np.y;}
private:
double x,y;
};
该题有好多种写法.我用C++写了其中的一种,代码如下:
#include
#include
using namespace std;
class Point
{
public:
Point(double nx=0.0,double ny=0.0):x(nx),y(ny){}
Point(Point &np):x(np.x),y(np.y){}
double GetX(){return x;}
double GetY(){return y;}
void SetX(double nx){x=nx;}
void SetY(double ny){y=ny;}
void SetPoint(Point &np){x=np.x;y=np.y;}
private:
double x,y;
};
int main(){
double distance;//A,B两点间的距离
Point A(1.5,2.8);
Point B(3.9,4.6);
distance = sqrt((B.GetY()-A.GetY())*(B.GetY()-A.GetY())+(B.GetX()-A.GetX())*(B.GetX()-A.GetX()));
cout