计算两点间的距离的C++程序

#include
#include
using namespace std;
class Point
{
public:
int x, y;
public:
Point(int a, int b)
{
x = a;
y = b;
}
};
class Distance
{
private:
int x1, y1, x2, y2;
public:
Distance(Point Point1, Point Point2)
{
x1 = Point1.x;
y1 = Point1.y;
x2 = Point2.x;
y2 = Point2.y;
}

void calDistance()
{
int c = abs(x2-x1); //求出横坐标距离
int d = abs(y2-y1); //求出纵坐标距离
cout << "两点的距离是:" << sqrt(c*c + d*d) << endl;
}
};
int main()
{
int P1x, P1y, P2x, P2y;
cout << "此程序用来计算两点的距离" << endl;
cout << "请输入第一个点的坐标:";
cin >> P1x >> P1y;
cout << "请输入第二个点的坐标:";
cin >> P2x >> P2y;
Point P1(P1x,P1y);
Point P2(P2x,P2y);
Distance P1P2(P1,P2); //把点P1和点P2的坐标传给类Distance,生成对象P1P2
P1P2.calDistance();
return 0;
}

相关文档
最新文档