C++ Program to Subtract Complex Numbers Using Operator Overloading


January 21, 2023, Learn eTutorial
1457

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 mathematical addition and the same operator can be used for string concatenation.  

How to Subtract 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.

Create the function for operator overloading. Here three objects of type complex are created (c1, c2, result). 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 difference between the complex numbers entered by the user 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: 4
5
Enter second complex number:
Enter real and imaginary parts respectively: 2
6
Output Complex number: 2-1i