C++ program to create boxes and calculate volume and check using less than operator


Suppose we shall have to define a box class with few conditions. These are as follows −

  • There are three attributes l, b and h for length, breadth and height respectively, (these are private variables)

  • Define one non-parameterized constructor to set l, b, h to 0, and one parameterized constructor to set values initially.

  • Define getter methods for each attribute

  • Define a function calculateVolume() get the volume of the box

  • Overload less than operator (<) to check current box is less than another box or not.

  • Create a variable that can count number of boxes that are created.

So, if we take input for three boxes (0, 0, 0) (5, 8, 3), (6, 3, 8) and display each box data, and check whether third box is smaller than second or not, and find volume of smaller box, and also print how many boxes are their by the count variable.

Then the output will be

Box 1: (length = 0, breadth = 0, width = 0)
Box 2: (length = 5, breadth = 8, width = 3)
Box 3: (length = 6, breadth = 3, width = 8)
Box 3 is smaller, its volume: 120
There are total 3 box(es)

To solve this, we will follow these steps −

  • To calculate volume we shall have to return l*b*h

  • to overload less-than (<) operator we shall have to check

  • if l of current object is not same as l of given another object, then

    • return true if l of current object is smaller than l of another object

  • otherwise when b of current object is not same as b of given another object, then

    • return true if b of current object is smaller than b of another object

  • otherwise when h of current object is not same as h of given another object, then

    • return true if h of current object is smaller than h of another object

Example

Let us see the following implementation to get better understanding −

#include <iostream>
using namespace std;
class Box {
    int l, b, h;
public:
    static int count;
    Box() : l(0), b(0), h(0) { count++; }
    Box(int length, int breadth, int height) : l(length), b(breadth), h(height) { count++; }
    int getLength() const {return l;}
    int getBreadth() const {return b;}
    int getHeight() const {return h;}
    long long CalculateVolume() const {
        return 1LL * l * b * h;
    }
    bool operator<(const Box& another) const {
        if (l != another.l) {
            return l < another.l;
        }
        if (b != another.b) {
            return b < another.b;
        }
        return h < another.h;
    }
};
int Box::count = 0;
int main(){
    Box b1;
    Box b2(5,8,3);
    Box b3(6,3,8);
    printf("Box 1: (length = %d, breadth = %d, width = %d)\n",b1.getLength(), b1.getBreadth(), b1.getHeight());
    printf("Box 2: (length = %d, breadth = %d, width = %d)\n",b2.getLength(), b2.getBreadth(), b2.getHeight());
    printf("Box 3: (length = %d, breadth = %d, width = %d)\n",b3.getLength(), b3.getBreadth(), b3.getHeight());
    if(b3 < b2){
        cout << "Box 3 is smaller, its volume: " << b3.CalculateVolume() << endl;
    }else{
        cout << "Box 3 is smaller, its volume: " << b2.CalculateVolume() << endl;
    }
    cout << "There are total " << Box::count << " box(es)";
}

Input

b1; b2(5,8,3); b3(6,3,8);

Output

Box 1: (length = 0, breadth = 0, width = 0)
Box 2: (length = 5, breadth = 8, width = 3)
Box 3: (length = 6, breadth = 3, width = 8)
Box 3 is smaller, its volume: 120
There are total 3 box(es)

Updated on: 07-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements