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;
}
相关推荐
倒流时光三十年14 小时前
Java 内存模型(JMM)通俗解释
java·开发语言
码兄科技15 小时前
Java AI智能体开发实战:从零构建企业级智能应用指南
java·开发语言·人工智能
zh路西法15 小时前
【现代控制理论与卡尔曼滤波】从状态空间到Python仿真实现
开发语言·python
Evand J15 小时前
【论文复现】MATLAB例程,存在测距误差的WSN无锚点分布式自定位,《WSN中存在测距误差的无锚点分布式自定位方法》
开发语言·分布式·matlab·定位·导航·wsn
techdashen15 小时前
kTLS 进入 rustls 组织:把 TLS 的数据面交给内核
开发语言·php
Lhappy嘻嘻16 小时前
Java 并发编程(六)|并发进阶高频:CAS、锁升级
java·开发语言
techdashen16 小时前
Arborium:把 tree-sitter 语法高亮打包成 Rust 文档生态的基础设施
开发语言·后端·rust
尘中远16 小时前
【Qwt 7.0 系列】常用图表类型实战 —— 柱状图、散点图、箱线图与直方图
qt·qwt·工业软件·科学绘图
会周易的程序员16 小时前
microLog 后端开发指南
开发语言·c++·物联网·设计模式·日志·iot·aiot
Esaka_Forever16 小时前
Python 完整内存管理机制详解
开发语言·python·spring