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