1、设计点类 Point,能够表示平面当中的任意点
(1)数据成员包括两点坐标(x,y),成员函数包括构造函数、析构函数、复制构造函数;
(2)包括求点的坐标的公有接口函数,打印点坐标的成员函数,并在主函数中调用。
(3)在主函数中实例化出两个点a(0,0),b(6,8),求出两点间的距离。
设计代码已给出,望采纳!
class Point
{
public:
Point(double x=0,double y=0):x(x),y(y){}
~Point(){}
Point(const Point & po){ //拷贝构造(复制构造)
this->x = po.x;
this->y = po.y;
}
void modify_coordinate(double modx,double mody){ //修改坐标
this->x = modx;
this->y = mody;
}
void printf_coordinate(){ //打印坐标
cout << "点坐标为:" << "(" << this->x << "," << this->y << ")" << endl;
}
void coordinate_distance(Point mp){ //求两点之间的距离
double xd = (this->x - mp.x) * (this->x - mp.x);
double yd = (this->y - mp.y) * (this->y - mp.y);
double dis = pow((xd + yd),0.5); //#include <math.h>
cout << "两点距离为:" << dis << endl;
}
private:
double x;
double y;
};
int main()
{
Point p{3,4};
Point p2 = p;
p2.modify_coordinate(6,6);
p.coordinate_distance(p2);
return 0;
}