In this article, you’ll learn how to make C++ program to add, multiply, divide and subtract two complex numbers using the concept of operator overloading with explanation.
What is Operating Overloading?
Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.
For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types. The advantage of Operators overloading is to perform different operations on the same operand.
C++ program to add, multiply, divide and subtract two complex numbers using the concept of operator overloading
#include<iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex() {}
Complex(double r, double i) {
real = r;
imag = i;
}
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
Complex operator-(Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
Complex operator*(Complex const &obj) {
Complex res;
res.real = real * obj.real - imag * obj.imag;
res.imag = real * obj.imag + imag * obj.real;
return res;
}
Complex operator/(Complex const &obj) {
Complex res;
double denominator = obj.real * obj.real + obj.imag * obj.imag;
res.real = (real * obj.real + imag * obj.imag) / denominator;
res.imag = (imag * obj.real - real * obj.imag) / denominator;
return res;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3, 2), c2(1, 7);
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
Complex c5 = c1 * c2;
Complex c6 = c1 / c2;
cout << "c1: ";
c1.display();
cout << "c2: ";
c2.display();
cout << "c1 + c2 = ";
c3.display();
cout << "c1 - c2 = ";
c4.display();
cout << "c1 * c2 = ";
c5.display();
cout << "c1 / c2 = ";
c6.display();
return 0;
}
In this program, we create a Complex class with two private data members real and imag representing the real and imaginary parts of a complex number, respectively. The class has four public member functions which overload the +, -, *, and / operators. These operators take a constant reference to another Complex object as their parameter and return a new Complex object that is the result of the operation.
The display() function is used to display the complex numbers in a readable format.
In the main() function, we create two Complex objects c1 and c2 with values (3, 2) and (1, 7) respectively. We then use the overloaded operators to perform arithmetic operations on these complex numbers and display the results using the display() function.
For example, c3 = c1 + c2 will add c1 and c2 and store the result in c3, which we then display using the display() function. Similarly, c5 = c1 * c2 will multiply c1 and c2 and store the result in c5, which we then display using the display() function.
The output of the program will be
c1: 3 + 2i
c2: 1 + 7i
c1 + c2 = 4 + 9i
c1 - c2 = 2 - 5i
c1 * c2 = -11 + 23i
c1 / c2 = 0.45283 - 0.226415i
As we can see, the program correctly performs arithmetic operations on the complex numbers using operator overloading.