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

结果:

相关推荐
郝学胜-神的一滴9 分钟前
Linux下的阻塞与非阻塞模式详解
linux·服务器·开发语言·c++·程序人生·软件工程
woshigaowei514613 分钟前
VS(QT)调用Matlab函数的方法
qt·matlab·vs
一只小风华~24 分钟前
学习笔记:Vue Router 中的链接匹配机制与样式控制
前端·javascript·vue.js·笔记·学习·ecmascript
月临水2 小时前
Redis 学习笔记(二)
redis·笔记·学习
Nan_Shu_6142 小时前
学习SpringBoot
java·spring boot·后端·学习·spring
●VON2 小时前
重生之我在大学自学鸿蒙开发第二天-《MVVM模式》
学习·华为·harmonyos
Samsong2 小时前
《C++ Primer Plus》读书笔记 第二章 开始学习C++
c++·后端
你真的可爱呀3 小时前
uniapp学习【vue3在uniapp中语法,使用element,使用uView UI】
学习·uni-app
Rubisco..3 小时前
牛客周赛 Round 111
数据结构·c++·算法
2501_916766543 小时前
【Git学习】初识git:简单介绍及安装流程
git·学习