自己封装一个矩形类(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);
int set_w(int w);
int set_h(int h);
void show();
};
void Rect::init(int w, int h)
{
this->width = w;
this->height = h;
}
int Rect::set_w(int w)
{
cout << "请输入修改的width:";
cin >> w;
return w;
}
int Rect::set_h(int h)
{
cout << "请输入修改的height:";
cin >> h;
return h;
}
void Rect::show()
{
cout << "C = " << (width+height)*2 << endl;
cout << "S = " << width*height << endl;
}
int main()
{
Rect r1;
r1.init(0,0);
cout << "初始为:" << endl;
r1.show();
int w = r1.set_w(w);
int h = r1.set_h(h);
r1.init(w,h);
cout << "修改后为:" << endl;
r1.show();
return 0;
}