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

相关推荐
伊灵eLing25 分钟前
GoLang 语言基础
开发语言·后端·golang
两年半的个人练习生^_^25 分钟前
JMM 进阶:彻底理解 synchronized 实现原理
java·开发语言
小白不白11127 分钟前
Invoke的用法
开发语言·人工智能·数码相机·计算机视觉·c#
techdashen30 分钟前
What is maintenance, anyway?
开发语言·后端·rust
万法若空33 分钟前
C/C++基本类型表示范围
c语言·开发语言·c++
yijianace36 分钟前
Python爬虫实战:BooksToScrape 多线程爬取与图片下载
开发语言·爬虫·python
凡人叶枫43 分钟前
Effective C++ 条款15:在资源管理类中提供对原始资源的访问
linux·开发语言·c++·stm32·单片机
swordbob43 分钟前
Spring Boot 2.0 改 CGLIB 后,接口实现是否有影响
java·开发语言·spring
郝学胜-神的一滴44 分钟前
中级OpenGL教程 009:用环境光告别模型死黑
前端·c++·unity·godot·图形渲染·opengl·unreal
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第106题】【并发篇】第6题:synchronized 锁的锁对象可以是什么?
java·开发语言·面试