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;
}
相关推荐
玉树临风江流儿27 分钟前
C++左值、右值、move移动函数
开发语言·c++
拾荒的小海螺1 小时前
JAVA:Spring Boot3 新特性解析的技术指南
java·开发语言·spring boot
程序猿20231 小时前
Python每日一练---第二天:合并两个有序数组
开发语言·python
椰羊sqrt1 小时前
CVE-2025-4334 深度分析:WordPress wp-registration 插件权限提升漏洞
android·开发语言·okhttp·网络安全
Js_cold1 小时前
Verilog任务task
开发语言·fpga开发·verilog
njxiejing1 小时前
Numpy一维、二维、三维数组切片实例
开发语言·python·numpy
许长安2 小时前
c/c++ static关键字详解
c语言·c++·经验分享·笔记
一位搞嵌入式的 genius2 小时前
前端实战开发(四):从迭代器到异步编程:ES6 Generator 全面解析 + 实战问题排查
开发语言·前端·es6·前端实战
来来走走2 小时前
Android开发(Kotlin) 高阶函数、内联函数
android·开发语言·kotlin
Murphy_lx2 小时前
C++ thread类
开发语言·c++