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;
}
相关推荐
炸膛坦客9 分钟前
单片机/C/C++八股:(二十一)include <> 和 include ““ 的区别
c语言·c++
Yupureki17 分钟前
《Linux系统编程》12.基础IO
linux·运维·c语言·开发语言·数据库·c++
淮北49418 分钟前
bash下好用的快捷键以及linux常用指令
linux·开发语言·ubuntu·bash
Jordannnnnnnn18 分钟前
追赶32名
c++
炸膛坦客19 分钟前
单片机/C/C++八股:(十八)C/C++ 中 sizeof 和 strlen 的区别
c语言·c++
薛定谔的猫喵喵36 分钟前
卸载 Python 3.8 报错 “Could not set file security” 的终极解决方案
开发语言·python
l1t1 小时前
编译测试clickhouse-cpp客户端
c++·clickhouse
看山是山_Lau1 小时前
代码命名规范原则与原理
c语言·开发语言
tankeven1 小时前
HJ147 最大 FST 距离
c++·算法