【嵌入式学习】C++&QT-Day2-C++基础

笔记

见我的博客:https://lingjun.life/wiki/EmbeddedNote/19Cpp

作业

自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),

定义公有成员函数:

初始化函数:void init(int w, int h)

更改宽度的函数:set_w(int w)

更改高度的函数:set_h(int h)

输出该矩形的周长和面积函数:void show()

cpp 复制代码
#include <iostream>

//自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),
//定义公有成员函数:
//初始化函数:void init(int w, int h)
//更改宽度的函数:set_w(int w)
//更改高度的函数:set_h(int h)
//输出该矩形的周长和面积函数:void show()
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 << "周长=" << 2*width+2*height <<endl;
        cout << "面积=" << width*height <<endl;
    }
};

int main()
{
    Rect rct1;
    rct1.init(20,25);
    rct1.show();
    rct1.set_h(12);
    rct1.set_w(15);
    rct1.show();
    return 0;
}

结果:

相关推荐
xlp666hub11 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网12 小时前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub14 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
不想写代码的星星15 小时前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub1 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
不想写代码的星星2 天前
C++继承、组合、聚合:选错了是屎山,选对了是神器
c++
不想写代码的星星3 天前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
Felix_One3 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
樱木Plus5 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit6 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip