2021-06-03

7-2 (组合类)一定平面二维点类Point,线段类Line是由两个Point点组成的组合类 (30 分)
定义平面二维点类Point,(有x,y坐标,构造函数、复制构造函数,输出函数)。线段类Line是由两个Point点组成的组合类(数据成员: 两个端点,线段长度; 函数成员:构造函数、复制构造函数、计算线段长度函数) //主函数 int main() {
int x1,y1,x2,y2; cin>>x1>>y1;
cin>>x2>>y2; Point myp1(x1,y1),myp2(x2,y2); //建立Point类的对象 Line L1(myp1,myp2); //建立Line类的对象 cout<<“Line start Point is:”; L1.GetPstart().print(); cout<<endl; cout<<“Line end Point is:”; L1.GetPend().print(); cout<<endl; cout<<“Length of Line is:”<<L1.GetLen()<<endl;

}

输入格式:
输入两行,第一行为线段的起点坐标(中间已空格隔开),第二行为线段的终点坐标 (中间已空格隔开)

输出格式:
输入各个函数被调用过程主程序,需要填写。

输入样例:
在这里给出一组输入。例如:

0 0
3 4
输出样例:
在这里给出相应的输出。例如:

create Point:P(0,0)
create Point:P(3,4)
Create a new Line:
Line start Point is:P(0,0)
Line end Point is:P(3,4)
Length of Line is:5

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
    int x,y;
public:
    int getx()
    {
        return x;
    }
    int gety()
    {
        return y;
    }
    void print()
    {
        cout<<"P("<<Point::x<<","<<Point::y<<")"<<endl;
    }
    Point(int a,int b)
    {
        Point::x=a;
        Point::y=b;
        cout<<"create Point:";
        Point::print();
    }
    Point(const Point& a)
    {
        x=a.x;
        y=a.y;
    }

};
class Line {
private:
    Point a,b;
    double c;
public:
    Line(Point c,Point d):a(c),b(d)
    {
        cout<<"Create a new Line:"<<endl;
    }
    void GetPstart()
    {
        a.print();
    }

    void GetPend() {
        b.print();

    }
    int GetLen()
    {
        auto x=static_cast<double>(a.getx()-b.getx());
        auto y=static_cast<double>(a.gety()-b.gety());
        c=sqrt(x*x+y*y);
        return c;
    }
};

int main() {
    int x1,y1,x2,y2;
    cin>>x1>>y1;
    cin>>x2>>y2;
    Point myp1(x1,y1),myp2(x2,y2); 
    Line L1(myp1,myp2); 
    cout<<"Line start Point is:";
    L1.GetPstart();
    cout<<"Line end Point is:";
    L1.GetPend();
    cout<<"Length of Line is:"<<L1.GetLen()<<endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值