#define _CRT_SECURE_NO_WARNINGS
/*4、编写程序sys13 - 4.c,请编写一个Point类,要求类中属性为:横坐标x,纵坐标y;行为为:从键盘输入两个点的坐标;求两点之间的距离。
class Point
{
private:float x, y;
public:
void set_xy(float a, float b);
float distance_xy(Point &p1);
};
① 点A(x1, y1)和点B(x2, y2)两点间的距离公式:
② distance_xy()函数中可用<cmath>头文件中的sqrt()函数和pow()函数。
sqrt(参数x) //表示求参数x的的平方根;
pow(参数x, 参数y) //表示求参数x的y次方的值。
}*/
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public :
void setxy(float a, float b);
float distancexy(Point &p1,Point &p2);
private:float x, y;
};
int main()
{
float x1,y1,x2,y2;
Point p1, p2;
cout << "please input first point:" << endl;
cin >> x1 >> y1;
cout << "please input second point:" << endl;
cin >> x2 >> y2;
p1.setxy(x1,y1);
p2.setxy(x2,y2);
float dis;
dis = p1.distancexy(p1, p2);
cout << "两点之间的距离是:" << dis << endl;
return 0;
}
void Point::setxy(float a,float b)
{
x = a, y = b;
}
float Point::distancexy(Point &p1, Point &p2)
{
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}