Homework 3 - Overloading Operators
Due: Wednesday, February 11, 2009 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 expanding the Money class:
money.h
Use the seperate compilation method from Lab 4 for
this assignment. You will need to add to money.h and create money.cpp.
Use the following main function to test your class:
hw3_main.cpp
Do NOT change this main function. I will be testing your assignments using
this form of hw3_main.cpp regardless of what main.cpp file you might include
in your submission.
Add the following operators and constructors to the class:
- Constructor that takes one double - Extract the whole portion of the double
as the dollars and the fractional portion as the cents. If there are more than
two digits of precision, throw away the remaining digits (rounding is not
required, but you are free to implement it if you wish).
- Constructor that takes one integer - Set the dollars to the integer and
the cents to 0.
- Constructor that takes two integers - Set the dollars to the first integer
and the cents to the second integer. Be sure to normalize the cents so that
the cents are between 0 and 99. Reject the cents if they are negative. Negative
dollars is allowed however.
- Copy constructor - Copy the values from the source Money object into the
current object.
- Output operator - Output the dollars and cents as $x.xx. If the dollars are
negative, output -$x.xx. Don't forget to output cents between 0 and 9 as 00 to
09.
- Input operator - Read an integer or double from the keyboard and store it
in the class. If the user gives an integer, set the dollars to that integer and
the cents to 0. Handle doubles as stated in the double constructor. Hint: read
as a double or use character-by-character I/O.
- Assignment operator - Copy the values from the source Money object into
the current object.
- Addition operator - Add two Money objects and return the result. Don't
forget to normalize the cents.
- Subtraction operator - Subtract one Money object from another. If the
second operand has more cents than the first operand, borrow cents from its
dollar member variable before performing the subtraction.
- Negation operator - Return the negation of the current object. If dollars
are not 0, negate the dollars. Otherwise, negate the cents. Only do the negation
on a temporary object, not the actual object.
- Increment operators (both prefix and postfix) - Adds 1 to the dollar
member variable.
- Decrement operators (both prefix and postfix) - Subtracts 1 from the
dollar member variable.
- Relational operators (< > <= >= == !=) - Compares two Money objects.
Make sure to compare the dollars first, then the cents; akin to how the
Score object did comparisons in Lab 3
Email me your money.h and money.cpp files. Do not email me your money.o file.