c++ 封装 权限

阅读 5

2024-06-05

#include <iostream>

using namespace std;

class Rect
{
private:
    int width;
    int height;
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=" << w << endl;
        //cout << "height=" << h << endl;
        cout << "面积=" << width*height << endl;
        cout << "周长=" << (width+height)*2 << endl;
    }

};

int main()
{
    Rect s1;
    s1.init(8,9);
    s1.set_h(10);
    s1.set_w(10);
    s1.show();

    return 0;
}

思维导图:

精彩评论(0)

0 0 举报