源程序:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
private:
double x, y;
public:
Point(double a, double b)
{
x = a;
y = b;
}
double distance()
{
double len;
len = sqrt(x*x+y*y);
return len;
}
friend double dis_point2(Point &,Point &);
};
double dis_point2(Point &s1, Point &s2)
{
double t_x = s1.x - s2.x;
double t_y = s1.y - s2.y;
double t_xy = sqrt(t_x * t_x + t_y * t_y);
return t_xy;
}
int main()
{
Point p1(2.5, 6.3), p2(5.5,9.0);
cout << "p1到原点的距离为:"<<p1.distance() << endl;
cout << "p2到原点的距离为:" <<p2.distance() << endl;
cout << "p1到p2的距离为:"<<dis_point2(p1, p2) << endl;
system("pause");
return 1;
}
运行结果: