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;
}
相关推荐
基德爆肝c语言1 小时前
Qt—信号和槽
开发语言·qt
yongui478341 小时前
NSGA-II求解多目标柔性作业车间调度算法(含甘特图绘制)
算法·甘特图
ximu_polaris1 小时前
设计模式(C++)-行为型模式-观察者模式
c++·观察者模式·设计模式
故事和你912 小时前
洛谷-算法2-1-前缀和、差分与离散化1
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
知识浅谈9 小时前
DeepSeek V4 和 GPT-5.5 在同一天发布了??我也很懵,但对比完我悟了
算法
DeepModel9 小时前
通俗易懂讲透 Q-Learning:从零学会强化学习核心算法
人工智能·学习·算法·机器学习
田梓燊9 小时前
力扣:19.删除链表的倒数第 N 个结点
算法·leetcode·链表
小短腿的代码世界10 小时前
Qt Concurrent 深度解析:并发编程范式与源码级实现原理
qt·系统架构·lucene
handler0110 小时前
从零实现自动化构建:Linux Makefile 完全指南
linux·c++·笔记·学习·自动化
简简单单做算法11 小时前
基于GA遗传优化双BP神经网络的时间序列预测算法matlab仿真
神经网络·算法·matlab·时间序列预测·双bp神经网络