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 分钟前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
时间的情敌17 分钟前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
Amumu1213833 分钟前
TypeScript学习: 基本类型(一)
开发语言·前端·typescript
sul.i1 小时前
浅析 · 分治
c++
让我们一起加油好吗1 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
敲上瘾2 小时前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
船神丿男人2 小时前
C++ STL 入门:用 std::string 读配置,四个容易漏掉的边界
开发语言·c++
让我们一起加油好吗2 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
爱和冰阔落3 小时前
【Linux网络】GET 和 POST 真的只是参数位置不同吗?从 HTTP 请求方法、抓包分析到长连接与路由实战
linux·c++·http·网络编程·get与post
30955666614 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口