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;
}
相关推荐
@––––––1 分钟前
力扣hot100—系列2-多维动态规划
算法·leetcode·动态规划
lxl13073 分钟前
C++算法(1)双指针
开发语言·c++
xsyaaaan13 分钟前
代码随想录Day31动态规划:1049最后一块石头的重量II_494目标和_474一和零
算法·动态规划
淀粉肠kk14 分钟前
C++11列表初始化:{}的革命性进化
c++
无小道20 分钟前
Qt-qrc机制简单介绍
开发语言·qt
zhooyu27 分钟前
C++和OpenGL手搓3D游戏编程(20160207进展和效果)
开发语言·c++·游戏·3d·opengl
HAPPY酷30 分钟前
C++ 和 Python 的“容器”对决:从万金油到核武器
开发语言·c++·python
Jay Kay1 小时前
GVPO:Group Variance Policy Optimization
人工智能·算法·机器学习
Epiphany.5561 小时前
蓝桥杯备赛题目-----爆破
算法·职场和发展·蓝桥杯
YuTaoShao1 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法三)DP 空间优化
算法·leetcode·职场和发展