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;
}
相关推荐
a程序小傲3 小时前
京东Java面试被问:基于Gossip协议的最终一致性实现和收敛时间
java·开发语言·前端·数据库·python·面试·状态模式
tqs_123453 小时前
Spring Boot 的自动装配机制和 Starter 的实现原理
开发语言·python
程序员小白条3 小时前
面试 Java 基础八股文十问十答第二十二期
java·开发语言·数据库·面试·职场和发展·毕设
编程大师哥3 小时前
JavaScript 和 Python 哪个更适合初学者?
开发语言·javascript·python
万象.3 小时前
redis客户端安装与实现C++版本
数据库·c++·redis
建军啊3 小时前
php伪协议、代码审计工具和实战
开发语言·php
WYH2873 小时前
为什么在cubeide里勾选了can1,生成的工程里没有can.c?
c语言·开发语言
3 小时前
java关于键盘录入
java·开发语言
马猴烧酒.3 小时前
JAVA后端对象存储( 图片分享平台)详解
java·开发语言·spring·腾讯云
wearegogog1233 小时前
基于MATLAB的D2D仿真场景实现
开发语言·网络·matlab