C++ Program to add two Complex Numbers using Classes

In this program, we will learn how to add two complex numbers using classes in C++ programming language.

What is a complex number?

Complex numbers are numbers that are expressed as a+bi where i is an imaginary number and a and b are real numbers. Some examples are −
6 + 4i
8 - 7i

C++ Programming Code to add two Complex Numbers using Classes

To make this program, we will use classes.

We will declare a class Complex which will store the real and imaginary part of the complex number. This class will have three methods also.

The first method is initComplex(), which will help in initializing them. In this method, it will ask for the real part and imaginary part of the complex number.

The second method is display(), which will allow displaying them on the output screen.

And the third method is sum(), which will help to add two complex numbers passed in the arguments.



using namespace std;

class Complex{
    int real;
    int img;

    void initComplex(){
        cout << "Enter real part: ";
        cin >> real;
        cout << "Enter imaginary part: ";
        cin >> img;

    void display(){
        cout << real << " + " << img << "i" << endl;
    void sum(Complex c1, Complex c2){
        real = c1.real + c2.real;
        img = c1.img + c2.img;
int main(){

    Complex c1,c2,c3;

    cout<<"Enter first complex number"<<endl;
    cout << "First Complex Number: ";

    cout<<"Enter second complex number"<<endl;
    cout << "Second Complex Number: ";

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


    return 0;


Enter first complex number
Enter real part: 6
Enter imaginary part: 3
First Complex Number: 6 + 3i
Enter second complex number
Enter real part: 8
Enter imaginary part: 6
Second Complex Number: 8 + 6i
Sum of two complex numbers is
14 + 9i

You can learn about many other C++ Programs Here.

The following two tabs change content below.
Amit Rawat

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...