关于c++的类的习题,不太懂怎么设置

11题会怎么弄,但是12题感觉弄不出来,刚学c++类啥的还没怎么搞明白,能提供一个参考一下

img

大致思路就是定义一个point结构体,用于存储坐标。然后定义recranger类,类里面包含函数。获取长度,宽度。周长,面积。最后判断长和宽是否相等。相等为正方形,不相等为长方形
代码如下:

#include <iostream>
using namespace std;

struct Point
{
    int x=0;
    int y=0;
};

class Rectangle{
public:
    Rectangle(Point left_top,Point right_top,Point right_bottom,Point left_bottom)
    {
        m_left_top=left_top;
        m_right_top=right_top;
        m_right_bottom=right_bottom;
        m_left_bottom=left_bottom;
    }
    Rectangle(){}
    ~Rectangle(){}
    void setRectangePoint(Point left_top,Point right_top,Point right_bottom,Point left_bottom)
    {
        m_left_top=left_top;
        m_right_top=right_top;
        m_right_bottom=right_bottom;
        m_left_bottom=left_bottom;
    }

    int getLength()
    {
        int length=abs(m_right_top.x-m_left_top.x);   //abs函数用于取绝对值.防止出现负数
        return length;
    }
    int getWidth()
    {
        int width=abs(m_left_bottom.y-m_left_top.y);
        return width;
    }
    int getPer()
    {
        return 2*(getLength()+getWidth());
    }
    int getArea()
    {
        return getLength()*getWidth();
    }

    bool isSquae()
    {
        if(getWidth()==getLength())
        {
            return true;
        }
        return false;
    }

protected:


private:
    Point m_left_top;
    Point m_right_top;
    Point m_left_bottom;
    Point m_right_bottom;
};

int main()
{
    Point p1;
    Point p2;
    Point p3;
    Point p4;
    p1.x=0;
    p1.y=0;

    p2.x=10;
    p2.y=0;

    p3.x=0;
    p3.y=20;

    p4.x=10;
    p4.y=20;

    Rectangle rectange(p3,p4,p2,p1);
    cout<<"length = "<<rectange.getLength()<<endl;
    cout<<"width = "<<rectange.getWidth()<<endl;
    cout<<"Per = "<<rectange.getPer()<<endl;
    cout<<"area = "<<rectange.getArea()<<endl;
    cout<<"is square = "<<rectange.isSquae()<<endl;

    return 0;
}

效果:

img

有用望采纳