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

相关推荐
熬夜敲代码的猫10 小时前
教你如何使用set和map
c++·算法
踩着两条虫18 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB18 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
即使再小的船也能远航18 小时前
【Python】安装
开发语言·python
Irissgwe18 小时前
类与对象(三)
开发语言·c++·类和对象·友元
️是7818 小时前
信息奥赛一本通—编程启蒙(3395:练68.3 车牌问题)
数据结构·c++·算法
计算机安禾19 小时前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
雪度娃娃19 小时前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++
我星期八休息19 小时前
Linux系统编程—基础IO
linux·运维·服务器·c语言·c++·人工智能·算法
萌新小码农‍19 小时前
python装饰器
开发语言·前端·python