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;
}
相关推荐
Z***25808 分钟前
JavaScript虚拟现实案例
开发语言·javascript·vr
tan180°16 分钟前
Linux网络TCP(上)(11)
linux·网络·c++·后端·tcp/ip
Halo_tjn26 分钟前
Set集合专项实验
java·开发语言·前端·python
席万里27 分钟前
关于Go的init函数执行顺序#黑魔法
开发语言·网络·golang
橘子真甜~1 小时前
C/C++ Linux网络编程6 - poll解决客户端并发连接问题
服务器·c语言·开发语言·网络·c++·poll
铭哥的编程日记1 小时前
【标准项目】C++基于正倒排索引的Boost搜索引擎
c++·搜索引擎
9***Y481 小时前
Java开发工具IntelliJ IDEA技巧
java·开发语言·intellij-idea
码力码力我爱你1 小时前
C++性能基准测试
开发语言·c++
张人玉1 小时前
C#WPF——MVVM框架编写管理系统所遇到的问题
开发语言·c#·wpf·mvvm框架
大师兄66682 小时前
Qt-for-鸿蒙PC-Electron应用鸿蒙平台白屏问题修复实战
qt·electron·harmonyos