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


October 27, 2023, Learn eTutorial
4111

For a better understanding of this C++ program example, we always recommend you learn the basic topics of C++ programming listed below:

What is a complex number?

The concept of a complex number was introduced by a Greek mathematician in the 1st century.

A number is considered a complex number if it has a real number and an imaginary part. We can write a complex number in the format of z = x + iy. In simple words, we can say that a number which is a  combination of a real number and an imaginary part is called a complex number. 

Here,

  • 'x' and 'y' are the real numbers
  • 'i' is the imaginary part called iota whose value is defined as a square root of minus 1. i = (√-1)

let us take an example, 5 + 8i which is a complex number as 5 is a real number and 8i is a imaginary number. The practical usage of complex numbers is to represent periodic motions like light waves, current waves, and water waves. 

What is operator overloading?

Operator overloading is one of the methods in polymorphism. In C++, operator overloading is defined as using an operator for different operations. for example, '+' can be used for addition and the same operator can be used for string concatenation. 

How to do the addition of Complex Numbers Using Operator Overloading in C++

In the C++ program, we are going to deal with a binary operator (an operator that operates on two operands)  ‘ + ‘.

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.

Create the function for operator overloading. Here three objects of type complex are created (c1, c2, result) to store the values that the user input in the variables c1 and c2 (real and imaginary values).  Define a function to display the real and imaginary parts of the complex number. The sum of the complex numbers the user enters will be stored in the object result.

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