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

【问题描述】

设计一个类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

#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;
}