Point_line_triangle_类的组合

描述
设计一个Point类,表示平面中的一个点
设计一个Line类,表示平面的一条线段
设计一个Triangle类(三角形类),内含三条边。
要求:
设计三个类的相应的构造函数、复制构造函数,完成初始化和对象复制
设计Triangle类的成员函数,分别完成三条边能否构成三角形的检查,三角形周长的计算

输入
三个点
输出
三角形的周长(保留小数点后三位数)。如果不能构成三角形,输出 no
样例输入
0 0
0 1
1 0
样例输出
3.414

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Point
{
private:
    int x,y;
public:
    Point(int xx,int yy)
    {
        x=xx;
        y=yy;
    }
    friend class Line;
};
class Line
{
private:
    Point A,B;
public:
    Line(Point AA,Point BB):A(AA),B(BB){}
    double length()
    {
        return sqrt(  (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)  );
    }
    friend class Triangle;
};
class Triangle
{
private:
    Line L1,L2,L3;
    double l1,l2,l3;
public:
    Triangle(Line LL1,Line LL2,Line LL3):L1(LL1),L2(LL2),L3(LL3){}
    void judge()
    {l1=L1.length();
    l2=L2.length();
    l3=L3.length();
    if(l1+l2>l3&&l2+l3>l1&&l1+l3>l2) cout<<fixed<<setprecision(3)<<(l1+l2+l3)<<endl;
        else cout<<"no"<<endl;}
};
int main()
{
    int x1,y1,x2,y2,x3,y3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    Point p1(x1,y1);
    Point p2(x2,y2);
    Point p3(x3,y3);
    Line LL1(p1,p2);
    Line LL2(p2,p3);
    Line LL3(p1,p3);
    Triangle TRI(LL1,LL2,LL3);
    TRI.judge();
}

注意初始化列表:
• 数据成员为常量
• 数据成员为引用类型
• 数据成员为没有无参构造函数的类的对象

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值