Question
Consider class Complex shown in Figures 1, 2, and 3. The class enables operations on so-called complex numbers. These are numbers of the form realPart + imaginaryPart * i, where i has the value a. Modify the class to enable input and output of complex numbers through the overloaded >> and << operators, respectively (you should remove the print 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. d. Overload the + operator to allow the addition of a double variable plus a complex number
Consider class Complex shown in Figures 1, 2, and 3. The class enables operations on so-called complex numbers. These are numbers of the form realPart + imaginaryPart * i, where i has the value a. Modify the class to enable input and output of complex numbers through the overloaded > > and
Explanation / Answer
Overloading the multiplication operator to enable multiplication of two complex numbers as in algebra:::: #ifndef COMPLEX_H #define COMPLEX_H #include #include class Complex { friend ostream &operator( istream &, Complex & ); public: Complex( double = 0.0, double = 0.0); //Constructor Complex operator+( const Complex & ) const; //addition Complex operator-( const Complex & ) const; //subraction Complex operator*( const Complex & ) const; //multiplication bool operator==(const Complex & ) const; // equal bool operator!=(const Complex & ) const; // not equal private: double real; // real part double imaginary; // imaginary part }; //end class Complex #endif #include #include #include #include "Complex.h" using namespace std; //Constructor Complex::Complex( double realPart, double imaginaryPart) :real( realPart), imaginary(imaginaryPart) { //empty body } //end Complex constructor //addition operator Complex Complex ::operator+( const Complex &operand2) const { return Complex ( real + operand2.real, imaginary + operand2.imaginary ); }//end function operator+ //subtraction operator Complex Complex::operator-( const Complex & operand2 ) const { return Complex( real - operand2.real, imaginary - operand2.imaginary ); } //end function operator- //multiplication operator Complex Complex::operator*( const Complex & operand2) const { return Complex(real * operand2.real, imaginary * operand2.imaginary ); } //end function operator* //equals operator bool Complex::operator==( const Complex &operand2) const { bool r = false; if (real == operand2.real && imaginary == operand2.imaginary) { r = true; } return r; } //not equals operator bool Complex::operator!=( const Complex &operand2) const { return !(*this == operand2); } //overloaded output operator ostream &operator