CSIS 250 Spring 2000 Assignment #4
Date
Assigned:
March 1, 2000
Date
Due:
March 8, 2000 at 10:00 AM
Create
a class called CRational for performing arithmetic with fractions. Write
a main driver program and several test functions (one for each function member
and operator) to test your class.
Use
integer variables to represent the private data of the class-the numerator and
denominator. Provide a constructor function that enables an object of
this class to be initialized when it is declared. The constructor should
contain default values in case no initializers are provided and should store
the fraction in reduced form (i.e., the fraction 2/4 would be stored in the
object as 1 in the numerator and 2 in the denominator). Provide the
following functions/operators (public, private, or friend):
-
Default constructor
-
A constructor
with given values for numerator and denominator
-
A
constructor to create a random
fraction
-
gcd
function (private) to calculate the greatest common divisor of two integers
-
Addition operator
(+) for two CRational numbers. The result should be stored in reduced
form.
-
Subtraction operator
(-) for two CRational numbers. The result should be stored in reduced
form
-
Multiplication operator
(*) for two CRational numbers. The result should be stored in reduced
form
-
Division operator
(/) for two CRational numbers. The result should be stored in reduced
form
-
Insertion operator
(<<) for printing CRational numbers in the form a/b where a is the
numerator and b is the denominator
-
Function displayValue
for printing CRational numbers in floating-point format
Self-Evaluation Sheet
Component
|
Max
Score
|
Your Score
|
Default constructor
|
1
|
|
A constructor with
given values for numerator and denominator
|
1
|
|
A
constructor to create random fraction |
1 |
|
Operator +
|
1
|
|
Operator -
|
1
|
|
Operator *
|
1
|
|
Operator /
|
1
|
|
Operator <<
|
1
|
|
Function
displayValue
|
1
|
|
gcd function
|
1
|
|
Test functions (10)
for the above
|
10
|
|
Test runs (10) from
the test functions
|
10
|
|
Total
|
30
|
|
Submit the following:
- Properly commented
printed source code
- Properly labeled
printed output from test runs
- Self-evaluation
sheet