Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Constructing a Class
3.
Initializing the constructors
3.1.
Non-parameterized ConstructorThis constructor is required to initialize the initial values of the data members to zero.Syntax:
3.2.
Parameterized ConstructorThis constructor is required to initialize the initial data members to the values passed by the main function.Syntax:
4.
C++ program to Add Two Complex Numbers
4.1.
Code
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

C++ program to add two complex numbers using Class

Author Sanjana Yadav
0 upvote

Introduction

Complex numbers are defined as a+bi, where i is an imaginary number and a and b are real numbers.

For example:

Input: 6 + i9 and 4 + i3

Here x1= 4 and x2 = 8. On adding x1 and x2, we get (6 + 4) = 10

Further, y1 = 9 and y2 = 3. On adding y1 and y2, we get (9 + 3) = 12

Constructing a Class

We'll start by building a class for complex numbers. The observation demonstrates that a complex number contains both a real number (x1) and an imaginary number (y1)

To represent complex integers, we require two data members.

The following is the class structure:

class ComplexSum  
{  
    public:  
    int realNum; // To store real part of complex number  
    int imaginaryNum; // To store imaginary part of complex number  
}
You can also try this code with Online C++ Compiler
Run Code

Initializing the constructors

To initialize the data members of the Complex class, two constructors will be created.

Non-parameterized Constructor
This constructor is required to initialize the initial values of the data members to zero.
Syntax:

ComplexSum()  
{  
    realNum = 0;  
    imaginaryNum = 0;  
}  
You can also try this code with Online C++ Compiler
Run Code

Parameterized Constructor
This constructor is required to initialize the initial data members to the values passed by the main function.
Syntax:

ComplexSum(int real, int imaginary)  
{  
    realNum = real; 
    imaginaryNum = imaginary; 
}
You can also try this code with Online C++ Compiler
Run Code

C++ program to Add Two Complex Numbers

Code

#include <bits/stdc++.h>  
using namespace std;  
 
class ComplexSum {  
public:  
    int realNum; // To store real part of complex number  
    int imaginaryNum; // To store imaginary part of complex number  
 
    ComplexSum()  
    {  
    // Initially values are zero  
        realNum = 0;  
        imaginaryNum = 0;  
    }  
    ComplexSum(int r, int i)  
    {  
        realNum = r; //initialized during object creation  
        imaginaryNum = i; //initialized during object creation  
    }  
 
    ComplexSum addComplexNums(ComplexSum CS1, ComplexSum CS2)  
    {  
 
        ComplexSum Sum; // result object of complex class  
 
        // adding real parts of complex numbers  
        Sum.realNum = CS1.realNum + CS2.realNum;  
 
        // adding Imaginary parts of complex numbers  
        Sum.imaginaryNum = CS1.imaginaryNum + CS2.imaginaryNum;  
 
        // returning the sum  
        return Sum;  
    }  
};  
 
// Main Class  
int main()  
{  
 
    // First ComplexSum number  
    ComplexSum CS1(6, 9);  
 
    // printing first complex number  
    cout << "First Complex number : " << CS1.realNum  
         << " + i" << CS1.imaginaryNum << endl;  
 
    // Second ComplexSum number  
    ComplexSum CS2(4, 3);  
 
    // printing second complex number  
    cout << "Second Complex number : " << CS2.realNum  
         << " + i" << CS2.imaginaryNum << endl;  
 
    // to Store the sum  
    ComplexSum CS3;  
 
    // calling addComplexNums() method  
    CS3 = CS3.addComplexNums(CS1, CS2);  
 
    // printing the sum  
    cout << "Sum of the two complex numbers : "  
         << CS3.realNum << " + i"  
         << CS3.imaginaryNum;  
 
    cout << endl  
         << endl;  
    // Test for second input  
    // First Complex number  
    ComplexSum X(3, 5);  
 
    // printing first complex number  
    cout << "Fist Complex number : " << X.realNum  
         << " + i" << X.imaginaryNum << endl;  
 
    // Second Complex number  
    ComplexSum Y(10, 6);  
 
    // printing second complex number  
    cout << "Second Complex number : " << Y.realNum  
         << " + i" << Y.imaginaryNum << endl;  
 
    // to Store the sum  
    ComplexSum C;  
 
    // calling addComplexNums() method  
    C = C.addComplexNums(X, Y);  
 
    // printing the sum  
    cout << "Sum of the two complex numbers : "  
         << C.realNum << " + i"  
         << C.imaginaryNum;  
}  
You can also try this code with Online C++ Compiler
Run Code

Output:

First Complex number : 6 + i9
Second Complex number : 4 + i3
Sum of the two complex numbers : 10 + i12

Fist Complex number : 3 + i5
Second Complex number : 10 + i6
Sum of the two complex numbers: 13 + i11

 

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Check out this problem - Two Sum Problem

Related Article Swap Two Numbers Without Using Third Variable

FAQs

  1. How do you make a complex class in C++?
    In C++, we may design a complex number class that stores the real and imaginary parts of the complex number as member members. This class will have specific member methods used to manage it.
     
  2. Can we create a complex number from two floats?
    The complex() method accepts two int or float inputs. The first argument is the real part, while the second is the complex part.

Key Takeaways

In this article, we have learned to write a simple C++ program to add two complex numbers using a class. 

We hope that this blog has helped you enhance your knowledge of the C++ classes, constructors, and their implementations, check out our articles C++ Archives. Do upvote our blog to help other ninjas grow. Happy Coding!

Recommended Readings:

Live masterclass