自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),
代码:
#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)
{
this->width = w;
this->height = h;
}
void Rect::set_w(int w)
{
this->width = w;
}
void Rect::set_h(int h)
{
this->height = h;
}
void Rect::show()
{
cout << "周长为:" << width+height << endl;
cout << "面积为:" << width*height << endl;
}
int main()
{
int height,width;
Rect s1;
s1.init(5,10);
cout << "初始的周长和面积:" << endl;
s1.show();
cout << "请输入要更改的长和宽:";
cin >> height >> width;
s1.set_h(height);
s1.set_w(width);
s1.show();
return 0;
}
思维导图:
原文地址:https://blog.csdn.net/qq_56986199/article/details/134696155
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_30994.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!