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;
}
相关推荐
顶点多余6 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
罗西的思考7 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
「QT(C++)开发工程师」7 小时前
C++ auto 用法详解
开发语言·c++
OPEN-F8 小时前
C++STL教程:容器适配器与实用工具
开发语言·c++
OPEN-F8 小时前
C++模板教程:变参模板、折叠表达式与SFINAE
java·开发语言·c++
hahaha60168 小时前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
有点。8 小时前
C++二叉搜索树进阶
开发语言·c++
HugoStudio_SWAN8 小时前
【擦除重绘】C++ 控制台动画:弹跳 Logo DVD 屏保效果
开发语言·c++·学习·程序人生
多弗朗皮卡丘9 小时前
算法详解4:买卖股票的最佳时机系列(上)
算法
kyle~10 小时前
C++_STL---迭代器失效
开发语言·c++