Overloading Addition Operator (+)

Written by Raza on. Posted in C++

While working with classes, we often want to have the user defined functionalities which help enormously during the rest of the program. When we deal a lot with arrays, we often want to add the contents of two arrays into a single larger array. This could tediously be done by declaring another array of the combined size of the two and adding them one by one in it. The better option to do this is to overload the addition operator ( ) once which saves us doing this process repeatedly if we add two arrays time and again in our program. Let us see how we can do that efficiently.

We are working once again with our simple Vector class. Its declaration is as follows:


<p>class Vector</p>
<p>private :</p>
<p>int *arr ;</p>
<p>int size ;</p>
<p>public :</p>
<p>Vector& operator = (const Vector&) ;      //we wrote it last time</p>
<p>}  v1, v2, v3 ;</p>


For the built in types, we add two variables this way.

<p>a = b c;</p>

We want the objects of our class to behave exactly the same way. This would certainly save us some head-ache. To ensure such syntax, we casino online need to write three things ourselves beforehand.

  1. Assignment Operator.
  2. Copy Constructor.
  3. Addition Operator.

We did overloading of assignment operator last time, so let us overload plus operator.

The overloaded plus operator function is called on one of the two objects to be added. The second object is passed as a parameter. The function creates temporary object with its data members, adds the data members of two adding object in it and return its copy. Make sure you return the temp object by copy and not by value. This is because when the function gets executed, the temp object gets destroyed. Returning reference iCare best-data-recovery.com Recovery Free scans 300% faster than other data recovery software to undelete lost files when files have been mistakenly deleted or lost due to virus attack and more. of that object is a very serious error and may lead to crashing of entire program. When you return the object by value, copy constructor gets called. It adds the value of the temp object in the object to the left hand of the (=) operator. The source code would look like this.

<p>Vector Vector :: operator ( const Vector & obj ) const<br />
<p>int i = 0 ;</p>
<p>Vector temp ;</p>
<p>temp.size = this->size obj.size ;</p>
<p>temp.arr = new int [temp.size] ;</p>
<p>for (i = 0 ; i < this->size ; i)</p>
<p>temp.arr[i] = this->arr [i] ;</p>
<p>for (int j = 0 ; j < obj.size ; j, i)</p>
<p>temp.arr[i] = obj.arr [j] ;</p>
<p>return temp ;<br />
<p>int main ()<br />
{<br />
Vector v1, v2, v3 ;</p>
<p>v3 = v1 v2 ;</p>
<p>return 0 ;<br />

In the above written code, we can declare 3 at the time of addition as well. In that case, assignment operator would not be needed. This code caters for chain of additions as well, like a = b c d e. the code is perfect for all practical purposes. Also remember to write     a = b c and not as b c = a. Subtraction, Multiplication and Division operators are overloaded in the similar manner with just the difference of arithmetic operation.

Tags: , , ,