2012年2月21日 星期二

Exercise #9


Create a Money class consisting of an unsigned int and an unsigned short data member, dollars and cents. You may assume that all money values are non-negative. Add the following member functions:
  1. A constructor with two default arguments, an unsigned int and an unsigned short. The arguments should initialize the dollars and cents members. Since both arguments have default values (both 0), this constructor also serves as a default constructor.
  2. A constructor with a double argument. The double must be used to initialize both the dollars and cents. For example, an argument of 1.75 should assign 1 to the dollars and 75 to the cents. Round off cents to the second decimal place, so 5.55555 would set the dollars to 5 and the cents to 56.
  3. A copy constructor.
  4. An overloaded !(unary) operator that serves as a print function. It should print the dollars and cents with a leading dollar sign and a decimal point separating the dollars and cents. Make sure you are able to print the following values:  $1.03, $10.00, $0.01, $1234.56, and $0.00.
  5. An overloaded + (unary) operator that "reduces" Money. For example, if you passed 5 and 150 into the first constructor, you would want to use this function to change the Money object to have dollars = 6 and cents = 50. This function should return Money by reference.
  6. An overloaded < (binary) operator that tests two Money objects to see if the first is less than the second. This function should return a bool.
  7. An overloaded == (binary) operator that tests two Money objects to see if the first is equal to the second. This function should return a bool.
  8. An overloaded + (binary) operator that adds two Money objects and returns the sum by value(Money). Make sure your logic can handle $2.56+$5.67 and $5.63+$0.37.
  9. An overloaded - (binary) operator that subtracts two Money objects and returns the difference. It should return a Money by value. If you try to subtract a larger Money from a smaller one, print an error message and have the function return $0.00. (0 dollars and 0 cents).
  10. An overloaded * (binary) operator with a double argument. This permits you to multiple a Money object by a double, For example, $4.29*67.3. It should return a Money by value.
  11. An overloaded += operator that adds more Money to a Money object and returns the result by reference. For example, M1 += M2;  (this should change M1).

Divide your final program into three files: a header file for your Money class, a source file for your Money methods, and another source file for main(). Write you own main() and thoroughly test all functions. Make sure your main() demonstrates calls to each member function. 

沒有留言:

張貼留言