Consider class Complex shown in Figs. 11.1–11.3. The class enables opera- tions on so-called complex numbers. These are numbers of the form realPart + imaginaryPart * i, where i has the value ?-1
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.
Fig. 11.1 Complex class definition.
// Complex class definition.
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
void print() const; // output
private:
double real; // real part
double imaginary; // imaginary part
}; // end class Complex
#endif
Fig. 11.2 | Complex class member-function definitions.
// Complex class member-function definitions.
#include
#include "Complex.h" // Complex class definition
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-
// display a Complex object in the form: (a, b)
void Complex::print() const
{
cout << '(' << real << ", " << imaginary << ')';
} // end function print
Fig. 11.3 | Complex numbers.
// 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.print();
cout << "\ny: ";
y.print();
cout << "\nz: ";
z.print();
x = y + z;
cout << "\n\nx = y + z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " + ";
z.print();
x = y - z;
cout << "\n\nx = y - z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " - ";
z.print();
cout << endl;
} // end main
x: (0, 0)
y: (4.3, 8.2)
z: (3.3, 1.1)
x = y + z:
(7.6, 9.3) = (4.3, 8.2) + (3.3, 1.1)
x = y - z:
(1, 7.1) = (4.3, 8.2) - (3.3, 1.1)
// Complex class definition.
#ifndef COMPLEX_H
#define COMPLEX_H
#include
using namespace std;
class Complex
{
friend ostream &operator<<( ostream &, const Complex & );
friend istream &operator>>( istream &, Complex & );
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex& ) const; // addition
Complex operator-( const Complex& ) const; // subtraction
Complex operator*( const Complex& ) const; // multiplication
Complex& operator=( const Complex& ); // assignment
bool operator==( const Complex& ) const;
bool operator!=( const Complex& ) const;
private:
double real; // real part
double imaginary; // imaginary part
}; // end class Complex
#endif
// Complex class member-function definitions.
#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-
// Overloaded multiplication operator
Complex Complex::operator*( const Complex &operand2 ) const
{
return Complex(
( real * operand2.real ) + ( imaginary * operand2.imaginary ),
( real * operand2.imaginary ) + ( imaginary * operand2.real ) );
} // end function operator*
// 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 );
} // end function operator==
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;
} // end function operator<<
istream& operator>>( istream &input, Complex &complex )
{
input.ignore(); // skip (
input >> complex.real;
input.ignore( 2 ); // skip ',' and space
input >> complex.imaginary;
input.ignore(); // skip )
return input;
} // end function operator>>
// Complex class test program.
#include
#include "Complex.h"
using namespace std;
int main()
{
Complex x, y( 4.3, 8.2 ), z( 3.3, 1.1 ), k;
cout << "Enter a complex number in the form: (a, b)\n? ";
cin >> k; // demonstrating overloaded >>
cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\nk: "
<< k << '\n'; // demonstrating overloaded <<
x = y + z; // demonstrating overloaded + and =
cout << "\nx = y + z:\n" << x << " = " << y << " + " << z << '\n';
x = y - z; // demonstrating overloaded - and =
cout << "\nx = y - z:\n" << x << " = " << y << " - " << z << '\n';
x = y * z; // demonstrating overloaded * and =
cout << "\nx = y * z:\n" << x << " = " << y << " * " << z << "\n\n";
if ( x != k ) // demonstrating overloaded !=
cout << x << " != " << k << '\n';
cout << '\n';
x = k;
if ( x == k ) // demonstrating overloaded ==
cout << x << " == " << k << '\n';
} // end main
Enter a complex number in the form: (a, b)
? (0, 0)
x: (0, 0)
y: (4.3, 8.2)
z: (3.3, 1.1)
k: (0, 0)
x = y + z:
(7.6, 9.3) = (4.3, 8.2) + (3.3, 1.1)
x = y - z:
(1, 7.1) = (4.3, 8.2) - (3.3, 1.1)
x = y * z:
(23.21, 31.79) = (4.3, 8.2) * (3.3, 1.1)
(23.21, 31.79) != (0, 0)
(0, 0) == (0, 0)
You might also like to view...
To indicate the passing of an array in a prototype or header function you must have:
A. data type variable name [ ]. B. variable name data type [ ]. C. *Parentheses ( ). D. No indication is necessary. The program intuitively knows what you are doing.
Font styles are font attributes such as bold, italic, and underline. ____________________
Answer the following statement true (T) or false (F)