point_line_友元类

描述
用友元类解决下面的问题: 设计一个point 类.其中:数据成员: 点的坐标x,y;成员函数:带有参的构造函数(不带默认值),其他成员函数不做要求。 定义一个line类 要求:数据成员:线上的两个点point1,point2(用定义好的point)成员函数: 定义一条直线 计算线段的长度

输入
两个点的坐标
输出
线段的长度(保留小数点后两位数)
样例输入
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 class line;
};
class line
{
    
    
private:
    Point P1,P2;
public:
    line(Point p1,Point p2):P1(p1),P2(p2){
    
    }
    double dist()
    {
    
    
        return sqrt( (P1.x-P2.x)*(P1.x-P2.x) +  (P1.y-P2.y)*(P1.y-P2.y) );
    }
};
int main()
{
    
    
    Point p1,p2;
    line d(p1,p2);
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<d.dist()<<endl;
}

猜你喜欢

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