CS35 Lab2: Inheritance in C++

Due 11:59pm Wednesday, February 3

The goal of this lab is to practice using inheritance in C++. A skeleton version of the programs will appear in your cs35/lab/2 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 remaining 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.
    • 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 (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> Jan 
Last name> Jones
Account number> 438457

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Name:    Jan Jones
Number:  438457
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> 78.99
Charged $78.99 to account 438457

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

0:End, 1:Card status, 2:Charge it, 3:Make payment> 3
Enter payment amount (Minimum $10.00, Balance $178.99)> 150.00
Payment of $150.00 made to account 438457
Charged $0.43 in interest.

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

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

0:End, 1:Card status, 2:Charge it, 3:Make payment> 0
Here's how the interaction might continue for a gold card:
Do you want to create another card (1=yes, 0=no)> 1

Create a credit card 1:Standard 2:Gold 3:Last Chance> 2
First name> Ann
Last name> Smith
Account number> 9874

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

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Gold Card
---------
Name:    Ann Smith
Number:  9874
Rate:    10.0
Limit:   $500000.00
Balance: $89.67
Frequent flyer miles earned: 89

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

0:End, 1:Card status, 2:Charge it, 3:Make payment> 1
Gold Card
---------
Name:    Ann Smith
Number:  9874
Rate:    10.0
Limit:   $500000.00
Balance: $5089.67
Frequent flyer miles earned: 5089

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. You shouldn't need to modify creditcard.h again
  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 goldcard.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/2 to my grading directory. You may run handin35 as many times as you like, and only the most recent submission will be recorded.