Complex Class with a Friend Function

Implement an overloaded “+” operator using a friend function.

Challenge

Write a program that implements a Complex class containing an overloaded + operator that can add two Complex objects:

  1. A Complex and a double
  2. A double and a Complex

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
After evaluating e = 1.1 + a using + operator: e = 2.6  1.3

Coding exercise

Your job is to declare a friend function in the Complex class and define it outside the class.

Get hands-on with 1400+ tech skills courses.