作业要求:
代码:
#include <iostream>using namespace std;class Rect
{
private:int width;int height;public:void init(int w,int h);void set_w(int w);void set_h(int h);void show();
};void Rect::init(int w,int h)
{width = w;height = h;
}void Rect::set_w(int w)
{width = w;
}void Rect::set_h(int h)
{height = h;
}void Rect::show()
{cout << "矩形周长为: " << (width+height)*2 << endl;cout << "矩形面积为: " << width*height << endl;
}int main()
{Rect rect;int wid,hei;cout << "请输入矩形的宽和高:";cin >> wid >> hei;rect.init(wid,hei);rect.show();cout << "请输入要改变的矩形的宽:";cin >> wid;rect.set_w(wid);rect.show();cout << "请输入要改变的矩形的高: ";cin >> hei;rect.set_h(hei);rect.show();return 0;
}
代码效果图:
思维导图: