CS12, Program 2

 

Assigned: January 25th, 2002

 

Due: February 4th, 2002, 11pm

 

 

 

Topics

 

·      Writing methods

·      Operator overloading

·      Using classes

 

 

Background

 

In order to keep track of amounts of money, we design a class called Purse.  Each Purse object can hold some amount of money.  We can keep track of the amount of dollars and the amount of cents in the Purse object.  We can also add and subtract money from the purse, add the amounts in two different Purse objects to create a third Purse, and we can compare two Purse objects to see if they contain the same amount of money.  Purses can be empty, but they cannot be overdrawn (contain negative amounts of money).

 

Purses are smart objects that keep the amounts in reduced form; the number of cents is always between 0 and 99, inclusive.  For example, if we put 3 dollars and 267 cents into a Purse, it will translate this to 5 dollars and 67 cents.  The amount of money in a Purse object can be displayed using the overloaded << operator. 

 

The Program

 

In this assignment you will complete a partially written Purse class. The header file for this program and shell for the implementation file have been written for you. Your job is to fill in the methods in the implementation file.  You may add other methods to the class if you think they are necessary. 

 

Once the class is functional, you will write a program that uses the class.  Actually, you can write the program even with the class as it is now, and it should compile and run just fine.  It just won’t be doing anything useful.

 

To Do

 

1.    Download the files purse.h and purse.cpp.   Complete the methods and operators in purse.cpp so that they perform the tasks as described.  Pay close attention to the comments preceding each method and adhere to the specifications.

 

2.    Write a program that does the following:

·      Create an empty Purse object.

·      Display the amount in the Purse.

·      Prompt the user for dollar and cent amounts, and add this to the Purse. 

·      Display the amount in the Purse.

·      Read a second set of dollar and cent amounts, and create a second purse object containing the amount entered by the user. 

·      Display the amount in the second Purse.

·      Read a third set and subtract it from the second Purse. 

·      Display the amount in the second Purse.

·      Read two more sets and add them to the second Purse, displaying after each addition. 

·      Compare the two Purses to see if they contain the same amount

·      Create a third Purse that will contain the sum of the first two Purses.

·      Display the amount in the third Purse.

 

 

The Input

 

Ten positive integers, entered from the keyboard, representing 5 sets of dollar/cent pairs.  Please note that I am not setting any upper limit on the values entered.  Your class should be smart enough to deal with cent amounts greater than 100.

 

 

The Output

 

The output should look like the following example (amounts will differ depending on user input). 

 

Purse 1

Start program with empty purse.

$0.00

Adding to the purse.

Enter dollars and cents: 2 74

$2.74

 

Purse 2

Creating purse.

Enter dollars and cents: 3 18

$3.18

Subtracting from purse.

Enter dollars and cents: 1 12

$2.06

Adding to purse.

Enter dollars and cents: 5 34

$7.40

Adding to purse.

Enter dollars and cents: 14 82

$22.22

 

The two purses contain different amounts

 

Purse 3 (sum of Purse 1 and Purse 2)

$24.96

 

 

 

What to turn in

 

·      purse.h

 

·      purse.cpp

 

·      main.cpp

 

 

Please note:

 

·      Do not use any other names for your files.  These names are case sensitive – do not use capital letters anywhere in the file names.  Failure to name your files as above will cost you 10 points. 

 

·      Please make sure that main.cpp includes your name, login and lab section.  Each is worth 2 points on this assignment.

 

 

Extra Credit

 

·      Add to your program the capacity of keeping track of the coins and bills in the purse, in addition to the amounts of dollars and cents. You will need additional methods to access the number of pennies, nickels, dimes, quarters, dollar coins, ones, fives, tens and twenties, as well as methods to add the different coins.

 

·      Add two separate methods, bool isEqual(Purse) and bool isEquivalent(Purse), to compare the invoking Purse to the parameter Purse. isEqual() will return true if the purses contain the exact same coin and bill amounts; isEquivalent() will return true if the two purses contain the same amount. For example, a purse containing four quarters is equivalent, but not equal, to a purse containing a dollar bill.