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

相关推荐
小七在进步41 分钟前
string的使用(1)
c++·visual studio
SomeOtherTime43 分钟前
Rust所有权(AI回答)
开发语言·后端·rust
Easyhood1 小时前
Android 开发入门实践(十四):Compose 副作用入门与四个常用 API
android·开发语言·kotlin·android jetpack·compose
白杨尚青2 小时前
C++进阶篇(六):map 和 set 的模拟实现——用红黑树封装 mymap/myset(迭代器 + operator[])
开发语言·c++·迭代器·stl·set·map·红黑树
kloc4 小时前
聊天室项目入口网关子服务的实现
linux·c++
饮水桶_T5 小时前
JAVA IO流笔记
开发语言·数据结构·算法
csdn_aspnet8 小时前
C++ 3个有序点的方向(Orientation of 3 ordered points)
c++·算法
Felven8 小时前
C. Brr Brrr Patapim
c语言·开发语言
小七在进步9 小时前
string的使用(2)
c++
路溪非溪9 小时前
python标准库使用总结
开发语言·python