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;
}
相关推荐
sulikey4 分钟前
C++ 四十年:一段跨越时代的语言旅程
c++·c++40周年
m***D2868 分钟前
JavaScript在Node.js中的内存管理
开发语言·javascript·node.js
我叫张小白。8 分钟前
JavaScript现代语法梳理:ES6+核心特性详解
开发语言·javascript·typescript·es6
SoleMotive.23 分钟前
redis实现漏桶算法--https://blog.csdn.net/m0_74908430/article/details/155076710
redis·算法·junit
-森屿安年-30 分钟前
LeetCode 283. 移动零
开发语言·c++·算法·leetcode
寻找华年的锦瑟34 分钟前
Qt-FFmpeg案例(0基础,包含环境配置)
开发语言·qt·ffmpeg
北京地铁1号线38 分钟前
数据结构:堆
java·数据结构·算法
tanxiaomi1 小时前
Spring、Spring MVC 和 Spring Boot ,mybatis 相关面试题
java·开发语言·mybatis
浮尘笔记1 小时前
Go并发编程核心:Mutex和RWMutex的用法
开发语言·后端·golang
散峰而望1 小时前
C++数组(一)(算法竞赛)
c语言·开发语言·c++·算法·github