C++ Qt day2
自己封装一个矩形类(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;
private:int height;
public:void init(int w,int h)
{
width=w;
height=h;
show();
}
public:void set_w(int w)
{
width=w;
}
public:void set_h(int h)
{
height=h;
}
public:void show()
{
cout << "周长为:" << 2*(height+width) << endl;
cout << "面积为:" << height*width << endl;
}
};
int main()
{
Rect rect;
int width,height;
cout << "请输入宽:";
cin>>width;
cout << "请输入高:";
cin>>height;
rect.init(width,height);
cout << "更改宽:";
cin>>width;
rect.set_w(width);
cout << "更改高:";
cin>>height;
rect.set_h(height);
rect.init(width,height);
return 0;
}
原文地址:https://blog.csdn.net/2203_75782206/article/details/135853801
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!