168k views
1 vote
Consider the class Complex (see problem 11.13, page 633-635). The class enables operations on so called complex numbers. a) Modify the class to enable input and output of complex number through the overloaded << and >> operators (you should modify or remove the print function from the class). b) Overload the multiplication * and the division / operators. c) Overload the

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

// Complex.h

#ifndef COMPLEX_H

#define COMPLEX_H

#include <iostream>

using namespace std;

class Complex

{

friend ostream &operator<<(ostream &, const Complex &);

friend istream &operator>>(istream &, Complex &);

private:

// Declare variables

double real;

double imaginary;

public:

// Constructor

Complex(double = 0.0, double = 0.0);

// Addition operator

Complex operator+(const Complex&) const;

// Subtraction operator

Complex operator-(const Complex&) const;

// Multiplication operator

Complex operator*(const Complex&) const;

// Equal operator

Complex& operator=(const Complex&);

bool operator==(const Complex&) const;

// Not Equal operator

bool operator!=(const Complex&) const;

};

#endif

// Complex.cpp

// Header file section

#include "Complex.h"

#include <iostream>

using namespace std;

// Constructor

Complex::Complex( double realPart, double imaginaryPart )

: real( realPart ),

imaginary( imaginaryPart )

{

// empty body

}

// Addition (+) operator

Complex Complex::operator+( const Complex &operd2 ) const

{

return Complex( real + operd2.real,

imaginary + operd2.imaginary );

}

// Subtraction (-) operator

Complex Complex::operator-( const Complex &operd2 ) const

{

return Complex( real - operd2.real,

imaginary - operd2.imaginary );

}

// Multiplication (*) operator

Complex Complex::operator*( const Complex &operd2 ) const

{

return Complex(

( real * operd2.real ) + ( imaginary * operd2.imaginary ),

( real * operd2.imaginary ) + ( imaginary * operd2.real ) );

}

// Overloaded = operator

Complex& Complex::operator=( const Complex &right )

{

real = right.real;

imaginary = right.imaginary;

return *this; // enables concatenation

} // end function operator=

bool Complex::operator==( const Complex &right ) const

{

return ( right.real == real ) && ( right.imaginary == imaginary )

? true : false;

}

bool Complex::operator!=( const Complex &right ) const

{

return !( *this == right );

} // end function operator!=

ostream& operator<<( ostream &output, const Complex &complex )

{

output << "(" << complex.real << ", " << complex.imaginary << ")";

return output;

}

istream& operator>>( istream &input, Complex &complex )

{

input.ignore();

input >> complex.real;

input.ignore( 2 );

input.ignore();

return input;

}

// ComplexMain.cpp

// Header file section

#include <iostream>

#include "Complex.h"

using namespace std;

// main method

int main()

{

// Declare complex numbers

Complex x, y(4.3, 8.2), z(3.3, 1.1), k;

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

// Demonstrating overloaded

// Accept a complex number

cin >> k;

// Display complex numbers

cout << "x: " << x

<< "\\y: " << y

<< "\\z: " << z

<< "\\k: " << k << '\\';

// Operator overloading

// Adding two complex numbers

x = y + z;

cout << "\\x = y + z:\\"

<< x << " = "

<< y << " + " << z << '\\';

// Subtract two complex numbers

x = y - z;

cout << "\\x = y - z:\\"

<< x << " = " << y << " - " << z << '\\';

// Multiply two complex numbers

x = y * z;

cout << "\\x = y * z:\\"

<< x << " = " << y << " * " << z << "\\\\";

if (x != k)

{

cout << x << " != " << k << '\\';

}

cout << '\\';

x = k;

if (x == k)

{

cout << x << " == " << k << '\\';

}

return 0;

}

User Liqang Liu
by
4.7k points