C++ Program to Add Two Complex Numbers using Class

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

Problem Description

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

Expected Input and Output

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

advertisement
advertisement

3. When both real and imaginary parts are negative.

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

Output= -8 + -11i

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Problem Solution

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.

Program/Source Code

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.

  1. /* C++ Program to add two Complex Numbers */
  2. #include<iostream>
  3. using namespace std;
  4. class Complex{
  5. public:
  6.     int real;
  7.     int imag;
  8.      /* Function to set the values of
  9.       * real and imaginary part of each complex number
  10.       */
  11.      void setvalue()
  12.     {
  13.         cin>>real;
  14.         cin>>imag;
  15.     }
  16. 	/* Function to display the sum of two complex numbers */
  17.     void display()
  18.     {
  19.         cout<<real<<"+"<<imag<<"i"<<endl;
  20.     }
  21. 	/* Function to add two complex numbers */
  22.  
  23.     void sum(Complex c1, Complex c2)
  24.     {
  25.         real=c1.real+c2.real;
  26.         imag=c1.imag+c2.imag;
  27.     }
  28.     };
  29. int main()
  30.     {
  31.         Complex c1,c2,c3;
  32.         cout<<"Enter real and imaginary part of first complex number"<<endl;
  33.         c1.setvalue();
  34.         cout<<"Enter real and imaginary part of second complex number"<<endl;
  35.         c2.setvalue();
  36.         cout<<"Sum of two complex numbers is"<<endl;
  37.         c3.sum(c1,c2);
  38.         c3.display();
  39.     return 0;
  40.     }
Program Explanation

Here in this program we have created a few functions.

advertisement

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.

Runtime Test Cases
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.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.