Homework 3 - Operator Overloading
Due: Tuesday February 8, 2010 at 5:00pm
Coding Conventions
Use the same coding conventions as described in
Homework 1.
Additionally, make sure to indent each section of the class in the class
definition.
Assignment
For this assignment, you will be modifying the Score class from
Lab 3
to support a full range of operators. You are not required to use separate
compilation (Lab 4) for this assignment.
Add the following constructors and operators to the Score class. Keep all the
features that Lab 3 had in the class.
- Conversion constructor for double. This constructor will take one double
parameter. It will reject the double if it is negative (and set the
millions and billions counters to 0). If the double is positive, it will
do the following conversion: While the double is greater than THRESHOLD,
it will increment the billions counter and subtract THRESHOLD from the
double. Once it exits the loop, it will assign whatever remains in the
double to the millions counter (be sure to typecase the assignment).
- Copy constructor. This constructor will take a const reference to another
Score object. It will copy the source's millions and billions counter to
the current object.
- Assignment operator, =. This function will take a const reference to
another Score object. It will copy the source's millions and billions
counter to the current object.
- Update operator, +=. This function will take a const reference to another
Score object. It will add the source's millions and billions counters to
its own counters. It will then call
normalizeScore()
.
- Less-than operator, <. This function will compare two Score objects and
return true if the left object has a lower score than the right object. Be
sure with less-than (and all other relational operators) to compare both
the billions and millions counters. For example, if left is 1 billion,
500 million and right is 2 billion, 300 million, then left is less than
right even though left's millions are greater than right's millions.
- Less-than-or-equal-to operator, <=. This function will compare two Score
objects and return true if the left object has a score that is less than
or equal to the right object.
- Greater-than operator, >. This function will compare two Score objects
and return true if the left object has a larger score than the right object.
- Greater-than-or-equal-to operator, >=. This function will compare two
Score objects and return true if the left object has a score that is
greater than or equal to the right object.
- Equality operator, ==. This function will compare two Score objects and
return true if the left object has a score that is equal to the right
object.
- Inequality operator, !=. This function will compare two Score objects and
return true if the left object has a score that is NOT equal to the right
object.
Main Function
Your main()
function is going to be a driver function that tests
all of the operators and constructors you have added to the class. Note how
the main()
function for Lab 3 tests input, output, converting an
integer, addition, the default constructor and so on. You will need to add
additional testing for all of the above features.
In particular, your main function should test the following:
Email your complete Score class and main function to me.