2024.1.25 C++&QT 作业

思维导图

练习题

自己封装一个矩形类(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{
private:int width;
private:int height;
public:void init(int w,int h)
    {
        width=w;
        height=h;
        show();
    }
public:void set_w(int w)
    {
        width=w;
    }

public:void set_h(int h)
    {
        height=h;
    }
public:void show()
    {
        cout << "周长为:" << 2*(height+width) << endl;
        cout << "面积为:" << height*width << endl;
    }
};
int main()
{
    Rect rect;
    int width,height;
    cout << "请输入宽:";
    cin>>width;
    cout << "请输入高:";
    cin>>height;

    rect.init(width,height);
    cout << "更改宽:";
    cin>>width;
    rect.set_w(width);
    cout << "更改高:";
    cin>>height;
    rect.set_h(height);
    rect.init(width,height);
    return 0;
}
相关推荐
浅念-4 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
Kiling_07045 小时前
Java集合进阶:Set与Collections详解
算法·哈希算法
智者知已应修善业5 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
洛水水5 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI6 小时前
聚类算法详解
算法·数据挖掘·聚类
刀法如飞6 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员
Byron Loong8 小时前
【c++】为什么有了dll和.h,还需要包含lib
java·开发语言·c++
Dlrb12118 小时前
C语言-指针数组与数组指针
c语言·数据结构·算法·指针·数组指针·指针数组·二级指针
WL_Aurora8 小时前
Python 算法基础篇之集合
python·算法
坚果派·白晓明8 小时前
【鸿蒙PC三方库移植适配框架解读系列】第一篇:Lycium C/C++ 三方库适配 — 概述与环境配置
c语言·开发语言·c++·harmonyos·开源鸿蒙·三方库·c/c++三方库