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

相关推荐
哥不想学算法7 小时前
【C++】字符串字面量拼接
开发语言·c++
gugucoding9 小时前
21. 【C语言】打包不同类型:结构体
c语言·开发语言
Brookty9 小时前
【JavaEE】线程安全(一).4:写块串行保安全、CAS
java·开发语言·java-ee·多线程·线程安全
F202269748610 小时前
西门子 PLC 与 C# 通信
开发语言·c#
gugucoding10 小时前
31. 【C语言】堆栈与队列的实现
c语言·开发语言·数据结构·链表
万联WANFLOW11 小时前
多分支企业组网,IP 网段到底该怎么规划
开发语言·php
阿里嘎多学长11 小时前
2026-07-07 GitHub 热点项目精选
开发语言·程序员·github·代码托管
cookies_s_s13 小时前
C++ 字符串动态创建对象 -- 工厂模式、自动注册、模板递归动态调用
服务器·开发语言·c++
王老师青少年编程13 小时前
2026年6月GESP真题及题解(C++七级):消消乐
数据结构·c++·算法·真题·gesp·2026年6月