Write a program to overload the operator ‘+’ for complex numbers.
Answers
Explanation:
In this program, three objects of type Complex are created and user is asked to enter the real and imaginary parts for two complex numbers which are stored in objects c1 and c2.
Then statement result = c1 -c 2 is executed. This statement invokes the operator function Complex operator - (Complex c2).
When result = c1 - c2 is executed, c2 is passed as argument to the operator function.
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.
Then, this function returns the resultant complex number (object) to main() function which is displayed on to the screen.
Though, this tutorial contains the overloading of - operators, binary operators in C++ programming like: +, *, <, += etc. can be overloaded in a similar manner.