Question
C++ Add the ++ operator to the complex class and test. #include using namespace std; class Complex { private: float real; float imag; public: Complex()
C++
Add the ++ operator to the complex class and test.
#include
public: Complex() : real(0) , imag(0) { } void input() { cout << "Enter real and imaginary parts respectively: "; cin >> real; cin >> imag; } Complex operator-(Complex c2) /* Operator Function */ { Complex temp; temp.real = real - c2.real; temp.imag = imag - c2.imag; return temp; }
Complex operator*(Complex c2) /* Operator Function */ { Complex temp; temp.real = real * c2.real; temp.imag = imag * c2.imag; return temp; } Complex operator/(Complex c2) /* Operator Function */ { Complex temp; temp.real = ((real * c2.real) + (imag * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag)); temp.imag = ((real * c2.real) - (imag * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag)); ; return temp; } void output() { if (imag < 0) cout << "(" << real << imag << "i )"; else cout << "(" << real << "+" << imag << "i )"; } }; int main() { Complex c1, c2, result; cout << "Enter first complex number: "; c1.input(); cout << "Enter second complex number: "; c2.input(); /* In case of operator overloading of binary operators in C++ programming, the object on right * hand side of operator is always assumed as argument by compiler. */ /* c2 is furnised as an argument to the operator function. */ c1.output(); cout << "-"; c2.output();
result = c1 - c2; cout << "="; result.output(); cout << endl;
c1.output(); cout << "*"; c2.output(); cout << "="; result = c1 * c2; result.output(); cout << endl;
c1.output(); cout << "/"; c2.output(); cout << "="; result = c1 / c2; result.output(); cout << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started