In this tutorial, we will create a program that adds two complex numbers (a1 + ib1) and (a2 + ib2) by utilizing a class.
For example
Input: 4 + i5 and 8 + i9
Here when a1 is equal to 4 and a2 is equal to 8. By adding a1 and a2 together, the result is (8 + 4) which equals 12.
Furthermore, assuming b1 equals 5 and b2 equals 9. When we sum up b1 and b2, the result is (5 + 9) which equals 14.
Output: 9 + i14
Input: 2 + i7 and 10 + i6
Here a1 equals 2 while a2 equals 10. When we sum up a1 and a2, the result is (2 + 10) which equals 12.
Additionally, assuming b1 equals 7 and b2 equals 6. When we sum up b1 and b2, the result is (7 + 6) = 13.
Output: 12 + i13
Class construction
Initially, let's establish a class dedicated to complex numbers. It is evident that a complex number comprises a real component (a1) and an imaginary component (b1) based on observations.
We require two data attributes to signify the complex numbers.
Below is the class structure:
class Complex
{
public:
int real; // To store real part of complex number
int imaginary; // To store imaginary part of complex number
}
Constructors
Two different constructors are going to be created in order to set the initial values of the attributes within the Complex class. The first one
- will be a constructor that doesn't require any parameters.
We require a constructor without parameters to set the initial values of the attributes to zero.
Below is the layout of non-parameterized constructor:
Complex()
{
real = 0;
imaginary = 0;
}
- Parameterized constructor
We require a parameterless constructor to set the data attributes to the values provided by the main function.
The following is the layout of a parameterized constructor:
Complex(int r, int i)
{
real = r; // r is initialized during object creation
imaginary = i; // i is initialized during object creation
}
Algorithm
When performing the addition of two complex numbers, it is necessary to instantiate two instances of the Complex class and assign the respective values to them. Subsequently, a third object should be created to hold the outcome of the addition.
C++ code
// C++ program to Add two complex numbers
#include <bits/stdc++.h>
using namespace std;
class Complex {
public:
int real; // To store real part of complex number
int imaginary; // To store imaginary part of complex number
Complex()
{
// Initial values are zero
real = 0;
imaginary = 0;
}
Complex(int r, int i)
{
real = r; // r is initialized during object creation
imaginary = i; // i is initialized during object creation
}
Complex addComplexNumber(Complex C1, Complex C2)
{
Complex res; // result object of complex class
// adding real part of complex numbers
res.real = C1.real + C2.real;
// adding Imaginary part of complex numbers
res.imaginary = C1.imaginary + C2.imaginary;
// returning the sum
return res;
}
};
// Main Class
int main()
{
// First Complex number
Complex C1(4, 5);
// printing first complex number
cout << "Complex number 1 : " << C1.real
<< " + i" << C1.imaginary << endl;
// Second Complex number
Complex C2(8, 9);
// printing second complex number
cout << "Complex number 2 : " << C2.real
<< " + i" << C2.imaginary << endl;
// for Storing the sum
Complex C3;
// calling addComplexNumber() method
C3 = C3.addComplexNumber(C1, C2);
// printing the sum
cout << "Sum of complex number : "
<< C3.real << " + i"
<< C3.imaginary;
cout << endl
<< endl;
// Test for second input
// First Complex number
Complex A(2, 7);
// printing first complex number
cout << "Complex number 1 : " << A.real
<< " + i" << A.imaginary << endl;
// Second Complex number
Complex B(10, 6);
// printing second complex number
cout << "Complex number 2 : " << B.real
<< " + i" << B.imaginary << endl;
// for Storing the sum
Complex C;
// calling addComplexNumber() method
C = C.addComplexNumber(A, B);
// printing the sum
cout << "Sum of complex number : "
<< C.real << " + i"
<< C.imaginary;
}
Output
Complex number 1 : 4 + i5
Complex number 2 : 8 + i9
Sum of complex number : 12 + i14
Complex number 1 : 2 + i7
Complex number 2 : 10 + i6
Sum of complex number : 12 + i13
Time complexity
The time complexity is O(1) as we have to call the function addComplexNumber() to add the two complex numbers.