C++ program to overload Decrement Operator (--)

//to decrease object of Box class by one (i.e. decrease each data member by one)
//l,b,h as private data members
//by member function

#include <iostream>
using namespace std;
class Box
{
    int l,b,h;
    public:
        Box(int le,int br,int he)
        {
            l=le;
            b=br;
            h=he;
        }
        Box operator--(int)
        {
            l--;
            b--;
            h--;
        }
        void displayVolume()
        {
            cout<<"The volume is "<<l*b*h<<endl;
        }
};
int main()
{
    Box b1(3,4,5);
    b1.displayVolume();
    b1--;
//same as b1.operator--();
    b1.displayVolume();
    return 0;
}

No comments:

Post a Comment