operators overloading in c++

Hello! I am looking at this code and trying to understand what is happening.
How does the program "know" that we get BOX3 adding sizes of first 2 ones? Is it because the overloading function has object as parameter and after we created first 2 box objects and as we don't have measures for hte third box, but have the overloading function, that is the last possibility for procesor to use it???- no other options have been left ,so it is taken for granted ,sth like this? We obvioulsy can't use overloaded function before we created and initializated first 2 objects (box1 and box2). It does not look too clear to me, which box is which one, this-> or b. , box1 or box2? Many thanks in advance!!!

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
  #include <iostream>
using namespace 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;

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

   return 0;
}
Last edited on
In another example forum our tutorial there is the similar problem (vectors instead of boxes!)

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
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {}
    CVector (int a, int b) : x(a), y(b) {}
};


CVector operator+ (const CVector& lhs, const CVector& rhs) {
  CVector temp;
  temp.x = lhs.x + rhs.x;
  temp.y = lhs.y + rhs.y;
  return temp;
}

int main () {
  CVector foo (3,1);
  CVector bar (1,2);
  CVector result;
  result = foo + bar;
  cout << result.x << ',' << result.y << '\n';
  return 0;
}




but here we do have a line, saying that third one is first+second.
 
 result = foo + bar;



In the code above there is no implicit such a formula. it is only let's say ,kind of INSINUATION. Please, any comments!!! Many thanks!!!
Last edited on
*this is the first operand and b is the second operand.
Line 67: The compiler processes the right hand side of the = sign first. Processing left to right, the compiler recognizes box1, then sees the + operator. This will generate a call to Box::operator + ( <obj> ). We could overload operator + with different kinds of objects, but in this case, the argument is another box (box2). So this resolves the call to the function at line 27. The result of the function at line 27 is a temporary box (box), which becomes the return value. The returned object (a copy of local variable box) is then assigned to Box3.

Inside operator +, this refers to the left hand side of the + operator (box1) while b refers to box2.
CVector operator+ (const CVector& lhs, const CVector& rhs)

In this case, we have a global function, not a member function of CVector.
The compiler will look for a CVector::operator + first. Not finding that, it will look for a global function. The function at line 12 will match. Global operator functions require two arguments since there is no implicit object as there would be if operator + were a member function.
Last edited on
Topic archived. No new replies allowed.