【嵌入式学习】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;
}

结果:

相关推荐
我变成萤火虫5 小时前
河南萌新联赛2026第(四)场:南阳理工学院
数据结构·c++·算法·贪心算法·stl·动态规划
平常心的技术小牛7 小时前
Qt-快速上手-QMenuBar
开发语言·qt
Shaoxi Zhang9 小时前
JAVA学习笔记035——对象和JSON格式
java·笔记·学习
陈皮波比茶9 小时前
Redis学习
数据库·redis·学习
余额瞒着我当琳9 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
ShineWinsu10 小时前
对于C++:布隆过滤器(bloomfilter)的详细解析
c++·面试·位图·位运算·布隆过滤器·海量数据·比特位
minglie111 小时前
espidf的esp32版的webClient
学习
学运维的Kysan11 小时前
暑假运维学习打卡第二十五天8.16
学习
键盘飞行员12 小时前
Flutter App 全套实战学习计划:从零搭建到 APK 部署
学习·flutter
会编程的吕洞宾14 小时前
DeepAgents In Action学习(Second)
android·java·学习