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 width;int height;public:void init(int w,int h){this->width = w;this->height = h;}int set_w(int w){this->width = w;return w;}int set_h(int h){this->height = h;return h;}void show(){double per = (height + width)*2;double area = height * width;cout << "该矩形的周长为:" << per << endl;cout << "该矩形的面积为:" << area << endl;}
};int main()
{Rect stu;//定义初始值stu.init(3,4);//改变宽度int wa;cout << "请输入宽度:" << endl;cin >> wa;stu.set_w(wa);//改变长度int ha;cout << "请输入长度:" << endl;cin >> ha;stu.set_h(ha);stu.show();return 0;
}
2.
思维: