Home  >  Article  >  Backend Development  >  C++ program to create box and calculate volume and check using less than operator

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

PHPz
PHPzforward
2023-08-30 14:49:131275browse

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

Suppose we have to define a box class with few conditions. As follows-

  • The three attributes l, b and h represent length, width and height respectively (these are private variables)

  • Define a non- Parameterized constructor to set l, b, h to 0, and define a parameterized constructor to initially set the values.

  • Define the getter method of each attribute

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

  • Overloaded less than operator (

  • Create a variable that counts the number of boxes created.

  • So if we enter three boxes (0, 0, 0) (5, 8, 3), (6, 3, 8) and display the data for each box, and check if the third box is smaller than the second one and find the volume of the smaller box and print how many boxes they have by counting 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 problem we will follow the following steps -

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

      li>
    • To overload the less than (

    • the current object's l is the same as the given another The l of the objects is different, then

      • If the l of the current object is less than the l of another object, then true is returned

    • Otherwise, when the b of the current object is different from the b of the given another object, then

      • Returns true if the b of the current object is smaller than the b## of the other object

    • # Otherwise, when the h of the current object is different from the h given another object, then

      • If the current object Returns true if h is less than the h of another object

    Example

    Let us see the following implementation for 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)

The above is the detailed content of C++ program to create box and calculate volume and check using less than operator. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete