Using C this code include includeBoxh using namespace std i
Using C++ & this code:
#include<iostream>
#include\"Box.h\"
using namespace std;
int main(void) {
Box b(5, 10, 5);
Box c(3, 20, 3);
cout << b << endl << c << endl;
cout << b << \" + \" << c << \" = \" << b + c << endl;
}
Add the Cube class which inherits from Box. There should be only 3 members of the Cube class: the constructor, and two operators (-,/). A Box has a height, width, and depth (abbreviated h,w,d). For two boxes a and b, the addition of two boxes is defined as: c = a + b c.h = a.h + b.h, c.w = a.w + b.w, c.d = a.d + b.d; Similarly, subtraction is defined as: c = a - b c.h = a.h - b.h, c.w = a.w - b.w, c.d = a.d - b.d; Division is defined as: c = a / b c.h = a.h2 /(a.h + b.h), c.w = a.w2 /(a.w + b.w), c.d = a.d2 /(a.d + b.d)
Here is the code of the Box Class:
void Box::getDims(double &h, double &w, double &d) const {
h = height;
w = width;
d = depth;
}
void Box::setDims(double h, double w, double d) {
height = h;
width = w;
depth = d;
}
Box Box::operator+(const Box &other) const {
Box result;
result.setDims(height + other.height,
width + other.width, depth + other.depth);
return result;
}
ostream& operator<<(ostream &os, const Box &box) {
double h, w, d;
box.getDims(h, w, d);
os << \"box[h,w,d]::[\"
<< h << \", \" << w << \", \" << d
<< \"]\";
return os;
}
The main function of Lab11_5.cpp should contain the following text exactly:
int main(void) {
Cube b(12, 30, 6);
Cube c(4, 10, 2);
cout << b << endl << c << endl;
cout << b << \" + \" << c << \" = \" << b + c << endl;
cout << b << \" - \" << c << \" = \" << b - c << endl;
cout << b << \" / \" << c << \" = \" << b / c << endl;
return 0; }
Which will produce the following output:
box[h,w,d]::[12, 30, 6]
box[h,w,d]::[4, 10, 2]
box[h,w,d]::[12, 30, 6] + box[h,w,d]::[4, 10, 2] = box[h,w,d]::[16, 40, 8]
box[h,w,d]::[12, 30, 6] - box[h,w,d]::[4, 10, 2] = box[h,w,d]::[8, 20, 4]
box[h,w,d]::[12, 30, 6] / box[h,w,d]::[4, 10, 2] = box[h,w,d]::[9, 22.5, 4.5]
Press any key to continue . . .
Solution
void Box::getDims(double &h, double &w, double &d) const {
h = height;
w = width;
d = depth;
}
void Box::setDims(double h, double w, double d) {
height = h;
width = w;
depth = d;
}
Box Box::operator+(const Box &other) const {
Box result;
result.setDims(height + other.height,
width + other.width, depth + other.depth);
return result;
}
ostream& operator<<(ostream &os, const Box &box) {
double h, w, d;
box.getDims(h, w, d);
os << \"box[h,w,d]::[\"
<< h << \", \" << w << \", \" << d
<< \"]\";
return os;
}
The main function of Lab11_5.cpp should contain the following text exactly:
int main(void) {
Cube b(12, 30, 6);
Cube c(4, 10, 2);
cout << b << endl << c << endl;
cout << b << \" + \" << c << \" = \" << b + c << endl;
cout << b << \" - \" << c << \" = \" << b - c << endl;
cout << b << \" / \" << c << \" = \" << b / c << endl;
return 0; }
OUTPUT:
box[h,w,d]::[12, 30, 6]
box[h,w,d]::[4, 10, 2]
box[h,w,d]::[12, 30, 6] + box[h,w,d]::[4, 10, 2] = box[h,w,d]::[16, 40, 8]
box[h,w,d]::[12, 30, 6] - box[h,w,d]::[4, 10, 2] = box[h,w,d]::[8, 20, 4]
box[h,w,d]::[12, 30, 6] / box[h,w,d]::[4, 10, 2] = box[h,w,d]::[9, 22.5, 4.5]


