Homework 3 - Operator Overloading
Due: Wednesday February 1, 2012 at 5:30pm
Note: Since Midterm 1 is on Monday February 6th, the last day to turn this assignment in LATE is Friday February 3rd, so the solutions can be posted for study over the weekend.
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.
Add the following constructors and operators to the Score class:
- Conversion constructor for double. This constructor will take one double
parameter. If the double is negative, set the millions and billions
counters to 0. If the double is positive, do the following conversion:
while the double is greater than THRESHOLD
increment the billions counter
subtract THRESHOLD from the double
end-while
set the millions counter to the double using integer typecasting
- 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 Score object.
- Assignment operator, =. Modify the provided assignment operator to support
chained assignment. Instead of being a void function, it will return a
Score reference. After assigning the counters, it will return the
dereferenced
this
pointer.
- Subtraction operator, -. Subtract the value of the RHS score from the LHS
score. Use the following logic to do subtraction:
declare a Score object called temp
if RHS Score is greater than LHS score
set temp's millions and billions counter to 0
else
if RHS's millions counter is greater than LHS's millions counter
subtract 1 from LHS's billions counter
add 1billion to LHS's millions counter (borrow from the billions counter)
end-if
set temp's millions counter to LHS's millions counter - RHS's millions counter
set temp's billions counter to LHS's billions counter - RHS's billions counter
end-if
return the temp object
- 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.