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、思维导图

相关推荐
信奥卷王几秒前
2025年9月GESPC++三级真题解析(含视频)
开发语言·c++·算法
喵了几个咪16 分钟前
Golang微服务框架kratos实现Socket.IO服务
开发语言·微服务·golang
q***420518 分钟前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
qq_433554541 小时前
C++ 稀疏表
开发语言·c++·算法
Bona Sun1 小时前
单片机手搓掌上游戏机(十二)—esp8266运行gameboy模拟器之编译上传
c语言·c++·单片机·游戏机
帅中的小灰灰1 小时前
C++编程观察者设计模式
数据库·c++·设计模式
z***y8621 小时前
Java数据挖掘开发
java·开发语言·数据挖掘
软件开发技术深度爱好者2 小时前
Python库/包/模块管理工具
开发语言·python
bubiyoushang8882 小时前
基于MATLAB的自然图像梯度分布重尾特性验证方案
开发语言·matlab
MSTcheng.2 小时前
【C++STL】priority_queue 模拟实现与仿函数实战
开发语言·c++