一定平面二维点类Point,线段类Line是由两个Point点组成的组合类 (30 分)

一定平面二维点类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< cout<<"Line end Point is:"; L1.GetPend().print();
cout< cout<<"Length of Line is:"< }
输入格式:
输入两行,第一行为线段的起点坐标(中间已空格隔开),第二行为线段的终点坐标 (中间已空格隔开)
输出格式:
输入各个函数被调用过程主程序,需要填写。
输入样例:
在这里给出一组输入。例如:
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
using namespace std;
class Point
{//(有x,y坐标,构造函数、复制构造函数,输出函数)。
    friend class Line;
private:
    int x, y;
public:
    Point(int x1, int x2)
    {
        this->x = x1;
        this->y = x2;
        cout << "create Point:P" << "(" << this->x << "," << this->y << ")" << endl;
    }
    Point(Point& p)
    {
        this->x = p.x; this->y = p.y;
    }
    //输出函数
    void print()
    {
        cout << "P" << "(" << this->x << "," << this->y << ")";
    }
};
class Line
{
private:
    Point p1;
    Point p2;
public:
    Line(Point& p1, Point& p2) :p1(p1), p2(p2) 
    {
        cout << "Create a new Line:" << endl;
    }
    Point GetPstart()
    {
        return this->p1;
    }
    Point GetPend()
    {
        return this->p2;
    }
    double GetLen()
    {
        return sqrt((this->p1.x - this->p2.x) * (this->p1.x - this->p2.x) +
            (this->p1.y - this->p2.y) * (this->p1.y - this->p2.y));
    }
};
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;
}

你可能感兴趣的:(c++)