C++ Program
3
rational.cpp
Build an ADT for rational numbers.
A rational number consists of two integer pieces a numerator, say a, and a
denominator, say b, representing a fraction a / b.
The rational number is said to be in proper form if the greatest common divisor
of both the numerator and denominator is 1 and the denominator is positive.
The result of operations between two rational numbers a / b and c / d follows.
- addition (a d + b c) / (b d)
- subtraction (a d - b c) / (b d)
- multiplication (a c) / (b d)
- division (a d) / (b c)
- equivalence if ( a d ) == ( b c )
- less than a / b < c / d if (a d) < (b c) with rationals in proper form
(denominators must always be positive - negative numbers have negative
numerators - this has an implication for how you store all rationals)
You should name the class Rational. You should have "four" constructors (perhaps
by overloading, or perhaps by default values, or perhaps by both as appropriate): default
(initial value zero, 0 / 1), input using a single integer (representing the
integer using 1 for the denominator), input using two integers (representing the
numerator and denominator respectively), and a copy constructor.
You should have two accessor functions, getNum() for the numerator and getDenom() for
the denominator.
You should overload the assignment operators (which should be member functions):
=, +=, -=, *=, /=
You should overload binary operators (which should be member functions): +, -, *, /
You should overload the unary operator (which should be a member function): -
You should overload the output operator (which should be a non-member function,
but may be a friend): <<
You should overload the comparison operators (which should be member functions): <, <=, >, >=, ==, !=
You should overload the absolute value function (which should be a non-member
function, but may be a friend): abs()
In addition to your *.cpp and *.h files turn in a main program that exercises
the class Rational. That is, a main program that runs through a selection of
arithmetic and logical operations and uses rational input and output.
What to turn in
- Provide, as an e-copy in the u:\program3 directory, a pseudocode design of your program.
- Be sure your program rational.h, rational.cpp and testRational.exe are saved under your
u:\program3
folder
Grading Criteria
- 80 points maximum
- 70 points – Design and Output
Correctness
- You should name the class Rational.
You should have four constructors: default (initial value zero, 0 /
1), input using a single integer (representing the integer using 1 for the
denominator), input using two integers (representing the numerator and
denominator respectively), and a copy constructor.
- You should have two accessor
functions, getNum() for the numerator and getDenom() for the denominator.
- You should overload the assignment
operators: =, +=, -=, *=, /=
- You should overload binary operators: +, -, *, /
- You should overload the unary
operator: -
- You should overload the output
operator: <<
- You should overload the comparison
operators: <,
<=, >, >=, ==, !=
- You should overload the absolute
value function: abs( )
- 5 points - Comments
- Complete and correct header comments,
declaration, and in-line comments.
- 5 points - creation of Visual C++
project
- folder u:\program3created correctly
on your network u: drive
- all files are saved correctly under
u:\program3 folder
- No compiler warning messages