# solution

Complex Class: Consider class Complex shown in Figures 10.14 â€“ 10.16. The class enables operations on so-called complex numbers. These are numbers of the form realPart + imaginaryPart * i, where i has the value v-1

a. Modify the class to enable input and output of complex numbers through the overloaded >> and << operators, respectively (you should remove the toString function from the class).

b. Overload the multiplication operator to enable multiplication of two complex numbers as in algebra.

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

*****Figure 10.14****

// Fig. 10.14: Complex.h

#include

#ifndef COMPLEX_H

#define COMPLEX_H

class Complex{

public:

explicit Complex(double = 0.0, double = 0.0); // constructor

Complex operator+(const Complex&) const; // addition

Complex operator-(const Complex&) const; // subtraction

std::string toString() const;

private:

double real; // real part

double imaginary; // imaginary part

};

#endif

**********************************************************************************************************************************************************************************************************************

***Fig 10.15******

// Fig 10.15: Complex.cpp

#include

#include â€œComplex.hâ€

using namespace std;

//Constructor

Complex::Complex(double realPart, double imaginaryPart)

: real{realPart}, imaginary{imaginaryPart} {}

Complex Complex::operator+(const Complex& operand2) const {

return Complex{real + operand2.real, imaginary + operand2.imaginary};

}

// subtraction operator

Complex Complex::operator-(const Complex& operand2) const {

return Complex{real – operand2.real, imaginary – operand2.imaginary};

}

// return string representation of a complex object in the form: (a, b)

string Complex::toString() const {

return â€œ(â€œs + to_string(real) + â€œ, â€œs + to_string(imaginary) + â€œ)â€s;

}

*********************************************************************************************************************************************************************************************************************

******Fig 10.16****

// Fig. 10.16: Assignment3.cpp

//Complex class test program

#include

#include â€œComplex.hâ€

using namespace std;

int main() {

Complex x;

Complex y{4.3, 8.2};

Complex z{3.3, 1.1};

cout << â€œx: â€œ << x.toString() << â€œny: â€œ << y.String()

<< â€œnz: â€œ << z;

x = y + z;

cout << â€œnnx = y + z:nâ€ << x.toString() << â€œ = â€œ << y.toString()

<< â€œ + â€œ << z.toString();

x = y + z;

cout << â€œnnx = y – z:nâ€ << x.toString() << â€œ = â€œ << y.toString()

<< â€œ – â€œ << z.toString() << endl;

}

##### Looking for a Similar Assignment? Our Experts can help. Use the coupon code SAVE30 to get your first order at 30% off!

Hi there! Click one of our representatives below and we will get back to you as soon as possible.

Chat with us on WhatsApp