Hello Bopaki,
You have been around long enough to know not to use the green check before you get an answer.
In addition to what salem c has said what is the reason for defining "x" and "y" as public variables of the class? It tends to defeat the purpose of the class.
The overloaded ctor is never used.
The default ctor should also set "vol1" and "vol2". Not real important since they are never used.
The function "setSum" defines the variable "total" in the parameter of the function, but uses "vol1" and "vol2" to get the answer except that "vol1" and "vol2" have garbage values at this point because they were never changed. In the end when the function ends so does the variable "total". Is there a point to this function?
The overloaded "operator +" is not working the way you are thinking. If you do the math on paper you will see that this function is working correctly, but not giving you the answer that you want. I think what you want is to add the volume of each box not add the dimensions of each box for later calculation. This is giving you the wrong answer.
In the "private" section of the class the variables "vol1" and "vol2" are never set to anything or never used. Did you have a use/point for them?
The overloaded "operator <<" prints the value of "x" and "y", but since they never receive a proper value, other than the ctor, they just print zeros.
In "main" you define the variables "volume" and "sum". "volume" may be useful, but you could just as easily say:
cout << "Volume of Box1 : " << Box1.getVolume() << endl;
.
In this bit of code:
1 2 3 4 5
|
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.setSum(210);
|
On line 5 did you really mean "Box2"?
In your code lines 77 and 83 would work much better as:
sum += Box1.getVolume();
and the same for "Box2", but in the end you never use "sum" beyond these two lines.
The following output is based on some adjustments I made to help show what is being print out.
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400 Value of volume using "Box3".
Volume of Box3 : 1770 Value of volume using "sum".
0 0 Value of "x" and "y".
|
This output should help you to understand what is happening.
Hope that helps,
Andy