Design a class named Box whose dimensions are integers and private to the class.
ID: 3815711 • Letter: D
Question
Design a class named Box whose dimensions are integers and private to the class. The dimensions are labeled: length l, breadth b, and height h.
The default constructor of the class should initialize l, b, and h to 0.
The parameterized constructor Box(int length, int breadth, int height) should initialize Box's l,b and h to length, breadth and height.
The copy constructor Box(Box B) should set l,b and h to B's l,b and h, respectively.
Every constructor should increment the global variable BoxesCreated.
The destructor should increment the global variable BoxesDestroyed.
Apart from the constructor and destructor, the class should have 4 functions:
int getLength() - Return box's length
int getBreadth() - Return box's breadth
int getHeight() - Return box's height
long long CalculateVolume() - Return the volume of the box
Overload the operator < for the class Box. Box A < Box B if:
A.l < B.l
A.b < B.b and A.l==B.l
A.h < B.h and A.b==B.b and A.l==B.l
Overload operator << for the class Box().
If B is an object of class Box:
cout<<Bcout<<B should print B.l, B.b and B.h on a single line separated by spaces.
Constraints
0 {l,b,h } 10^5
Two boxes being compared using the < operator will not have all three dimensions equal
I was given this prompt. The question I have is:
What phase of the Software design cycle does this document represent? Are there any preconditions and postconditions presented here? If so what are they?
Explanation / Answer
#include <iostream>
using namespace std;
//global variables
int BoxesCreated =0;
int BoxesDestroyed = 0;
class Box
{
private:
int l;
int b;
int h;
public:
Box()//default constructor
{
l=0;
b=0;
h=0;
BoxesCreated++;
}
Box(int length, int breadth, int height)//argument constructor
{
l = length;
b = breadth;
h = height;
BoxesCreated++;
}
Box(const Box &B) //copy constructor
{
l = B.l;
b = B.b;
h = B.h;
BoxesCreated++;
}
~Box() //destructor
{
BoxesDestroyed++;
}
//get methods
int getLength() //- Return box's length
{
return l;
}
int getBreadth() //- Return box's breadth
{
return b;
}
int getHeight() //- Return box's height
{
return h;
}
long long CalculateVolume() //- Return the volume of the box
{
return l*b*h;
}
//Overload the operator < for the class Box. Box A < Box B if:
bool operator<(Box other) //overloaded < operator
{
if(l<other.l || (l == other.l && b<other.b) || (l== other.l && b == other.b && h<other.h))
return true;
else
return false;
}
friend ostream& operator<<(ostream& os, const Box& B);
};
ostream& operator<<(ostream& os, const Box& B) //overloaded ostream operator
{
os << B.l << " " << B.b << " " << B.h;
return os;
}
int main()
{
Box box1(56,73,54);
Box box2(56,73,65);
cout<<" Number of boxes created = "<<BoxesCreated<<endl;
cout<<box1<<endl;
cout<<box2<<endl;
if(box1 < box2)
cout<<" Box 1 is smaller than Box 2";
else
cout<<" Box 1 is bigger than Box 2";
cout<<" Volume of Box 1 = "<<box1.CalculateVolume();
cout<<" Volume of Box 2 = "<<box2.CalculateVolume();
return 0;
}
output:
Number of boxes created = 2
56 73 54
56 73 65
Box 1 is smaller than Box 2
Volume of Box 1 = 220752
Volume of Box 1 = 265720
This is coding phase of Software design cycle.
Preconditions: 0 {l,b,h } 10^5
Postconditions : To check which is the bigger box object.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.