Complex Class with a + Operator

Understand overloading the + operator in a Complex Class.

Challenge

Write a program that implements a Complex class to maintain complex numbers. Also, provide an overloaded + operator in it that can add two complex numbers and make a provision to add a complex number with a double using the same overloaded operator. In this addition, the double value should be taken as the real value of the complex number and the imaginary value should be treated as 0.

Sample run

Here’s what you should see when you run the program.

a = 1.5  1.3
b = 2.5  2.3
After evaluating c = a + b using + operator: c = 4  3.6
After evaluating d = a + 1.1 using + operator: d = 2.6  1.3

Coding exercise

Your job is to overload the + operator in the code widget given below.

Get hands-on with 1200+ tech skills courses.