Constructor Overloads

Lab C++ Intermediate 20 points

Write class Box with constructors Box() (1×1×1), Box(int side) and Box(int l, int w, int h), plus volume(). Read lines with 0 (-), 1 or 3 numbers and print each volume.

Input

n, then n lines.

Output

n lines.

Example

Input

3
-
4
2 3 5

Output

1
64
30
Sample input available Sample output available
Editor
C++

Hints

A constructor can delegate to another: Box() : Box(1) {}.

Solution explanation

Delegating constructors keep the real initialisation in one place.

Show solution
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

class Box {
public:
    Box() : Box(1) {}
    explicit Box(int side) : Box(side, side, side) {}
    Box(int l, int w, int h) : l_(l), w_(w), h_(h) {}
    long long volume() const { return 1LL * l_ * w_ * h_; }

private:
    int l_, w_, h_;
};

int main() {
    int n;
    cin >> n;
    string line;
    getline(cin, line);
    for (int i = 0; i < n; i++) {
        getline(cin, line);
        istringstream in(line);
        vector<int> nums;
        int x;
        while (in >> x) nums.push_back(x);
        Box b = nums.empty() ? Box() : nums.size() == 1 ? Box(nums[0]) : Box(nums[0], nums[1], nums[2]);
        cout << b.volume() << "\n";
    }
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD