C++ allows us to specify more than one definition for a function or an operator this property is known as function overloading and operator overloading.
Operator overloading lets us define the meaning of an operator when applied to operand(s) of a class type. Well use of operator overloading can make our
programs easier to write and easier to read.
By operator overloading we can't define a new operator i.e.. we can't use ** and define it to find power.
Here is a program in which some arithmetic operators are overloaded for Complex Number class.
Operator overloading lets us define the meaning of an operator when applied to operand(s) of a class type. Well use of operator overloading can make our
programs easier to write and easier to read.
By operator overloading we can't define a new operator i.e.. we can't use ** and define it to find power.
Here is a program in which some arithmetic operators are overloaded for Complex Number class.
#include <iostream>
using namespace std;
class Complex
{
float real, imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
friend ostream& operator<<(ostream&, const Complex&);
friend istream& operator>>(istream&, Complex&);
friend Complex operator+(const Complex&, const Complex&);
Complex operator+(const Complex&);
friend Complex operator-(const Complex&, const Complex&);
Complex operator-(const Complex&);
friend Complex operator*(const Complex&, const Complex&);
Complex operator*(const Complex&);
friend Complex operator/(const Complex&, const Complex&);
Complex operator/(const Complex&);
class DivideByZero {};
};
Complex operator*(const Complex& lhs, const Complex& rhs)
{
Complex result;
cout << "Friend * Function Is Called." << endl;
result.real = (lhs.real * rhs.real) - (lhs.imag * rhs.imag);
result.imag = (lhs.real * rhs.imag) + (lhs.imag * rhs.real);
return result;
}
Complex Complex::operator*(const Complex& rhs)
{
Complex result;
cout << "Member * Function Is Called." << endl;
result.real = (real * rhs.real) - (imag * rhs.imag);
result.imag = (real * rhs.imag) + (imag * rhs.real);
return result;
}
Complex operator/(const Complex& lhs, const Complex& rhs)
{
Complex result;
cout << "Friend / Function Is Called." << endl;
float tmp = (lhs.imag * lhs.imag) + (rhs.real * rhs.real);
if (tmp == 0.0) {
throw Complex::DivideByZero();
}
result.real = ((lhs.real * rhs.real) + (lhs.imag * rhs.imag)) / tmp;
result.imag = ((lhs.imag * rhs.real) - (lhs.real * rhs.imag)) / tmp;
return result;
}
Complex Complex::operator/(const Complex& rhs)
{
Complex result;
cout << "Member / Function Is Called." << endl;
float tmp = (imag * imag) + (rhs.real * rhs.real);
if (tmp == 0.0) {
throw Complex::DivideByZero();
}
result.real = ((real * rhs.real) + (imag * rhs.imag)) / tmp;
result.imag = ((imag * rhs.real) - (real * rhs.imag)) / tmp;
return result;
}
Complex operator+(const Complex& lhs, const Complex& rhs)
{
Complex result;
cout << "Friend + Function Is Called." << endl;
result.real = lhs.real + rhs.real;
result.imag = lhs.imag + rhs.imag;
return result;
}
Complex Complex::operator+(const Complex& rhs)
{
Complex result;
cout << "Member + Function Is Called." << endl;
result.real = real + rhs.real;
result.imag = imag + rhs.imag;
return result;
}
Complex operator-(const Complex& lhs, const Complex& rhs)
{
Complex result;
cout << "Friend - Function Is Called." << endl;
result.real = lhs.real - rhs.real;
result.imag = lhs.imag - rhs.imag;
return result;
}
Complex Complex::operator-(const Complex& rhs)
{
Complex result;
cout << "Member - Function Is Called." << endl;
result.real = real - rhs.real;
result.imag = imag - rhs.imag;
return result;
}
ostream& operator<<(ostream& out, const Complex& rhs)
{
out << "(" <>(istream& in, Complex& rhs)
{
in >> rhs.real >> rhs.imag;
return in;
}
int main()
{
float r, i;
cout << "---Arithmetic Operations---" << endl;
cout << "Enter The First Complex Number: " << endl;
cout << "Enter Real Part: " << endl;
cin >> r;
cout << "Enter Imaginary Part: " << endl;
cin >> i;
Complex c1(r, i);
cout << "Enter The Second Complex Number: " << endl;
cout << "Enter Real Part: " << endl;
cin >> r;
cout << "Enter Imaginary Part: " << endl;
cin >> i;
Complex c2(r, i);
cout << "The Result Is Of c1 + c2: " << c1 + c2 << endl;
cout << "The Result Is Of c1 - c2: " << c1 - c2 << endl;
cout << "The Result Is Of c1 * c2: " << c1 * c2 << endl;
try {
cout << "The Result Is Of c1 + c2:" << c1 / c2 << endl;
}
catch (Complex::DivideByZero) {
cout << "Exception: Can't Divide By Zero." << endl;
return -1;
}
cout << "---Automatic Type Conversion Testing---" << endl;
Complex c3(2,5);
cout << "Addition Of c3 + 2: " << c3 + 2 << endl;
cout << "Multiplication Of c3 * 0: " << c3 * 0.0 << endl;
return 0;
}
---Arithmetic Operations--- Enter The First Complex Number: Enter Real Part: 2 Enter Imaginary Part: 3 Enter The Second Complex Number: Enter Real Part: 4 Enter Imaginary Part: 5 Member + Function Is Called. The Result Is Of c1 + c2: (6) + (8i) Member - Function Is Called. The Result Is Of c1 - c2: (-2) + (-2i) Member * Function Is Called. The Result Is Of c1 * c2: (-7) + (22i) Member / Function Is Called. The Result Is Of c1 + c2:(0.92) + (0.08i) ---Automatic Type Conversion Testing--- Member + Function Is Called. Addition Of c3 + 2: (4) + (5i) Member * Function Is Called. Multiplication Of c3 * 0: (0) + (0i) --------------------------------