C++ day2 练习

思维导图

自己封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height),

定义公有成员函数:

初始化函数:void init(int w, int h)

更改宽度的函数:set_w(int w)

更改高度的函数:set_h(int h)

输出该矩形的周长和面积函数:void show()

复制代码
#include <iostream>

using namespace std;

class Rect {
private:
    int wide;
    int hight;
    int area;

public:
    void init(int w, int h){    //数据初始化函数

        wide = w;
        hight = h;
        area = wide * hight;
    }

    void set_w(int w){    //更改宽度的函数

        wide = w;
    }

    void set_h(int h){

        hight = h;
    }

    void show_area(){

        cout << "area is :" << wide * hight << endl;
    }

};

int main()
{
    int w,h;
    Rect r1;
    cout << "请依次输入长和宽 " << endl;
    cin >> w;
    cin >> h;
    r1.init(w,h);
    cout << "长方形的面积为:" << endl;
    r1.show_area();

    return 0;
}
相关推荐
海琴烟Sunshine6 分钟前
leetcode 383. 赎金信 python
python·算法·leetcode
VBA63371 小时前
VBA即用型代码手册:利用函数保存为PDF文件UseFunctionSaveAsPDF
开发语言
say_fall1 小时前
C语言编程实战:每日刷题 - day2
c语言·开发语言·学习
上去我就QWER2 小时前
Qt快捷键“魔法师”:QKeySequence
开发语言·c++·qt
Pluto_CSND4 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
将编程培养成爱好5 小时前
C++ 设计模式《外卖骑手状态系统》
c++·ui·设计模式·状态模式
猿太极5 小时前
设计模式学习(3)-行为型模式
c++·设计模式
惊讶的猫6 小时前
LSTM论文解读
开发语言·python
cynicme6 小时前
力扣3228——将 1 移动到末尾的最大操作次数
算法·leetcode
熬了夜的程序员6 小时前
【LeetCode】109. 有序链表转换二叉搜索树
数据结构·算法·leetcode·链表·职场和发展·深度优先