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;
}
相关推荐
lang20150928几秒前
Java反射利器:Apache Commons BeanUtils详解
java·开发语言·apache
沐知全栈开发2 分钟前
HTML DOM 方法
开发语言
扶苏10023 分钟前
前端js高频面试点汇总
开发语言·前端·javascript
项目題供诗4 分钟前
C语言基础(五)
c语言·开发语言
Mh_ithrha7 分钟前
题目:小鱼比可爱(java)
java·开发语言·算法
l1t10 分钟前
数独优化求解C库tdoku-lib的使用
c语言·开发语言·python·算法·数独
带土118 分钟前
8. C++ explicit 关键字
开发语言·c++
im_AMBER19 分钟前
Leetcode 103 反转链表 II
数据结构·c++·笔记·学习·算法·leetcode
人道领域21 分钟前
【零基础学java】(TCP协议)
java·开发语言·tcp/ip