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 wideth;
int height;
public:
void init(int wideth,int height);
void set_w(int wideth);
void set_h(int height);
void show();
};
//将终端输入的长块赋值给类中的长宽
void Rect::init(int wideth, int height)
{
Rect::wideth=wideth;
this->height=height;
}
//更改宽度
void Rect::set_w(int wideth)
{
Rect::wideth=wideth;
}
//更改长度
void Rect::set_h(int height)
{
this->height=height;
}
//计算周长与面积并在终端输出
void Rect::show()
{
cout << "Long=" << 2*wideth+2*height << endl;
cout << "area=" << wideth*height << endl;
}
int main()
{
//示例化一个对象
Rect r1;
r1.init(0,0);
//终端输入宽
int w;
cout << "Please input w" << endl;
cin >> w;
r1.set_w(w);
//终端输入长
int h;
cout << "Please input h" << endl;
cin >> h;
r1.set_h(h);
r1.show();
return 0;
}
2.思维道图