Consider class Complex shown in
Figs. 11.19
11.21
. The class enables operations on so-called complex numbers. These are numbers of the form realPart + imaginaryPart * i, where i*i has the value -1,
-
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).
-
Overload the multiplication operator to enable multiplication of two complex numbers as in algebra.
-
Overload the == and != operators to allow comparisons of complex numbers.
Figure 11.19. Complex class definition.
1 // Fig. 11.19: Complex.h
2 // Complex class definition.
3 #ifndef COMPLEX_H
4 #define COMPLEX_H
5
6 class Complex
7 {
8 public:
9 Complex( double = 0.0, double = 0.0 ); // constructor
10 Complex operator+( const Complex & ) const; // addition
11 Complex operator-( const Complex & ) const; // subtraction
12 void print() const; // output
13 private:
14 double real; // real part
15 double imaginary; // imaginary part
16 }; // end class Complex
17
18 #endif
Figure 11.20. Complex class member-function definitions.
1 // Fig. 11.20: Complex.cpp
2 // Complex class member-function definitions.
3 #include <iostream>
4 using std::cout;
5
6 #include "Complex.h" // Complex class definition
7
8 // Constructor
9 Complex::Complex( double realPart, double imaginaryPart )
10 : real( realPart ),
11 imaginary( imaginaryPart )
12 {
13 // empty body
14 } // end Complex constructor
15
16 // addition operator
17 Complex Complex::operator+( const Complex &operand2 ) const
18 {
19 return Complex( real + operand2.real,
20 imaginary + operand2.imaginary );
21 } // end function operator+
22
23 // subtraction operator
24 Complex Complex::operator-( const Complex &operand2 ) const
25 {
26 return Complex( real - operand2.real,
27 imaginary - operand2.imaginary );
28 } // end function operator-
29
30 // display a Complex object in the form: (a, b)
31 void Complex::print() const
32 {
33 cout << '(' << real << ", " << imaginary << ')';
34 } // end function print
Figure 11.21. Complex numbers.
1 // Fig. 11.21: fig11_21.cpp
2 // Complex class test program.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include "Complex.h"
8
9 int main()
10 {
11 Complex x;
12 Complex y( 4.3, 8.2 );
13 Complex z( 3.3, 1.1 );
14
15 cout << "x: ";
16 x.print();
17 cout << "\ny: ";
18 y.print();
19 cout << "\nz: ";
20 z.print();
21
22 x = y + z;
23 cout << "\n\nx = y + z:" << endl;
24 x.print();
25 cout << " = ";
26 y.print();
27 cout << " + ";
28 z.print();
29
30 x = y - z;
31 cout << "\n\nx = y - z:" << endl;
32 x.print();
33 cout << " = ";
34 y.print();
35 cout << " - ";
36 z.print();
37 cout << endl;
38 return 0;
39 } // end main
Solution:
// Exercise 11.13 Solution: Complex.h
// Complex class definition.
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::ostream;
using std::istream;
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
// Exercise 11.13 Solution: Complex.cpp
// Complex class member-function definitions.
#include "Complex.h"
#include <iostream>
using std::ostream;
using std::istream;
// 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 )
? true : false;
} // 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>>
// Exercise 11.13 Solution: ex11_13.cpp
// Complex class test program.
#include <iostream>
using std::cout;
using std::cin;
#include "Complex.h"
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';
return 0;
} // end main