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
-
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.
-
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: