1.思维导图
2.自己封装一个矩形类(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//类
{
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 << "该矩形周长为" << 2*(width+height) << ",面积为" << width*height << endl;
}
private:
double width;
double height;
};
int main()//改变宽高,计算周长和面积的自定义类
{
int w=3,h=4;
Rect pa;
pa.init(w,h);
pa.show();
h=3;
pa.set_h(h);
pa.show();
w=4;
pa.set_w(w);
pa.show();
return 0;
}