qq_62227344 2022-09-15 19:07 采纳率: 100%
浏览 36
已结题

关于#析构函数#的问题,如何解决?

【问题描述】

设计一个类Box,要求如下所述。

(1)包含构造函数和析构函数。

默认10106的Box对象。

(2)包含成员函数能计算Box的体积。

【输入形式】输入BOX1的长宽高
【输出形式】输出BOX1和BOX2的体积、构造和析构函数结果;

【样例输入】

5 3 2
【样例输出】

构造Box 5 3 2
构造Box 10 10 6
The volume of box1 is 30
The volume of box2 is 600
析构Box 10 10 6
析构Box 5 3 2

  • 写回答

2条回答 默认 最新

  • _GX_ 2022-09-15 21:08
    关注
    #include <iostream>
    
    class Box {
    public:
        Box(int length = 10, int width = 10, int height = 6)
            : _length(length), _width(width), _height(height) {
            std::cout << "构造Box " << _length << " " << _width << " " << _height << '\n';
        }
    
        ~Box() {
            std::cout << "析构Box " << _length << " " << _width << " " << _height << '\n';
        }
    
        int volume() const { return _length * _width * _height; }
    
    private:
        int _length;
        int _width;
        int _height;
    };
    
    int main() {
        int length, width, height;
        std::cin >> length >> width >> height;
        Box box1(length, width, height);
        Box box2;
        std::cout << "The volume of box1 is" << box1.volume() << '\n';
        std::cout << "The volume of box2 is" << box2.volume() << '\n';
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月23日
  • 已采纳回答 9月15日
  • 创建了问题 9月15日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?