CS35 Lab2: Inheritance in C++

Standard Card | Gold Card | Last Chance Card | Tips
Due 11:59pm Wednesday, 15 September

The goal of this lab is to practice using inheritance in C++. A skeleton version of the programs will appear in your cs35/lab/02 directory when you run update35. The program handin35 will only submit files in this directory. In later labs you can work with a partner, but for this lab you should work on your own.

Introduction

You will write a program that allows a user to repeatedly create new credit cards from three possible types: a standard card, a gold card, or a last chance card.

Program Requirements
  1. creditcard.h, declares a base class called CreditCard to handle the standard type of card. It includes constructors (one which takes name and account number, and another which takes name, account number, interest rate, and spending limit), accessor methods, and update methods to allow the user to make payments and charge the card. Eventually you will need to designate some methods as virtual that you will override in the derived classes. Don't forget the semicolon at the end of the class declaration!
  2. In creditcard.cpp, implement the CreditCard methods.
    • When an amount is charged, it must not exceed the available balance (limit-balance).
    • When a payment is made, if the balance is not paid in full, then interest will be charged on the original balance. The interest rate represents an annual rate. We will assume that payments are made monthly. Therefore the interest rate should be divided by 12 before computing the interest owed, which is then added on to the balance. When you compute the interest owed, it may end up having more precision than two decimal places (which represent cents). A typical bank would round this up to the nearest cent. You can implement this by multiplying the interest owed by 100, then rounding up with the ceil function from the cmath library, and finally dividing by 100.0.. Remember, unless the balance is paid in full, the interest added to the balance is the interest on the balance before any payments are applied. Thus if you pay $800 on a $1000 balance, interest is computed on $1000, not $200. Credit card companies are evil.
    • For the minimum payment, we want to ensure that the interest charged will never exceed the payment made. When the balance is less than $10.00, then the minimum payment due will be the entire balance. Otherwise, the minimum payment due is the maximum of either twice the monthly interest or $10.00.
    • Be sure to check for valid input from the user. Negative payments and negative charges are not allowed. Print an error message to warn users.
  3. In trycredit.cpp, modify the main program along with any appropriate helper functions to test your base class. Your main program should declare a pointer to a CreditCard and then use new to create the type of card chosen by the user. Here's an example prototype of a function that you might create to use the card:
    void useCard(CreditCard *card);
    
    Inside this function, you would use card-> to call methods. Do not move on to the next step until your CreditCard class has been fully tested and is working correctly.
  4. goldcard.h, declares a class called GoldCard that inherits from CreditCard (remember the semi-colon at the end is required). Next in goldcard.cpp, add the implementation of this class. We want to re-use as much of the base class as possible. Create new accessor methods to handle new data. Only override the base class methods when you need to insert additional functionality. If you override a base class method, you will need to make the method virtual in creditcard.h &endash; and only the header file. Finally, update the main program in trycredit.cpp to test this derived class. Do not move on to the next step until you have thoroughly tested this class.
  5. Create another derived class called LastChanceCard that inherits from CreditCard and go through the same steps to declare, implement, and test it. This class should be declared in lastchancecard.h and implemented in lastchancecard.cpp
Sample Run
Here's how an interaction with the program might go for a standard credit card:
Create a credit card 1:Standard 2:Gold 3:Last Chance> 1
First name> Mike
Last name> Doughty
Account number> 610970

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Name:    Mike Doughty
Number:  610970
Rate:    18.0
Limit:   $5000.00
Balance: $0.00

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 35.99
Charged 35.99 to account 610970

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 100
Charged 100.00 to account 610970

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $135.99> 120
Payment of $120.00 made to account 610970
Charged $2.04 in interest

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 54.40
Charged 54.40 to account 610970

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $72.43> 72.43
Payment of $72.43 made to account 610970
Balance paid in full

0:End, 1:Card status, 2:Charge it, 3:Make payment> 0

Do you want to create another card (1=yes, 0=no)> 0
Goodbye
Here's how the interaction might continue for a gold card:
Create a credit card 1:Standard 2:Gold 3:Last Chance> 2
First name> Lady
Last name> Gaga
Account number> 03281986

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 25000
Charged 25000.00 to account 03281986

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Gold Card
---------
Name:    Lady Gaga
Number:  03281986
Rate:    10.0
Limit:   $100000.00
Balance: $25000.00
Points earned: 25000
0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $416.67, Balance $25000.00> 10
Payment failed 

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Gold Card
---------
Name:    Lady Gaga
Number:  03281986
Rate:    10.0
Limit:   $100000.00
Balance: $25000.00
Points earned: 25000
0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $416.67, Balance $25000.00> 20000
Payment of $20000.00 made to account 03281986
Charged $208.34 in interest

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Gold Card
---------
Name:    Lady Gaga
Number:  03281986
Rate:    10.0
Limit:   $100000.00
Balance: $5208.34
Points earned: 25000
0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $86.81, Balance $5208.34> 5208.34
Payment of $5208.34 made to account 03281986
Balance paid in full

0:End, 1:Card status, 2:Charge it, 3:Make payment> 0

Do you want to create another card (1=yes, 0=no)> 0
Goodbye
And finally a LastChanceCard:
Create a credit card 1:Standard 2:Gold 3:Last Chance> 3
First name> Lindsay
Last name> Lohan
Account number> 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Last Chance Card
---------
Name:    Lindsay Lohan
Number:  07021984
Rate:    25.0
Limit:   $1000.00
Balance: $0.00
Consecutive full pays: 0

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 2000
Charge failed 
Current balance $0.00

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 25.00
Charged 25.00 to account 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $25.00> 25
Payment of $25.00 made to account 07021984
Balance paid in full

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Last Chance Card
---------
Name:    Lindsay Lohan
Number:  07021984
Rate:    25.0
Limit:   $1000.00
Balance: $0.00
Consecutive full pays: 1

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 10
Charged 10.00 to account 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $10.00> 10
Payment of $10.00 made to account 07021984
Balance paid in full

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 35.00
Charged 35.00 to account 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 25.25 
Charged 25.25 to account 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Last Chance Card
---------
Name:    Lindsay Lohan
Number:  07021984
Rate:    25.0
Limit:   $1000.00
Balance: $60.25
Consecutive full pays: 2

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $60.25> 60.25
Payment of $60.25 made to account 07021984
Balance paid in full
Congratulations! Because you paid your balanace if full
Three consecutive times, we are raising your limit to $1500.00
and lowering your rate to 20.00%

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Last Chance Card
---------
Name:    Lindsay Lohan
Number:  07021984
Rate:    20.0
Limit:   $1500.00
Balance: $0.00
Consecutive full pays: 3

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 20
Charged 20.00 to account 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $20.00> 20
Payment of $20.00 made to account 07021984
Balance paid in full

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Last Chance Card
---------
Name:    Lindsay Lohan
Number:  07021984
Rate:    20.0
Limit:   $1500.00
Balance: $0.00
Consecutive full pays: 4

0:End, 1:Card status, 2:Charge it, 3:Make payment> 2
Enter amount to charge> 200
Charged 200.00 to account 07021984

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $200.00> 10
Payment of $10.00 made to account 07021984
Charged $3.34 in interest

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Last Chance Card
---------
Name:    Lindsay Lohan
Number:  07021984
Rate:    20.0
Limit:   $1500.00
Balance: $193.34
Consecutive full pays: 0

0:End, 1:Card status, 2:Charge it, 3:Make payment> 0

Do you want to create another card (1=yes, 0=no)> 0
Goodbye
Tips
  1. run update35
  2. cd cs35/labs/02/
  3. make
  4. ./trycredit
  5. See? It works. kinda.
  6. Open creditcard.h and creditcard.cpp
  7. modify creditcard.cpp to add methods described in creditcard.h add one method at a time and then add sample test code in trycredit.cpp to test your newly implemented method
  8. save, make, debug, ./trycredit, test, add another method, repeat
  9. Once all the methods in creditcard.h are working move on to goldcard.h and goldcard.cpp
  10. You only need to add code to goldcard.cpp, and you only need to add methods that are not in the base class, or methods in the base class you want to override. goldcard.cpp should be much shorter than creditcard.cpp
  11. Go back and modify creditcard.h and add virtual to the methods you override in goldcard.cpp
  12. Add code in trycredit.cpp to support the goldcard. Don't forget to include the header file at the top
  13. Edit the top of the Makefile to add goldcard.o to the list of objects in OBJ
  14. save, make, debug, ./trycredit, test, repeat
  15. move on to lastchancecard.h and lastchance.cpp.
  16. Add code in trycredit.cpp to support the lastchance card. Don't forget to include the header file at the top
  17. Edit the top of the Makefile to add lastchancecard.o to the list of objects in OBJ
Submit
Once you are satisfied with your code, hand it in by typing handin35. This will copy the code from your cs35/lab/02 to my grading directory. You may run handin35 as many times as you like, and only the most recent submission will be recorded.