2024.3.12 C++

1、自己封装一个矩形类(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;
    int height;
public:
    void init(int w, int h)
    {
        width = w;
        height = h;
    }
    void set_w(int w)
    {
        width = w;
    }
    void set_h(int h)
    {
        height = h;
    }
    void show()
    {
        cout << "width = " << width << endl;
        cout << "height = " << height << endl;
    }

};

int main()
{
    Rect rec1;
    rec1.init(5, 6);
    rec1.show();

    rec1.set_w(7);
    rec1.set_h(9);
    rec1.show();


    return 0;
}

2、思维导图

相关推荐
小白学大数据18 分钟前
现代Python爬虫开发范式:基于Asyncio的高可用架构实战
开发语言·爬虫·python·架构
渔舟小调28 分钟前
P19 | 前端加密通信层 pikachuNetwork.js 完整实现
开发语言·前端·javascript
不爱吃炸鸡柳30 分钟前
数据结构精讲:树 → 二叉树 → 堆 从入门到实战
开发语言·数据结构
网络安全许木30 分钟前
自学渗透测试第21天(基础命令复盘与DVWA熟悉)
开发语言·网络安全·渗透测试·php
t***54435 分钟前
如何在Dev-C++中使用Clang编译器
开发语言·c++
码界筑梦坊1 小时前
93-基于Python的中药药材数据可视化分析系统
开发语言·python·信息可视化
Qbw20041 小时前
【Linux】进程地址空间
linux·c++
Cosmoshhhyyy2 小时前
《Effective Java》解读第49条:检查参数的有效性
java·开发语言
棋子入局2 小时前
C语言制作消消乐游戏(2)
c语言·开发语言·游戏
布谷歌2 小时前
常见的OOM错误 ( OutOfMemoryError全类型详解)
java·开发语言