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;
}
相关推荐
沐知全栈开发2 小时前
HTML5 浏览器支持
开发语言
wasp5202 小时前
AgentScope Java 核心架构深度解析
java·开发语言·人工智能·架构·agentscope
WHOVENLY2 小时前
【javaScript】- 笔试题合集(长期更新,建议收藏,目前已更新至31题)
开发语言·前端·javascript
慌糖2 小时前
流-为序列化解释
开发语言
LXS_3572 小时前
Day 18 C++提高 之 STL常用容器(string、vector、deque)
开发语言·c++·笔记·学习方法·改行学it
源代码•宸3 小时前
Leetcode—620. 有趣的电影&&Q3. 有趣的电影【简单】
数据库·后端·mysql·算法·leetcode·职场和发展
王琦03183 小时前
Python 函数详解
开发语言·python
胡伯来了3 小时前
13. Python打包工具- setuptools
开发语言·python
2301_800256113 小时前
地理空间数据库中的CPU 和 I/O 开销
数据库·算法·oracle
小鸡吃米…3 小时前
Python 中的多层继承
开发语言·python