C++ day2

思维导图

封装一个矩形类

#include <iostream>

using namespace std;

class Rect
{
private:
    int height;
    int width;

public:
    void init(int w,int h)
    {
        width = w;
        height = h;
    }
    void set_w(int w)
    {
        width = w;
    }

    void set_h(int h)
    {
        height = h;
    }

    void show()
    {

        cout << "周长为:" << (width+height)*2 << endl;
        cout << "面积为:" << width * height << endl;

    }

};


int main()
{
    Rect s1;
    s1.init(10,5);
    s1.show();
    int w,h;
    cout << "请输入更改的宽度:";
    cin >> w ;
    cout << "请输入要更改的长度:";
    cin >> h;
    s1.init(w,h);
    s1.show();
    return 0;
}