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} {}

// addition operator

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? Get Expert Help at an Amazing Discount!"
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