Help me display the total volumes of Box1 & Box3

Line 87 and 88 calculates the sum of two volumes Box1 + Box2.
This is what I get when I try to display their sum:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
The sum of volume of Box1 & Box2 = 0 0

Process returned 0 (0x0) execution time : 0.089 s
Press any key to continue.


The answer should be 1770 (210 + 1560)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 #include <iostream>
using namespace std;

class Box {
   public:
       double x, y;
      // Box();
       //friend ostream&operator<<(ostream& stream, Box obj);
     friend ostream& operator<< (ostream& stream, const Box& box);
       Box(double a, double b)
       {
           x = vol1;
           y = vol2;
           }
    Box()
{
    x = 0.0;
    y = 0.0;
}
      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;
      }
      void setSum(double total) {
      total = vol1 + vol2;
      }

      // 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
      double vol1, vol2;
};
//ostream& operator<<(ostream& stream, sum obj)
ostream& operator <<(ostream& stream, const Box& box)
{
    stream<<box.x<<" ";
stream<<box.y<<" ";
return stream;
}
// 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
    double sum = 0.0;
   // box 1 specification
   Box1.setLength(6.0);
   Box1.setBreadth(7.0);
   Box1.setHeight(5.0);
   Box2.setSum(210);

   // box 2 specification
   Box2.setLength(12.0);
   Box2.setBreadth(13.0);
   Box2.setHeight(10.0);
   Box2.setSum(1560);

    sum = Box1.getVolume();
   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;


    sum = Box2.getVolume();
   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
   Box3 = Box1 + Box2;
      // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   cout << "  " << Box3 << endl;


   return 0;
}
 

please help
Fix the warnings first.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ g++ -Wall -Wextra -O2 foo.cpp
foo.cpp:10:19: warning: unused parameter ‘a’ [-Wunused-parameter]
        Box(double a, double b)
                   ^
foo.cpp:10:29: warning: unused parameter ‘b’ [-Wunused-parameter]
        Box(double a, double b)
                             ^
foo.cpp: In member function ‘void Box::setSum(double)’:
foo.cpp:32:26: warning: parameter ‘total’ set but not used [-Wunused-but-set-parameter]
       void setSum(double total) {
                          ^
foo.cpp: In function ‘int main()’:
foo.cpp:64:12: warning: variable ‘sum’ set but not used [-Wunused-but-set-variable]
     double sum = 0.0;
            ^



> The answer should be 1770 (210 + 1560)
Well you need to do something different when you add two boxes together then.
1
2
3
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;

Because this just isn't the sum of two small boxes.
It's the minimum sized box capable of holding the two smaller boxes within it (plus some dead space).

Of course, this makes no sense now the post has been reported....
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
Topic archived. No new replies allowed.