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;
}
相关推荐
全栈凯哥15 分钟前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
全栈凯哥17 分钟前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
SuperCandyXu21 分钟前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
Humbunklung38 分钟前
机器学习算法分类
算法·机器学习·分类
Ai多利1 小时前
深度学习登上Nature子刊!特征选择创新思路
人工智能·算法·计算机视觉·多模态·特征选择
愈努力俞幸运1 小时前
c++ 头文件
开发语言·c++
~山有木兮1 小时前
C++设计模式 - 单例模式
c++·单例模式·设计模式
十五年专注C++开发2 小时前
CMake基础:gcc/g++编译选项详解
开发语言·c++·gcc·g++
Q8137574602 小时前
中阳视角下的资产配置趋势分析与算法支持
算法
yvestine2 小时前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示