Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

4. Are there any disadvantages?

Answered: 1 week ago

Question

What is IUPAC system? Name organic compounds using IUPAC system.

Answered: 1 week ago

Question

What happens when carbonate and hydrogen react with carbonate?

Answered: 1 week ago

Question

Be familiar with the five basic ways to manage demand.

Answered: 1 week ago