Program To Add Two Complex Numbers By Overloading Constructors In C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.


Background and Explanation of the code


The above program is used to add two complex numbers using the concept of constructor overloading.There are three constructor functions associated with the class complex. A particular constructor invoked depends upon the number of arguments passed when an object is created. Both the data members real and imag of object c2 initializes to 5 using constructor with one parameter.The data members of object c1 are initialized using constructor with two parameters.These two complex numbers are added and result is stored in c3 object which is finally displayed.

OUTPUT

11 + 13i