This is a C++ Program to add two Complex Numbers.

We have to input the real and imaginary parts of two complex numbers separately and add them using classes and objects in C++.

**1. When the real and imaginary part both are positive.**

First Complex Number = 5 + 6i Second Complex Number = 1 + 3i

**Output= 6 + 9i**

**2. When any one of the imaginary or real part is negative.**

First Complex Number = -4 + 6i Second Complex Number = 5 + (-3i)

**Output= 1 + 3i**

**3. When both real and imaginary parts are negative.**

First Complex Number = -5 + -(6i) Second Complex Number = -3 + -(5i)

**Output= -8 + -11i**

In this problem, we are going to create a function that adds two complex numbers. First the user has to provide real and imaginary parts as input for both the complex numbers. After that the real part of first complex number will be added with the real part of second one, similarly the imaginary part of first complex number will be added with the imaginary part of second complex number.

Here is source code of the C++ Program to add two Complex Numbers using Classes and Objects. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

`/* C++ Program to add two Complex Numbers */`

`#include<iostream>`

using namespace std;

class Complex{

public:

int real;

int imag;

`/* Function to set the values of`

`* real and imaginary part of each complex number`

`*/`

void setvalue()

`{`

cin>>real;

cin>>imag;

`}`

`/* Function to display the sum of two complex numbers */`

void display()

`{`

cout<<real<<"+"<<imag<<"i"<<endl;

`}`

`/* Function to add two complex numbers */`

void sum(Complex c1, Complex c2)

`{`

real=c1.real+c2.real;

imag=c1.imag+c2.imag;

`}`

};

int main()

`{`

`Complex c1,c2,c3;`

cout<<"Enter real and imaginary part of first complex number"<<endl;

c1.setvalue();

cout<<"Enter real and imaginary part of second complex number"<<endl;

c2.setvalue();

cout<<"Sum of two complex numbers is"<<endl;

c3.sum(c1,c2);

c3.display();

return 0;

`}`

Here in this program we have created a few functions.

1. **setvalue()**

This function allows us to take input for real and imaginary parts of both the complex numbers. To take in real and imaginary part as an input for first complex number we have called this function using an object c1. So, this function sets the real and imag values for first complex number. Similarly to set the values of real and imaginary part of second complex number, we can call it using a new object c2.

2. **sum()**

This function takes in two complex numbers as a parameter. Using this function we have added the real parts of both the complex numbers together and stored them into variable real which is the real part of the resultant complex number. Similarly we have added the imaginary parts of both the complex numbers and stored the result into variable imag which is the imaginary part of the resultant complex number.

3. **display()**

We have called this function using an object for the resultant complex number, to display the real and imaginary parts of the resultant complex number obtained after addition.

1. Enter real and imaginary part of first complex number 5 6 Enter real and imaginary part of second complex number 1 3 Sum of two complex numbers is 6+9i 2. Enter real and imaginary part of first complex number -4 6 Enter real and imaginary part of second complex number 5 -3 Sum of two complex numbers is 1+3i 3. Enter real and imaginary part of first complex number -5 -6 Enter real and imaginary part of second complex number -3 -5 Sum of two complex numbers is -8+-11i

**Sanfoundry Global Education & Learning Series – 1000 C++ Programs.**

**Related Posts:**

- Practice Programming MCQs
- Check C++ Books
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for C++ Internship