Create a class Box containing length, breath and height. Include following methods in it:
a) Calculate surface Area
b) Calculate Volume
c) Increment, Overload ++ operator (both prefix & postfix)
d) Decrement, Overload -- operator (both prefix & postfix)
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 cuboid
Write a program which takes input from the user for length, breath and height to test the
above class.
#include <iostream>
using namespace std;
class Box {
private:
float length;
float breath;
float height;
public:
//Constructor
Box(): length(0),breath(0),height(0) { }
//Constructor
Box(float length,float breath,float height){
this->length=length;
this->breath=breath;
this->height=height;
}
//Calculate surface Area
float calculateSurfaceArea(){
return 2*length*breath+2*length*height+2*length*breath;
}
//Calculate Volume
float calculateVolume(){
return 0;
}
// prefix increment
Box operator ++(){
Box temp;
temp.length = ++length;
temp.breath = ++breath;
temp.height = ++height;
return temp;
}
//postfix
Box operator ++ (int){
Box temp;
temp.length=length++;
temp.breath=breath++;
temp.height=height++;
return temp;
}
Box operator --(){
Box temp;
temp.length = --length;
temp.breath = --breath;
temp.height = --height;
return temp;
}
//postfix
Box operator - (int){
Box temp;
temp.length=length--;
temp.breath=breath--;
temp.height=height--;
return temp;
}
friend bool operator == (Box ob1, Box ob2){
return (ob1.length==ob2.length) && (ob1.breath==ob2.breath) && (ob1.height==ob2.height);
}
Box& operator= (const Box &box){
this->length=box.length;
this->breath=box.breath;
this->height=box.height;
return *this;
}
bool isCube(){
return (this->length==this->breath) && (this->breath==this->height);
}
//void display info about box
void display(){
cout<<"The length: "<<this->length<<"\n";
cout<<"The breath: "<<this->breath<<"\n";
cout<<"The height: "<<this->height<<"\n";
}
};
//The start point of the program
int main (){
Box box(10,10,10);
float length;
float breath;
float height;
// input from the user for length, breath and height to test the above class.
cout<<"Enter the length: ";
cin>>length;
cout<<"Enter the breath: ";
cin>>breath;
cout<<"Enter the height: ";
cin>>height;
Box userBox(length,breath,height);
cout<<"\nbox:\n";
box.display();
cout<<"\n++box:\n";
++box;
box.display();
userBox++;
cout<<"\nuserBox++:\n";
userBox.display();
cout<<"\n";
if(userBox==box){
cout<<"The boxes are equal.\n";
}else{
cout<<"The boxes are not equal.\n";
}
cout<<"\nuserBox\n";
if(userBox.isCube()){
cout<<"The box is cube.\n";
}else{
cout<<"The box is cuboid.\n";
}
cout<<"\nuserBox=box\n";
userBox=box;
cout<<"userBox\n";
userBox.display();
//delay
system("pause");
return 0;
}
Comments
Leave a comment