day2-C++

1>自己封装一个矩形类(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 width;
    int height;
public:
    void init(int width,int height)
    {
        Rect::width = width;
        Rect::height = height;
    }
    void set_w(int width)
    {
        this->width = width;
    }
    void set_h(int height)
    {
        this->height = height;
    }
    void show()
    {
        cout << "周长:" << 2*(width+height) << " " << "高度:" << width*height << endl;
    }
};

int main()
{
    Rect r;
    r.init(5,5);
    r.set_w(4);
    r.set_h(4);
    r.show();
    return 0;
}

结果:

2>思维导图

相关推荐

  1. c++ day2

    2024-03-14 19:34:03       57 阅读
  2. <span style='color:red;'>c</span>++<span style='color:red;'>day</span><span style='color:red;'>2</span>

    c++day2

    2024-03-14 19:34:03      46 阅读
  3. <span style='color:red;'>day</span><span style='color:red;'>2</span>-<span style='color:red;'>C</span>++

    day2-C++

    2024-03-14 19:34:03      33 阅读
  4. <span style='color:red;'>C</span>++ <span style='color:red;'>day</span><span style='color:red;'>2</span>

    C++ day2

    2024-03-14 19:34:03      44 阅读
  5. <span style='color:red;'>c</span>++ <span style='color:red;'>day</span><span style='color:red;'>2</span>

    c++ day2

    2024-03-14 19:34:03      32 阅读
  6. <span style='color:red;'>C</span>++ <span style='color:red;'>day</span><span style='color:red;'>2</span>

    C++ day2

    2024-03-14 19:34:03      39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-14 19:34:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-14 19:34:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-14 19:34:03       87 阅读
  4. Python语言-面向对象

    2024-03-14 19:34:03       96 阅读

热门阅读

  1. 当代计算机语言占比分析

    2024-03-14 19:34:03       51 阅读
  2. 文件系统事件监听

    2024-03-14 19:34:03       43 阅读
  3. 【OpenGL经验谈01】Vertex 规范最佳实践

    2024-03-14 19:34:03       42 阅读
  4. SpringCloud中Gateway提示OPTIONS请求跨域问题

    2024-03-14 19:34:03       41 阅读
  5. 如何详细自学python?

    2024-03-14 19:34:03       41 阅读
  6. 自动化运维工具Ansible之playbooks剧本

    2024-03-14 19:34:03       49 阅读
  7. Android 卫星通信计算方位角,仰角,极化角

    2024-03-14 19:34:03       36 阅读
  8. el-table 合集行合并

    2024-03-14 19:34:03       39 阅读
  9. 数据库基础知识

    2024-03-14 19:34:03       39 阅读
  10. WIFI攻击方法总结

    2024-03-14 19:34:03       48 阅读