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

相关推荐
仰泳之鹅几秒前
【FreeRTOS】调试技巧篇
开发语言
量子炒饭大师8 分钟前
【C++入门】Cyber高维的蜂巢意识 —— 【类与对象】static 成员
开发语言·c++·静态成员变量·static成员
开开心心就好9 分钟前
实用PDF批量加马赛克,抹除敏感信息绿色版
java·linux·开发语言·网络·人工智能·pdf·word2vec
不想看见40412 分钟前
Word Ladder Ⅱ -- 广度优先搜索--力扣101算法题解笔记
开发语言·c#
xyq202417 分钟前
HTML 速查列表
开发语言
ShineWinsu21 分钟前
对于stack和queue经典算法题目:155. 最小栈、JZ31 栈的压入、弹出序列和102. 二叉树的层序遍历的解析
数据结构·c++·算法·面试·力扣·笔试·牛客网
全栈小521 分钟前
【C#】.net 6.0和8.0有什么区别,以及8.0增加了那些功能,这些功能有那些作用
开发语言·c#·.net
SWAGGY..23 分钟前
【c++初阶】:(1)c++入门基础知识
开发语言·c++
froginwe1125 分钟前
Bootstrap 输入框组
开发语言
lly20240626 分钟前
HTML 脚本:构建交互式网页的基石
开发语言