-4

The code below is being used to add two complex numbers using operator overloading but I do not understand the argument and the body of the plus operator. Can anyone please explain this?

complex operator+(complex a2) // why this argument is used please explain 
{  
    complex a;  //what is the need of this
    a.r=r+a2.r;  //and how it is being done 
    a.i=i+a2.i;  //this one too
    return a;  
}

I understand that complex is a class and a is its object but i am in illusion why then we used the a2.

Galik
  • 47,303
  • 4
  • 80
  • 117
Paramour
  • 1
  • 1
  • 5
  • Have you read [this](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading)? That should cover more than everything you need to answer the question. – user202729 Mar 11 '18 at 06:58
  • If you're adding two numbers, you need two numbers. `a2` is the second number and the insance of the class is the first. `a = a1 + a2;` – Retired Ninja Mar 11 '18 at 07:01
  • i want a simple explanation of my code very simple.. – Paramour Mar 11 '18 at 07:01
  • Thanks i got it.. a little bit more is expected to explain me the a object – Paramour Mar 11 '18 at 07:02
  • @Paramour Why can't you read the linked questions yourself? People must do serious research before asking. – user202729 Mar 11 '18 at 07:09

4 Answers4

1

The operator in the question deals with the situation when two complex objects are added together to form a new temporary value:

a1 + a2

When the compiler sees that expression it creates a third (temporary) value which is their sum.

It does this by treating the + as a member function of the left hand side of the expression. So the above code is equivalent to this:

a1.operator+(a2); // member function call

So the body of your function is the body of a member function for object a1:

complex operator+(complex a2) // member function of a1 receiving a2 as a parameter 
{  
    complex a;       // this is the new value that will be created from a1 + a2
    a.r = r + a2.r;  // r is a1.r because this object is a1 
    a.i = i + a2.i;  // same with i

    return a;        // the (temporary) result is returned
}

The temporary result is only used if it is assigned to a named variable like this:

a = a1 + a2;
Galik
  • 47,303
  • 4
  • 80
  • 117
0

Operator overloading generally means that you can do something more than what that operator generally does. Here you are adding two complex numbers using "+" operator overloading.So you are doing some thing like this.

result = a1+ a2 where a1 and a2 are two complex numbers with a real and imaginary part respectively. Consider +operator as function name so how will you pass a parameter to the function. a2 is the another number that you want to add with a1 so a2 goes in as a parameter which is also of complex type, and the function return a complex object as "a" which includes real and imaginary part. I hope I answer what you are seeking.

0
complex operator+(complex a2) // why this argument is used please explain 
{  
    complex a;  //what is the need of this
    a.r=r+a2.r;  //and how it is being done 
    a.i=i+a2.i;  //this one too
    return a;  
}

Ans 1. complex operator+(complex a2)
The complex is specifying that the return type is going to be of the type class complex. complex a2 specifies that the operator is going to be used with an object of class complex.

Ans 2. complex a; This is required to do a temporary operation with the objects and data members of the class complex. Think of it like a temp variable you introduces to add 2 numbers in a function and return it later on.

Ans 3. a.r=r+a2.r; Okay now that you have cheated a temporary object of the class complex you say storing the sum of a2's r and the the object that called this overloading function's r and storing it into the temporary object's r.

Ans 4. a.i=i+a2.i; Same as above.

Then you return the temporary object a with the calculated sums of r and i. Note that the type of a is of the class complex and so the return type was specified as complex in line 1.

You will understand it better if you also see the operator being used in the main() function.

complex x,y,z;
x=y+z;

Here x will get the value of the temporary object a. The calling variable is y and a2 will receive z. It is like calling + here like x = y.overloaded_plus(z) // only for explanation

Rishav
  • 3,818
  • 1
  • 31
  • 49
  • Thanks for all the kind answers. – Paramour Mar 11 '18 at 07:44
  • 1
    @Paramour Consider up-voting an answer if it helped (by hitting the up-arrow symbol) and mark it accepted by clicking the *tick* symbol next to the answer. This is to keep the author's spirit high and for some recognition –  Mar 11 '18 at 08:12
0
complex operator+(complex a2) // why this argument is used please explain 
{  
   complex a;  //what is the need of this
   a.r=r+a2.r;  //and how it is being done 
   a.i=i+a2.i;  //this one too
   return a;   
 }

Your question is "I do not understand the argument and the body of the plus operator" and "I understand that complex is a class and a is its object but i am in illusion why then we used the a2."

Think about if you don't pass complex a2 object as a parameter when you are defining the overloaded function like this complex operator+() it will produce an error message because it has required one parameter.

When you will create the object of complex class in main function complex a2 provide help you to pass the value of any other object for sum operation.

Here is a demo.

#include<iostream>
using namespace std;
class N{
private:
    int x,y;
public:
    N(int a=0,int b=0)
    {
        x=a;
        y=b;
    }
    void print(){
        cout<<x<<" : "<<y<<endl;
    }
    N operator+(N a)
    {
        x=a.x;
    }
};
int main(){
    N b;
    N a(2,3);
   a.print();
   b.print();
   b.operator+(a);
   b.print();
   return 0;
 }  

Hope you have got the idea about complex a2 argument. If not comment below.

Sarmad Ali
  • 122
  • 1
  • 13