How can I display the sum of object1 and object2

I get this output when I try to add two objects:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 10395

The sum of Box1 & Box2 = 0

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

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
99
100
101
102
103
104
105
 #include <iostream>
#include<cstdlib>
#include<cctype>
using namespace std;

class Box {
   public:
        void print(ostream& outs) ;
        //void print();
        Box();

        Box(double vol1, double vol2, double total);

       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
      double vol1;
      double vol2;
};
//void Box::print()
//{
  //  print(cout);
//}
Box::Box(double vol1, double vol2, double total)
{
    total = vol1 + vol2;
}
Box::Box()
        {
            vol1 = 0;
            vol2 = 0;
        }

void Box::print(ostream& outs)
{
           outs <<vol1 + vol2;
}

// 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);

    //Box 3 specification
    Box3.setLength(21.0);
   Box3.setBreadth(33.0);
   Box3.setHeight(15.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;

   // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

    cout <<endl;
     // Add two object as follows:
       Box3 = Box1 + Box2;
   cout << "The sum of Box1 & Box2 = " ;
   Box3.print(cout);

 cout << endl;
   return 0;
}


Please assist
After the constructor, vol1 and vol2 are never updated, so that is why their values remains as zero.
it's more egregious than that
1
2
3
4
Box::Box(double vol1, double vol2, double total)
{
    total = vol1 + vol2;
}
¿what do you think you are doing there?


1
2
3
4
5
6
7
private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
      double vol1;
      double vol2;
};
¿why are `vol1' and `vol2' member variables? ¿what do they represent?
consider
1
2
3
Box brick(4, 2, 1);
brick.vol1; //¿what's this?
brick.vol2; //¿what's this? 



Box operator+( const Box& b)
not sure how does that work, you can't combine two boxes to get a new one
it makes no sense to overload the operator
Last edited on
You don't need to create a new thread for each response that you make. This is a response to the other thread that you created today about the exact same problem...

One thing that you need to realize is that the = operator in C++ is an assignment operator. It evaluates the expression on the right, with the current values that the variables has at that point, and assigns it to the variable on the left.

1
2
3
void setSum(double total) {
	total = vol1 + vol2;
}

With this in mind we can see that this function isn't good for anything. It calculates the sum of vol1 and vol2 and assigns the value to total, which is a local variable, so there are no observable effects from outside the function.

I agree with what ne555 said above, vol1 and vol2 does not really make sense as member variables. I also don't understand the purpose of the setSum function (What's the sum of a box?).

I also agree that it doesn't make much sense to be able to do addition with boxes. The way you have defined the + operator does not mean that the resulting box will have a volume that is equal to the sum of volumes of the two original boxes.

If the sides of box1 is 6, 7, 5, and the sides of box2 is 12, 13, 10, then adding the sides using your + operator will result in a box with the sides 18, 20, 15 which means that the volume is 5400.

If you want to calculate the sum of volumes of the two boxes the correct way would be
 
double volumeSum = box1.getVolume() + box2.getVolume();
Last edited on
Okay thank you everybody.
You are both correct.
The answer of 5400 is sport ON correct.
I was very confused.
I did the calculation myself manually
Topic archived. No new replies allowed.