赞
踩

封装一个矩形类
- #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 ;
- s1.set_w(w);
- cout << "请输入要更改的长度:";
- cin >> h;
- s1.set_h(h);
- s1.show();
- return 0;
- }


Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。