#include <iostream>
usingnamespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Main function for the program
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3;
// Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
Box3.getVolume();
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
cout << "The sum of Box1 & Box2 = " << (Box3 = (Box1 + Box2)) << endl;
return 0;
}
I get this error:
C:\Martin\MalikChapter3\overloadOperatorPlus.cpp: In function 'int main()':
C:\Martin\MalikChapter3\overloadOperatorPlus.cpp:66:64: error: no match for 'operator<<' in 'std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"The sum of Box1 & Box2 = ")) << (Box3 = (*(const Box*)(& Box1.Box::operator+((*(const Box*)(& Box2))))))'
In the expression Obj1 + Obj2,
Obj1 is the one that calls the operator. So length, breadth and height would refer to the length breadth and height of the calling object which is Obj1. Second operand is captured into 'b'.
I think you only use this-> when local variable has the same name. Somebody else can explain this->'s use better.
On line 60 & 61 there is that code:
// Add two object as follows:
Box3 = Box1 + Box2;
I changed line 66 to this:
1 2
cout << "The sum of Box1 & Box2 = ";
Box3 = (Box1 + Box2) ;
and I now get this output:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
The sum of Box1 & Box2 =
Process returned 0 (0x0) execution time : 0.131 s
Press any key to continue.
The sum of the two volumes (210 + 1560) is not displayed.
When you have a sphere, you increase its dimension by increasing its radius, not by increasing its volume.
When you have cuboid, you increase its dimensions by increasing its length, width and height, not by increasing its volume because then you can't trace back its length, width and height.
So technically in this case you're not adding to cuboids. You're increasing a cuboid's dimension by the magnitude of the dimensions of another cuboid.
If you had to do it with volumes then you wouldn't need an operator for +. And you would not know the dimensions of the new cuboid because you've lost them when you added the volumes. If you had to do it by extending the dimensions manually by adding to length, breadth and height then again you wouldn't need operator for + and it would be more cumbersome.
Addition of two separate cuboids has nothing to do with their dimensions. It only has to do with their volume. So again you wouldn't need an operator for +.
Anyways it's just a problem nothing of that really matters.