1、自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),定义公有成员函数:
初始化函数:void init(int w, int h)更改宽度的函数:set_w(int w)更改高度的函数:set_h(int h)
输出该矩形的周长和面积函数:void show()
#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()
{
cout << "please enter width: " << endl;
cin >> width;
}
void set_h()
{
cout << "please enter height: " << endl;
cin >> height;
}
void show()
{
cout << "perimeter = " << (height+width)*2 << endl;
cout << "acreage = " << (height*width) << endl;
}
};
int main()
{
class Rect r;
r.init(1,1);
r.set_h();
r.set_w();
r.show();
return 0;
}
2、思维导图