|
|
 |
|
|
|
|
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. |
|
|
|
 |
|
|
|
|
b. Write a driver and test the IntArray class using your test data. |
|
|
|
|
|
|
|
|
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. |
|
|
|
 |
|
|
|
|
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 |
|
|
|
 |
|
|
|
|
is likely to be a value like 0.999999 rather than 1.0. |
|
|
|
 |
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
The product of 1/3 and 3/1 is 1/1 |
|
|
|
 |
|
|
|
|
At the very least, you should provide the following operations: |
|
|
|
 |
|
|
|
|
constructors for explicit as well as default initialization of Rational objects |
|
|
|
 |
|
|
|
|
arithmetic operations that add, subtract, multiply, and divide two Rational objects. These should be implemented as value-returning functions, each returning a Rational object. |
|
|
|
|
|