/*5、编写C++程序,实现如下功能:
a.定义一个类,Point, 具有x, y轴坐标两个私有数据成员(float类型);
b.具有获取和设置x,y值的公有函数;写出两种构造函数;其原型为:
Point();//此时初始化x,y为0
Point(float xx,float yy);
c.具有计算与另一个类对象p1之间距离的公有函数:float calculate_distance(Point p1);
d. 在主函数中,提示输入p1, p2两点的x,y坐标,并计算两点的距离;
e.在main函数中测试你所设计的所有成员函数。*/
#include <iostream>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Point{
public:
Point(){
x=0;
y=0;
} //无参数的构造函数
Point(float xx,float yy); //有参数的构造函数
float calculate_distance(Point p1);
private:
float x,y;
};
Point::Point(float xx,float yy){
x=xx;
y=yy;
cout<<"("<<x<<","<<y<<")"<<endl;
}
float Point::calculate_distance(Point p1){
return (float)sqrt((x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y));
}
int main(int argc, char** argv) {
Point p,p1; //定义对象p、p1
float x,y; //对象p的私有数据成员
float x1,y1; //对象p1的私有对象成员
float d1=0,d2=0;
cin>>x>>y;
cin>>x1>>y1;
Point p; //调用无参数的构造函数
Point p(x,y); //调用有参数的构造函数
p1.Point(x1,y1);
d1=p.calculate_distance(p1);
d2=p1.calculate_distance(p);
cout<<"两点间距离为"<<d1<<endl;
cout<<"两点间距离为"<<d2<<endl;
return 0;
}