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

相关推荐
吴阿福|一人公司9 分钟前
Python 类变量修改的压力测试:高并发场景
开发语言·python
天天进步201515 分钟前
Tunnelto 源码解析 #13:自托管部署:Docker、环境变量、端口规划与单实例限制
开发语言
AI科技星16 分钟前
第三卷:质数王朝志(全卷定稿)
c语言·开发语言·汇编·electron·概率论
kyle~28 分钟前
DDS分布式实时系统---自省机制
开发语言·分布式·机器人·c#·接口·ros2
yujunl28 分钟前
Integrated Security=True(Windows 集成身份验证)
开发语言
右耳朵猫AI30 分钟前
Python周刊2026W23 | Polars 1.41、PyPy v7.3.23、Python 3.15、httpx2、dj-lite-tenant
开发语言·python
昭昭颂桉a36 分钟前
TypeScript 前端的必修课,从 JS 到 TS
开发语言·前端·javascript·typescript
何以解忧,唯有..37 分钟前
Go 语言安装与环境配置完整指南
开发语言·后端·golang
alwaysrun37 分钟前
C++之常量体系const
c++·后端·程序员
郝学胜_神的一滴39 分钟前
CMake 016:深入浅出变量核心用法
c++·cmake