C++ Program to Addition of Complex Numbers Using Operator Overloading

Here we are discussing subtracting complex numbers using operator overloading of the binary operator ‘+‘.

 What is operator overloading?

Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator.
Before practicing the concept of operator overloading, you should have to know about the below topics.

Classes and Objects in C++

A class is a blueprint for the object.We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.

class className {
   // some data
   // some functions
}
;

A class is defined in C++ using the keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.

C++ Objects

When a class is defined, only the specification for the object is defined; no memory or storage is allocated.
To use the data and access functions defined in the class, we need to create objects.
className objectVariableName;
// sample function
void sampleFunction() {
    // create objects
    Room room1, room2;
}

int main(){
    // create objects 
    Room room3, room4;
}

Here, two objects room1 and room2 of the Room class are created in sampleFunction(). Similarly, the objects room3 and room4 are created in the main().
As we can see, we can create objects of a class in any function of the program. We can also create objects of a class within the class itself, or in other classes.

C++ constructors

A constructor is a special type of member function that is called automatically when an object is created.
In C++, a constructor has the same name as that of the class and it does not have a return type.
class  Wall {
  public:
    // create a constructor
    Wall() {
      // code
    }
};

Here, the function Wall() is a constructor of the class Wall. Notice that the constructor
•    has the same name as the class,
•    does not have a return type, and
•    is public

Operator overloading in C++

In C++, we can change the way operators work for user-defined types like objects and structures. This is known as operator overloading.
class className {
    ... .. ...
    public
       returnType operator symbol (arguments) {
           ... .. ...
       } 
    ... .. ...
};

Here,
•    returnType is the return type of the function.
•    operator is a keyword.
•    symbol is the operator we want to overload. Like: +, <, -, ++, etc.
arguments are the arguments passed to the function.

How  to Add Complex Numbers Using Operator Overloading in C++

Here we are going to deal with a binary operator (an operator that operates on two operands)  ‘ + ‘, one of the operands should be passed as an argument to the operator function.
Create a class complex. Declare two float type variables real and imag for the class complex. Create a constructor complex and set the value of data members real and imag to 0. Define a function input to read the value for the variable real and imag from the user.
void input()
       {
           cout << "Enter real and imaginary parts respectively: ";
           cin >> real;
           cin >> imag;
       }

Create the function for operator overloading. For binary operators in C++, the object on the right-hand side of the operator is always assumed as an argument by the compiler. Here three objects of type complex are created (c1, c2, result). The user is asked to enter the real and imaginary parts for two complex numbers which are stored in objects c1 and c2. Function for operator overloading will be like
Complex operator + (Complex c2)
       {
           Complex temp;
           temp.real = real + c2.real;
           temp.imag = imag + c2.imag;

           return temp;
       }

Define a function to display the real and imaginary parts of the complex number. 
void output()
       {
           if(imag < 0)
               cout << "Output Complex number: "<< real << imag << "i";
           else
               cout << "Output Complex number: " << real << "+" << imag << "i";
       }

The difference between the complex numbers entered by the user will be stored in the object result.
result = c1 + c2
This statement invokes the operator function Complex operator + (Complex c2).
When result = c1 + c2 is executed, c2 is passed as an argument to the operator function.

Display the result on the screen.
result.output();
  
Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Create a class complex with float  variables real and imag;

Step 4: create a constructor complex( ); set the value of real and imag to 0

Step 5: Define the function for reading the real and imaginary parts of the numbers from the user.

Step 6: Define a function for operator overloading.

Step 7: Define a function to display the real and imaginary parts of the complex number.

Step 8:Create three objects for the class complex, c1, c2, and result;

Step 9: Read the numbers from the user and store them in the objects c1 and c2. C1.step 5 and c2.step5

Step 10: invoke step 6 and store the resultant number in the object result;

Step 11: call step 7 with the object result. result.step7; 

Step 12:Exit

C++ Source Code

                                          #include <iostream>
using namespace std;

class Complex
{
    private:
      float real;
      float imag;
    public:
       Complex(): real(0), imag(0){ }
       void input()
       {
           cout << "Enter real and imaginary parts respectively: ";
           cin >> real;
           cin >> imag;
       }

       // Operator overloading
       Complex operator + (Complex c2)
       {
           Complex temp;
           temp.real = real + c2.real;
           temp.imag = imag + c2.imag;

           return temp;
       }

       void output()
       {
           if(imag < 0)
               cout << "Output Complex number: "<< real << imag << "i";
           else
               cout << "Output Complex number: " << real << "+" << imag << "i";
       }
};

int main()
{
    Complex c1, c2, result;

    cout<<"Enter first complex number:\n";
    c1.input();

    cout<<"Enter second complex number:\n";
    c2.input();

    // In case of operator overloading of binary operators in C++ programming, 
    // the object on right hand side of operator is always assumed as argument by compiler.
    result = c1 + c2;
    result.output();

    return 0;
}

  





                                      

OUTPUT

Enter first complex number:
Enter real and imaginary parts respectively: 2
8
Enter second complex number:
Enter real and imaginary parts respectively: 4
3
Output Complex number: 6+11i