Lab
Test 5

1
A rational number is a number that can be represented as the quotient of
two integers..

a)
Define a C++ class called Rational with two private integer members, numerator and denominator.

b)
Include a constructor with two arguments that can be used to set the
member variables of an object to any legitimate values.

c)
Include a default constructor that initializes an object to 0.

d)
Overload the output operator <<.

e)
Overload the operator +.

f)
Overload the operator *.

g)
Write your own test program.

2
A complex number (複數) is a number of the form

*a + b i*

where for our purposes, *a* and *b* are numbers of type double, and *i* is a number
that represents the quantity. *a* is called the
real part (實部) and *b*
the imaginary part (虛部).

a)
Define a C++ class called Complex with two private double members, real and imaginary.

b)
Include a constructor with two arguments that can be used to set the
member variables of an object to any legitimate values.

c)
Include a default constructor that initializes an object to 0.

d)
Overload the output operator <<.

e)
Overload the operator +. Note that (*a
+ b i*) + (*c
+ d i*) = (*a
+ c*) + (* b + d *)* i*

f)
Overload the operator *. Note that (*a
+ b i*) * (*c
+ d i*) = (*ac
- bd*) + (* bc + ad *)* i*

g)
Write your own test program.