Hackerrank Box It! Solution

Hackerrank Box It! Solution

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

The default constructor of the class should initialize , , and  to .

The parameterized constructor Box(int length, int breadth, int height) should initialize Box's  and  to length, breadth and height.

The copy constructor BoxBox ) should set  and  to 's  and , respectively.

Apart from the above, the class should have  functions:

  • int getLength()  - Return box's length
  • int getBreadth() - Return box's breadth
  • int getHeight()  - Return box's height
  • long long CalculateVolume() - Return the volume of the box

Overload the operator  for the class Box. Box   Box   if:

  1. <
  2. <  and ==
  3. <  and == and ==

Overload operator  for the class Box().
If  is an object of class Box:

should print ,  and  on a single line separated by spaces.

Constraints


Two boxes being compared using the  operator will not have all three dimensions equal.

Solution in cpp

Approach 1.

#include <tuple>

class Box {
    int l = 0, b = 0, h = 0;

public:
    Box() {
        BoxesCreated += 1;
    }
    Box(int il, int ib, int ih)
    : l{il}, b{ib}, h{ih}
    {
        BoxesCreated += 1;
    }
    Box(const Box& other) {
        l = other.l;
        b = other.b;
        h = other.h;
        BoxesCreated += 1;
    }
    
    ~Box() {
        BoxesDestroyed += 1;
    }

    int getLength() const { return l; }
    int getBreadth() const { return b; }
    int getHeight() const { return h; }
    long long CalculateVolume() const {
        return (long long)(l) * b * h;
    }
};

bool operator <(const Box& a, const Box& b) {
    int al = a.getLength(), ab = a.getBreadth(), ah = a.getHeight();
    int bl = b.getLength(), bb = b.getBreadth(), bh = b.getHeight();
    return std::tie(al, ab, ah) < std::tie(bl, bb, bh);
}

ostream& operator <<(ostream& out, Box b) {
    return out << b.getLength() << ' ' << b.getBreadth() << ' ' << b.getHeight();
}

Approach 2.


//Implement the class Box  
class Box
{
    private:
    int l, b, h; //l,b,h are integers representing the dimensions of the box

    public:
// The class should have the following functions : 
// Constructors: 
    Box():l(0),b(0),h(0) 
    {
        ++BoxesCreated;
    }
    Box(int x, int y, int z):l(x),b(y),h(z) 
    {
        ++BoxesCreated;
    }
    Box(const Box &M)
    {
        l = M.l;
        b = M.b;
        h = M.h;
        ++BoxesCreated;
    }

    // Destructor
    ~Box()
    {
        ++BoxesDestroyed;
    }

    inline int getLength(){ return l;}// Return box's length
    inline int getBreadth (){ return b;} // Return box's breadth
    inline int getHeight (){ return h;}  //Return box's height
    long long CalculateVolume() // Return the volume of the box
    {
        long long k = l;
        k = k*h;
        k = k*b;
        return (k);
    }

//Overload operator < as specified
  bool operator<(Box &X)
      { 
      if((l < X.getLength()) || 
         ((b < X.getBreadth()) && (l == X.getLength())) ||
         ((h == X.getHeight()) && (b == X.getBreadth()) && (l == X.getLength())))
          return true;
      else
          return false;
  }

    //Overload operator << as specified
    friend ostream& operator<<(ostream& out, const Box B)
    {
        out << B.l << " " << B.b << " " << B.h;
        return out;
    }
};

Approach 3.

class Box{
    private:
        int l, b, h;
    public:
        Box() {
            this->l = 0;
            this->b = 0; 
            this->h = 0;
            BoxesCreated++;
        }
        Box(int a, int b, int c) {
            this->l = a;
            this->b = b;
            this->h = c;
            BoxesCreated++;
        }
        Box(Box &box) {
            this->l = box.getLength();
            this->b = box.getBreadth();
            this->h = box.getHeight();
            BoxesCreated++;
        }
        ~Box() {
            BoxesDestroyed++;
        }
        int getLength() {
            return this->l;
        }
        int getBreadth() {
            return this->b;
        }
        int getHeight() {
            return this->h;
        }
        long long CalculateVolume() {
            long long volume = long(this->l)*long(this->b)*long(this->h);
            return volume;
        }
        bool operator<(Box &B) {
            if (this->l < B.getLength()) return true;
            else if ((this->b < B.getBreadth())&&(this->l == B.getLength())) return true;
            else if ((this->h < B.getHeight())&&(this->b == B.getBreadth())&&(this->l == B.getLength())) return true;
            else return false;
        }
        
};

ostream& operator<<(ostream& out, Box B) {
            out << B.getLength() << ' ' << B.getBreadth() << ' ' << B.getHeight();
            return out;
        }



Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe