- #include
-
- using namespace std;
- class Rect
- {
- 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 << endl;
- cout << "高: " << height << endl;
- cout << "周长: " << (width+height)*2 << endl;
- cout << "面积: " << (width*height) << endl;
-
- }
- };
-
- int main()
- {
- Rect r1;
-
- int a,b;
- cout << "请输入宽:" << endl;
- cin >> a;
- cout << "请输入高:" << endl;
- cin >> b;
- r1.init(a,b);
- r1.set_w(a);
- r1.set_h(b);
- r1.show();
- return 0;
- }