3.26C++

定义一个矩形类(Rectangle),包含私有成员:长(length)、宽(width),

定义成员函数:

设置长度:void set_l(int l)

设置宽度:void set_w(int w)

获取长度:int get_l();

获取宽度:int get_w();

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

#include <iostream>
using namespace std;
class Rectangle {
private:
    int length;
    int width;
public:
    void set_l(int l) {
        length = l;
    }
    void set_w(int w) {
        width = w;
    }
    int get_l(){
        return length;
    }
    int get_w(){
        return width;
    }
    void show(){
        int c = 2 * (length + width);
        int area = length * width;
        cout<<"Rectangle 周长 = "<<c<<endl;
        cout<<"Rectangle 面积 = "<<area<<endl;
    }
};
int main()
{
    Rectangle rect;
    rect.set_l(10);
    rect.set_w(4);
    cout<<"长 = "<<rect.get_l()<<endl;
    cout<<"宽 = "<<rect.get_w()<<endl;
    rect.show();
    return 0;
}

相关推荐

  1. ABC336(A-C)

    2024-03-26 23:58:03       67 阅读
  2. C++】316 去除重复字母

    2024-03-26 23:58:03       29 阅读
  3. CS32 C++ programming

    2024-03-26 23:58:03       32 阅读
  4. STM<span style='color:red;'>32</span> I2<span style='color:red;'>C</span>

    STM32 I2C

    2024-03-26 23:58:03      49 阅读
  5. C语言习题~day32

    2024-03-26 23:58:03       33 阅读

最近更新

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

    2024-03-26 23:58:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 23:58:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 23:58:03       82 阅读
  4. Python语言-面向对象

    2024-03-26 23:58:03       91 阅读

热门阅读

  1. 2024.3.25力扣(1200-1400)刷题记录

    2024-03-26 23:58:03       34 阅读
  2. 力扣438---找到字符串中所有字母异位词

    2024-03-26 23:58:03       40 阅读
  3. 使用Spring ORM和MyBatis简化数据库访问

    2024-03-26 23:58:03       37 阅读
  4. 13、Spring CLI中的特殊命令

    2024-03-26 23:58:03       44 阅读
  5. LeetCode1047:删除字符串中的所有相邻重复项

    2024-03-26 23:58:03       44 阅读
  6. Python 命名规则

    2024-03-26 23:58:03       41 阅读
  7. __init__.py 的作用

    2024-03-26 23:58:03       43 阅读
  8. vue3之动态路由

    2024-03-26 23:58:03       35 阅读
  9. Python networkx库中,G.add_edge方法的原理和使用方法

    2024-03-26 23:58:03       37 阅读