< previous page page_902 next page >

Page 902
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Implement each member function as it would appear in the implementation file. To halt the program, use the exit function supplied by the C++ standard library through the header file stdlib.h (see Appendix C).
11. a. Design the data sets necessary to thoroughly test the IntArray class of Programming Warm-Up Exercise 10.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Write a driver and test the IntArray class using your test data.
Programming Problems
1. A rational number is a number that can be expressed as a fraction whose numerator and denominator are integers. Examples of rational numbers are 0.75 (which is ¾ ) and 1.125 (which is 9/8). The value p is not a rational number; it cannot be expressed as the ratio of two integers.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Working with rational numbers on a computer is often a problem. Inaccuracies in floating point representation can yield imprecise results. For example, the result of the C++ expression
1.0 / 3.0 * 3.0
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
is likely to be a value like 0.999999 rather than 1.0.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Design, implement, and test a Rational class that represents a rational number as a pair of integers instead of a single floating point number. The Rational class should have two class constructors. The first one lets the client specify an initial numerator and denominator. The otherthe default constructorcreates the rational number 0, represented as a numerator of 0 and a denominator of 1. The segment of client code
Rational num1(1, 3);
Rational num2(3, 1);
Rational result;

cout << The product of ;
num1. Write ();
cout <<  and ;
num2 .Write ();
cout <<  is ;
result = num1.MultipliedBy(num2);
result.Write();
would produce the output
The product of 1/3 and 3/1 is 1/1
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
At the very least, you should provide the following operations:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
constructors for explicit as well as default initialization of Rational objects
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
arithmetic operations that add, subtract, multiply, and divide two Rational objects. These should be implemented as value-returning functions, each returning a Rational object.

 
< previous page page_902 next page >