Homework 3

CS 23

60 pts., final code due Mar. 19

A Rational Class

A rational number is the ratio of two integers. Design a rational class with the following operations:

All rationals should be stored in reduced form, using a private method to reduce to lowest terms (see exercise 19 in Chapter 2).

The constructor should take zero, one, or two arguments (initialize to 0, an integer, or a rational, respectively). Again, use default arguments.

The rational class should be implemented in the files rational.cc and rational.h, as we've been doing in class.

Write a small driver program to demonstrate and test the rational class. This program should be put in the file main.cc. In addition to testing the obvious (that rationals can be added, subtracted, displayed, etc.), the program should test the following conditions:

  1. Attempting to set the denominator to zero.

  2. Demonstrate that rationals are stored in reduced form.

  3. That a rational may be properly subtracted from itself.

Questions to clarify the program requirements should be asked in class on Friday, Mar. 7.

Design Review Turn-In Instructions

This is optional. If you elect this option, it will count for 30% of your homework grade. The due date is Mar. 12.

Turn in your design of the rational class. This consists of the file rational.h and a stub version of the file rational.cc Each of the stub functions should be documented with pre- and post-conditions.

Here's what I mean be a stub function:

// Prototype from .h file:

char *strcpy(char *dest, char *src);

// Stub function from .cc file:

// Pre-Conditions:
// Post-Conditions:
char *strcpy(char *dest, char *src)
In words, a stub function has no body.

Those who turn-in design reviews will be e-mailed my versions of the two design files. You will be on your honor to not share these files with those not electing this option.

Final Code Turn-In Instructions

E-mail me a script showing a compilation and sample run. Turn-in printed copies of the three source files in class.

Thomas P. Kelliher
Wed Mar 5 10:51:55 EST 1997
Tom Kelliher