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

相关推荐
SomeB1oody44 分钟前
【RustyML入门】2.0. 经典机器学习
开发语言·后端·机器学习·rust·教程
皓月斯语1 小时前
B2132 素数对
c++·算法·题解
0566462 小时前
Python高级——解释器执行原理与虚拟环境工程化实战
开发语言·python
星轨初途2 小时前
LeetCode 热题 100——day2 字母异位词分组
c++·算法·leetcode
HJX_07242 小时前
嵌入式软件C语言八股文复习笔记5——编译、链接与底层硬核机制
c语言·开发语言·笔记
笨鸟先飞的橘猫3 小时前
c++面向对象编程
开发语言·c++
小玮看世界3 小时前
[Python]自动驾驶感知与决策练习题 (1-10)
开发语言·python·自动驾驶
满栀5853 小时前
原生JS + ECharts 多图表可视化实战:从业务实现到工程化优化
开发语言·javascript·echarts
啦啦啦啦啦zzzz3 小时前
设计模式:桥接模式和组合模式
c++·设计模式·组合模式·桥接模式
-森屿安年-3 小时前
647. 回文子串
c++·算法·动态规划