- #include
- /*自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),
- 定义公有成员函数:
- 初始化函数:void init(int w, int h)
- 更改宽度的函数:set_w(int w)
- 更改高度的函数:set_h(int h)
- 输出该矩形的周长和面积函数:void show()*/
-
- using namespace std;
-
- struct Rect
- {
- private:
- int width;
- int height;
- public:
- void init(int w, int h);
- void set_w(int w);
- void set_h(int h);
- void show();
- };
-
- void Rect::init (int w, int h)
- {
- width = w;
- height = h;
- }
-
- void Rect::set_w(int w)
- {
- width = w;
- }
-
- void Rect::set_h(int h)
- {
- height = h;
- }
-
- void Rect::show()
- {
- cout << "矩形的周长为:" << (width+ height)*2 << endl;
- cout << "矩形的面积为:" << width * height;
- }
-
- int main()
- {
- int w, h;
- cout << "输入宽度>>>";
- cin >> w;
- cout << "输入高度>>>";
- cin >> h;
- Rect r;
- r.set_w(w);
- r.set_h(h);
- r.show();
- return 0;
- }

