C++ Qt day2

自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),

定义公有成员函数:

初始化函数:void init(int w, int h)

更改宽度的函数:set_w(int w)

更改高度的函数:set_h(int h)

输出该矩形的周长和面积函数:void show()

cpp 复制代码
#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;
}
相关推荐
a努力。1 分钟前
字节Java面试被问:系统限流的实现方式
java·开发语言·后端·面试·职场和发展·golang
独自破碎E3 分钟前
Java中的Exception和Error有什么区别?
java·开发语言
zyx没烦恼10 分钟前
YAML模块
开发语言·python
智航GIS14 分钟前
4.2 集合(Set)
开发语言·python
小白学大数据17 分钟前
利用 Selenium 与 BeautifulSoup 构建链家动态爬虫
开发语言·爬虫·selenium·beautifulsoup
qq_3771123717 分钟前
从零开始深入理解并发、线程与等待通知机制
java·开发语言
花哥码天下25 分钟前
修复Bash脚本Here Document错误
开发语言·bash
Rysxt_30 分钟前
UniApp uni_modules 文件夹详细教程
开发语言·javascript·ecmascript
Wang's Blog1 小时前
Lua: 核心机制解析之函数的多维魔法与模块化封装艺术
开发语言·lua
小高Baby@1 小时前
使用Go语言中的Channel实现并发编程
开发语言·后端·golang