13030 - Box   

Description

Design a class named Box whose dimensions are integers and private to the class. The dimensions are labelled: length, width, and height.

 

Constructors:

  1. The default constructor of the class should initialize length, width, and height to 0.
  2. The parameterized constructor Box(int l, int w, int h) should initialize Box's length, width and height to l, w and h.
  3. The copy constructor Box(Box& b) should set length, width and height to b's length, width and height, respectively.

 

Methods:

  1. void setLength(int l) – Should set box's length to l
  2. void setWidth(int w) - Should set box's width to w
  3. void setHeight(int h) - Should set box's height to h
  4. int getLength() - Return box's length
  5. int getWidth() - Return box's width
  6. int getHeight() - Return box's height
  7. long long CalculateVolume() - Return the volume of the box (Hint: cout << volume << endl; )

 

ref: https://www.hackerrank.com/challenges/box-it/proble

 

main.cpp

#include <iostream>
#include <string>
 
using namespace std;
 
class Box
{
public:
    Box() : length(0), width(0), height(0) {}
    /* Todo */
 
    void setLength(int _l) { length = _l; }
    /* Todo */
 
    int getLength() { return length; }
    /* Todo */
 
    /* Todo */
    // 1. calculateVolume() 2. calculateSurfaceArea()
 
private:
    int lengthwidthheight;
};
 
// Don't touch main()
int main()
{
    int testcase = 0;
    cin >> testcase;
 
    if (testcase == 1)
    {
        Box box = Box();
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
        box.setLength(300);
        box.setWidth(400);
        box.setHeight(500);
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
    }
    else if (testcase == 2)
    {
        Box box = Box(171734);
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
        box.setLength(101);
        box.setWidth(50);
        box.setHeight(34);
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight() << endl;
    }
    else if (testcase == 3)
    {
        Box box = Box(1001717);
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
        cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
    }
    else if (testcase == 4)
    {
        Box box = Box();
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
        cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
        box.setLength(44);
        box.setWidth(200);
        box.setHeight(1);
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
        cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
    }
    else if (testcase == 5)
    {
        Box box = Box(100002000030000);
        cout << "Length: " << box.getLength() << ", Width: " << box.getWidth() << ", Height: " << box.getHeight();
        cout << ", Surface Area: " << box.calculateSurfaceArea() << ", Volume: " << box.calculateVolume() << endl;
    }
 
    return 0;
}

Input

No need to handle input. (Input would be number 1~5 to represent the index of test-case)

Output

Depend on test case show in main.cpp.

Sample Input  Download

Sample Output  Download

Tags




Discuss