思维导图
自己封装一个矩形类(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 wide;
int hight;
int area;
public:
void init(int w, int h){ //数据初始化函数
wide = w;
hight = h;
area = wide * hight;
}
void set_w(int w){ //更改宽度的函数
wide = w;
}
void set_h(int h){
hight = h;
}
void show_area(){
cout << "area is :" << wide * hight << endl;
}
};
int main()
{
int w,h;
Rect r1;
cout << "请依次输入长和宽 " << endl;
cin >> w;
cin >> h;
r1.init(w,h);
cout << "长方形的面积为:" << endl;
r1.show_area();
return 0;
}