初学C++,请求帮助课后习题把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,再

1个回答

  • 我写一段伪代码,没有经过编译器编译的:

    class CPoint

    {

    public:

    CPoint();

    CPoint(x,y){m_x -= x; m_y = y;};

    void SetPos(int x, int y){m_x -= x; m_y = y;};

    int GetX(){return m_x;};

    int GetY(){return m_y;};

    public:

    int m_x,m_y;

    }

    class CLine : public CPoint

    {

    public:

    CLine();

    CLine(x,y);

    int GetDistance();

    }

    CLine::CLine(x,y)

    :CPoint(x,y)

    {

    }

    int CLine::GetDistance()

    {

    return y-x;

    }

    class CRect : public CLine

    {

    public:

    CRect(left, top, right, bottom);

    int GetSize();

    private:

    CPoint m_leftTop;

    CPoint m_rightBottom;

    }

    CRect::CRect(left, top, right, bottom)

    :m_leftTop(left, top)

    , m_rightBottom(right, bottom)

    {

    }

    int CRect::GetSize()

    {

    return (m_rightBottom.y-m_leftTop.y)*(m_rightBottom.x - m_leftTop.x);

    }

    敲得累死了,没有经过编译编译,应该问题不大。