#include <iostream>
using namespace std;
class Rect
{
private:
int width;
int height;
public:
void init(int width, int height)
{
Rect::width = width;
this->height = height;
}
void set_w(int w)
{
width = w;
}
void set_h(int h)
{
height = h;
}
void show()
{
cout << "矩形周长为:" << 2*(width + height) << endl;
cout << "矩形的面积为:" << width * height << endl;
}
};
int main()
{
cout << endl;
Rect rec;
rec.init(2, 4);
//输出此矩形的周长和面积
rec.show();
cout << endl;
cout << endl;
//改变矩形的宽和高
rec.set_w(4);
rec.set_h(8);
//输出改变后的周长和面积
rec.show();
return 0;
}
C++day2
最新推荐文章于 2024-06-06 21:46:22 发布