Description
One of the tools you can use within C++ is that of overloading operators.
This is a highly controversial tool in the Object-Oriented community as some
believe it is a dangerous tool while others believe it allows a developer to
use built-in constructs in a reasonable manner. One of the most common
examples of the usefulness of overloading operators is a Rational number
class (fractions!).
Assignment
You have been tasked with the responsibility of developing a Rational number
class to be used in your company's educational software. It will be used in
the development of a software application that will allow students to test their
knowledge and understanding of fractions. Your job is to implement the
Rational class.
Rational Class
A rational number is a fraction (not a "mixed"-number) with a single integer
for the numerator and a single integer for the denominator. Denominators
cannot be zero. Negative Rational numbers are indicated by the numerator
possessing a negative value. Default Rational numbers are (1/1). They are
displayed with a slash between the numerator and denominator (i.e. 3/4, where
3 is the numerator and 4 is the denominator). Rational numbers are ALWAYS
stored in reduced form (therefore after each computation - you must be sure
to reduce the result, i.e. a reduced Rational number is 3/4 instead of 6/8).
The Rational class has the following:
- Numerator - integer, may be positive, negative, or zero
- Denominator - integer, may only be positive,
If a negative denominator is supplied either by the user or by a
computation, the negation should be applied to the numerator.
If a denominator of zero is supplied, the value 1 should be used.
- Default constructor - builds a default Rational number with value 1/1
- Constructor - takes 2 parameters (numerator, denominator) [you may
combine the two constructors if you like]
- GetNumerator - accessor for numerator
- GetDenominator - accessor for denominator
- SetRational(numerator, denominator) - a single mutator for
both (Why would we choose to do this?)
- Overloaded operator+ - addition
- Overloaded operator- - subtraction
- Overloaded operator* - multiplication
- Overloaded operator/ - division
- Overloaded operator< - less than
- Overloaded operator<= - less than or equal to
- Overloaded operator> - greater than
- Overloaded operator>= - greater than or equal to
- Overloaded operator== - is equal to
- Overloaded operator!= - is NOT equal to
- Overloaded operator++ - adds (1/1) to the Rational number, returning
result
- Overloaded operator++ (postfix) - adds (1/1) to the Rational number,
returning the value BEFORE the increment
- Overloaded operator-- - substracts (1/1) from the Rational number,
returning result
- Overloaded operator-- (postfix) - subtracts (1/1) from the Rational
number, returning the value BEFORE the decrement
- Overloaded operator<< - displays the Rational number as n/d,
this should not display an endl after the number
- Overloaded operator>> - reads in a Rational number from the
user in the form n/d (i.e. the user enters the slash)
You should add any other "helper" functions that you need to your class, Note
that the parameters, types and return types have not been specified for you -
this is intentional. It is your choice which of these should be non-members,
members or friends. Implementation of ALL overloaded operators must be in the
Rational.cpp file. Prototypes of ALL overloaded operators (in the class or
not) must be in the Rational.h file.
You will be evaluated on your code reuse in this project - there are many
opportunities to reuse the code you will be developing - take advantage of
them!
Files
You can copy all necessary files from the following directory:
/afs/umbc.edu/users/d/a/dana3/pub/CMSC202/hw2/
Hw2.cpp
You have been supplied with a main file that will allow you to test your
Rational class. It is a menu-driven application that will request input
from the user and run tests on values supplied by the user. This file is
named Hw2.cpp
You MUST NOT change this file. For your own testing purposes, you may
change the file by commenting-out code so that you can develop your Rational
class incrementally. However, before submitting your work - be sure that you
are testing your class with the original file.
Your Rational class will have to adhere to the class and method naming scheme
defined in this document (and used in Hw2.cpp). If you change the names of
any methods or the calls to any operators, you will lose all credit for the
tests involved.
makefile
The makefile supplied will allow you to build and test your application. You
should not need to make any changes to the makefile. You can use
make test to run your application on the supplied input file. The
output should match the example output EXACTLY. You can use diff
exampleOutput.txt output.txt to verify that the files are the same. If
nothing prints to the screen, then the output is identical, otherwise, there
are some differences.
input.txt
This is an example of the input file that your application must work with. It
is NOT a comprehensive testing suite - you should develop your own testing
suite to test your class, but you can use this file as a starting point.
exampleOutput.txt
This is an example of the output that should be produced by your program when
run on input.txt.
Grading
The grade for this homework will be broken down as follows. A more detailed
breakdown will be provided in the grade form you receive
with your homework grade.
85% - Correctness and Design
This list may not be comprehensive, but everything on this list will be
verified by the graders.
- Followed the naming standards above, works with provided application
- Each overloaded operator works correctly
- Output matches requirements
- Reduction works correctly
- Reused code to the maximum extent possible
15% - Style and Standards
Your code must adhere to the course coding standards.
Homework Submission
What to Submit
You should submit the following files:
- Rational.h
- Rational.cpp
- Hw2.cpp
- makefile
Steps for Submission
- submit all files
- submitls to verify they are in the remote directory
- submitmake to build your files remotely
- submitrun to run the application in your remote directory
Assuming you've used the recommended file names, then
to submit your homework, type the command
submit cs202 Hw2 files ...
The order in which the files are listed doesn't matter. However, you must make
sure that all files necessary to compile your project (using the makefile)
are listed. You need not submit all files at the same time. You may resubmit
your files as often as you like, but only the last submittal will be graded and
will be used to determine if your project is late. For more information,
see the projects page on the course website.
You can check to see what files you have submitted by typing
submitls cs202 Hw2
Be sure to build your project once it has been submitted using the submitmake
command, so that you know that all of the files are there and are the most
up-to-date versions:
/afs/umbc.edu/users/d/a/dana3/pub/CMSC202/submitmake cs202 Hw2
More complete documentation for submit and related commands can be found
here.
Remember -- if you make any change to your program, no matter how
insignificant it may seem, you should recompile and retest your program before
submitting it. Even the smallest typo can cause compiler errors and a reduction
in your grade.
Avoid unpleasant surprises!
Be sure to use the submitmake
utilities provided for you to compile, link and run your program after you've
submitted it.