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;
}
相关推荐
Mr_WangAndy3 分钟前
C++设计模式_行为型模式_策略模式Strategy
c++·设计模式·策略模式·依赖倒置原则
LoveXming28 分钟前
Chapter11—适配器模式
c++·设计模式·适配器模式·开闭原则
Benny_Tang42 分钟前
题解:P7989 [USACO21DEC] Bracelet Crossings G
c++·算法
有时间要学习1 小时前
Qt——窗口
开发语言·qt
小白杨树树1 小时前
【C++】力扣hot100错误总结
c++·leetcode·c#
ajassi20001 小时前
开源 C++ QT QML 开发(二十三)程序发布
c++·qt·mfc
消失的旧时光-19432 小时前
@JvmStatic 的作用
java·开发语言·kotlin
skywalk81632 小时前
基于频域的数字盲水印blind-watermark
linux·开发语言·python
Tiger_shl2 小时前
三大并发集合ConcurrentDictionary、ConcurrentBag、ConcurrentQueue
开发语言·c#
火锅机器2 小时前
java 8 lambda表达式对list进行分组
java·开发语言·list