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.