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;
}
相关推荐
Java菜鸟、8 分钟前
设计模式(代理设计模式)
java·开发语言·设计模式
Once_day10 分钟前
代码训练LeetCode(23)随机访问元素
算法·leetcode
小河豚oO26 分钟前
LeetCode 热题 100 - 哈希 - 128
算法·leetcode·哈希算法
客卿12327 分钟前
力扣100题之128. 最长连续序列
算法·leetcode·哈希算法
T1an-127 分钟前
【力扣链表篇】206.反转链表
算法·leetcode·链表
景天科技苑1 小时前
【Rust宏编程】Rust有关宏编程底层原理解析与应用实战
开发语言·后端·rust·rust宏·宏编程·rust宏编程
yorushika_1 小时前
python打卡训练营打卡记录day45
开发语言·python·深度学习·tensorboard
封奚泽优1 小时前
使用Python进行函数作画
开发语言·python
aningxiaoxixi1 小时前
JAVA之 Lambda
java·开发语言
xphjj2 小时前
树形数据模糊搜索
前端·javascript·算法