Question: C++ For this assignment, you will make changes to the previous coding to now include the following upgrades: Modify the class to enable input and

C++ For this assignment, you will make changes to the previous coding to now include the following upgrades:

Modify the class to enable input and output of complex numbers via overloaded + and - operators, respectively (addition and subtraction).

Modify the class to enable input and output of complex numbers via overloaded = and * operators, respectively (assignment and multiplication).

Modify the class to enable input and output of complex numbers via overloaded >> and << operators, respectively (you should remove the print function from the class). Use friend functions to accomplish this.

Overload the == and != operators to allow comparisons of complex numbers.

Add friend member functions to enable input and output of complex numbers via overloaded >> and << operators, respectively (you should remove the print function from the class).

Change the Complex class definition, the Complex class member-function definitions, and modify the driver program as appropriate.

#include #include #include

using std::string; using std::ostringstream;

class Complex { private: double realPart , imaginaryPart; public: Complex(double real=0 , double imaginary=0) { realPart = real; imaginaryPart = imaginary; }

Complex add(const Complex &c) { double new_real, new_imaginary; new_real = realPart + c.realPart; new_imaginary = imaginaryPart + c.imaginaryPart;

Complex sum(new_real, new_imaginary); return sum; }

Complex subtract(const Complex &c) { return Complex(realPart - c.realPart, imaginaryPart - c.imaginaryPart); }

Complex multiply(const Complex &c) { return Complex(realPart * c.realPart - imaginaryPart * c.imaginaryPart, imaginaryPart * c.realPart + realPart * c.imaginaryPart); }

void setComplexNumber(double real , double imaginary) {

realPart = real; imaginaryPart = imaginary;

}

string toString() { ostringstream convert; convert << realPart; string str1=convert.str();

convert << imaginaryPart; string str2=convert.str();

return str1 + " + " +str2 + "i"; }

int main() { for (double i = 1; i < 10; ++ i) {

Complex y{i * 2.7, i + 3.2};

Complex z{i * 6, i + 8.3};

Complex x;

Complex k;

std::cout << "Enter a complex number in the form: (a, b) ? ";

std::cin >> k; // demonstrating overloaded >>

std::cout << "x: " << x << " y: " << y << " z: " << z << " k: "

<< k << ' '; // demonstrating overloaded <<

x = y + z; // demonstrating overloaded + and =

std::cout << " x = y + z: " << x << " = " << y << " + " << z << ' ';

x = y - z; // demonstrating overloaded - and =

std::cout << " x = y - z: " << x << " = " << y << " - " << z << ' ';

x = y * z; // demonstrating overloaded * and =

std::cout << " x = y * z: " << x << " = " << y << " * " << z << " ";

if (x != k) { // demonstrating overloaded != std::cout << x << " != " << k << ' '; } std::cout << ' '; x = k;

if (x == k) { // demonstrating overloaded ==

std::cout << x << " == " << k << ' ';

}

std::cout << std::endl; } }

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!