C++day2——引用、结构体、类

思维导图:

2、自己封装一个矩形类(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
{

protected:

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 << "周长 = " << 2 * ( width + height ) << endl;
        cout << "面积 = " << width * height << endl;
    }

};

int main()
{
    Rect rec;
    rec.init(3,4);
    rec.show();
    rec.set_h(6);
    rec.show();
    rec.set_w(5);
    rec.show();
    return 0;
}
相关推荐
QT 小鲜肉21 小时前
【C++基础与提高】第二章:C++数据类型系统——构建程序的基础砖石
开发语言·c++·笔记
lsx2024061 天前
HTML5 新元素
开发语言
先知后行。1 天前
C/C++八股文
java·开发语言
程序员buddha1 天前
C语言数组详解
c语言·开发语言·算法
寻找华年的锦瑟1 天前
Qt-视频播放器
开发语言·qt
又是忙碌的一天1 天前
Java IO流
java·开发语言
fish_study_csdn1 天前
Python内存管理机制
开发语言·python·c python
卡提西亚1 天前
C++笔记-25-函数模板
c++·笔记·算法
ghie90901 天前
MATLAB/Simulink水箱水位控制系统实现
开发语言·算法·matlab
cs麦子1 天前
C语言--详解--指针--上
c语言·开发语言