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 using namespace std; class Complex { private: float real; float imag;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!