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;
}
相关推荐
xushichao19893 分钟前
C++中的职责链模式实战
开发语言·c++·算法
fqbqrr5 分钟前
2603C++,C++强项
c++
清风徐来QCQ9 分钟前
js中的模板字符串
开发语言·前端·javascript
2301_8184190116 分钟前
C++中的协程编程
开发语言·c++·算法
add45a18 分钟前
C++中的工厂方法模式
开发语言·c++·算法
java1234_小锋18 分钟前
Java高频面试题:Spring-AOP通知和执行顺序?
java·开发语言·spring
番茄去哪了22 分钟前
Java基础面试题day02
java·开发语言·面向对象编程
xushichao198926 分钟前
C++中的工厂模式高级应用
开发语言·c++·算法
njsgcs26 分钟前
c# solidworks 折弯系数检查
开发语言·c#
SuperEugene30 分钟前
Vue3 + Element Plus 表格实战:批量操作、行内编辑、跨页选中逻辑统一|表单与表格规范篇
开发语言·前端·javascript