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;
}
相关推荐
Ralph_Y2 分钟前
C++数据库操作
开发语言·数据库·c++
酬勤-人间道2 分钟前
CAD 曲线切割 3D 曲面:坡面 / 开挖模型的开挖 - 填埋精准计算解决方案
c++·计算机·cad·布尔计算·曲线切割·工业级解决方案·岩体
superman超哥3 分钟前
Rust 日志级别与结构化日志:生产级可观测性实践
开发语言·后端·rust·可观测性·rust日志级别·rust结构化日志
咸鱼2.06 分钟前
【java入门到放弃】数据结构
java·开发语言·数据结构
啊西:7 分钟前
SuperMap iObjects Java地图生成栅格瓦片并保存到mongodb
java·开发语言·mongodb
老歌老听老掉牙10 分钟前
PyQt5中RadioButton互斥选择的实现方法
开发语言·python·qt
一路往蓝-Anbo13 分钟前
C语言从句柄到对象 (四) —— 接口抽象:从 Switch-Case 到通用接口
c语言·开发语言·stm32·嵌入式硬件
csbysj202014 分钟前
WebPages 数据库:构建现代网页管理的基石
开发语言
lzhdim15 分钟前
C#性能优化:从入门到入土!这10个隐藏技巧让你的代码快如闪电
开发语言·性能优化·c#
不会写代码的里奇17 分钟前
从零开发基于DeepSeek的端侧离线大模型语音助手:全流程指南
c++·后端·音视频