Point_友元函数

描述
定义一个Point类,用来描述平面上的一个点.要求支持以Point A,B(0,0)等方式完成对象的生成.定义【友元函数】Dist,计算并返回两点之间的距离

输入
两个点的坐标
输出
两点之间的距离(保留小数点后两位数)
样例输入
0 0 0 2
样例输出
2.00

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Point{
    
    
private:
    int x,y;
public:
    Point()
    {
    
    
        cin>>x>>y;
    }
    friend double Dist(Point &p1,Point &p2);
};
double Dist(Point &p1,Point &p2)
    {
    
    
        return  sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y)  );
    }
int main()
{
    
    
    Point p1,p2;
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<Dist(p1,p2)<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_45921943/article/details/105074034