Create a class Box containing length, breath and height. Include following methods in it: a) Increment, Overload ++ operator (both prefix & postfix) b) Decrement, Overload -- operator (both prefix & postfix) c) Calculate surface Area d) Calculate Volume e) Overload operator == (to check equality of two boxes), as a friend function f) Overload Assignment operator g) Check if it is a Cube or cuboids Write a program which takes input from the user for length, breath and height to test the above class.




#include<iostream.h>
#include<conio.h>
class Box
{
int length,breadth,height;
public:
double surfacearea();
void print();
double volume();
Box operator++(int n);
Box operator++();
Box operator--(int n);
Box operator--();
Box operator=(Box b1);
friend Box operator==(Box b1,Box b2);
int check(); /*returns 1 if cube and 0 for cuboid*/
Box();
}
Box::Box()
{
cout<<"enter the length , breadth and height respectively"<<endl;
cin>>length>>breadth>>height;
}
int Box::check()
{
if(length==breadth==height)
return 1;
else
return 0;
}

double Box::surfacearea()
{
if(check()==0)
return(2*((length*breadth)+(breadth*height)+(height*length)));
else
return(6*length*length);
}
double Box::volume()
{
return(length*breadth*height);
}
Box Box::operator++()
{
length++;
breadth++;
height++;
return(*this);
}
Box Box::operator--(int n)
{
length--;
breadth--;
height--;
return(*this);
}
Box Box::operator++(int n)
{
length++;
breadth++;
height++;
return(*this);
}
Box Box:: operator--()
{
--length;
--breadth;
--height;
return(*this);
}

Box Box::operator=(Box b1)
{
length=b1.length;
breadth=b1.breadth;
height=b1.height;
return(*this);
}
Box operator==(Box b1, Box b2)
{
if((b1.length==b2.length) && (b1.breadth==b2.breadth) && (b1.height==b2.height))
cout<<"The two boxes have equal dimensions";
else
cout<<"The two boxes are unequal";

}
void Box::print()
{
cout<<endl;
cout<<"the  length is "<<length<<endl;
cout<<"the breadth is "<<breadth<<endl;
cout<<"the height is "<<height<<endl;
}


int main()
{
clrscr();
int d=1;
do
{

Box obj;
cout<<"enter 1 to show volume" <<endl<<" 2 for surface area"<<endl<<"3 to increment using postfix"<<endl<<" 4 to increment using prefix"<<endl;
cout<<"5 to decrement using postfix"<<endl<<"6 to decrement using prefix"<<"7 to check equality of two boxes"<<endl;
cout<<"8 to assign values to a box"<<endl<<"9 to check if it is a cube or cu                      boid"<<endl<<"enter 10 to print current box status"<<endl;
cout<<"enter 0 to quit"<<endl;
cin>>d;
switch(d)
{
case 1:
cout<<"the volume is "<<obj.volume()<<endl;
break;
case 2:
cout<<"the surface area is "<<obj.surfacearea()<<endl;
break;
case 3:
cout<<"increment using postfix";
obj++;
obj.print();
break;
case 4:
cout<<"increment using prefix";
++obj;
obj.print();
break;
case 5:
cout<<"decrement using postfix";
obj--;
obj.print();
break;
case 6:
cout<<"decrement using prefix";
--obj;
obj.print();
break;
case 7:
cout<<"enter the dimensions of other box";
Box obj2;
obj2==obj;
obj2.print();
break;
case 8:
cout<<"to assign values to a box object"<<endl;
Box b2=obj;
b2.print();
break;
case 9:
if(obj.check()==1)
cout<<"it is a cube "<<endl;
else
cout<<"it is a cuboid."<<endl;

break;
case 10:
obj.print();
}
cout<<"Enter 0 to quit"<<endl;
cin>>d;
clrscr();
}
while(d!=0);
getch();
return 1;
}




Output:-


















Comments