# CS35 Lab2: Inheritance in C++

Due 11:59pm Wednesday, September 16

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.

• A standard credit card has an interest rate of 18%, and an upper limit of \$5,000.
• A gold card has an interest rate of 10% and an upper limit of \$500,000. In addition, gold card holders can earn frequent flyer miles for every dollar they spend (spend 1 dollar earn 1 frequent flyer mile).
• A last chance card is for people with a bad credit history. The interest rate is 25% and there is a \$1,000 charge limit. However, a last chance card holder can get a better deal by consistently paying off the entire balance. If a last chance card holder pays off the balance three times in a row, then their interest rate drops to 20% and their limit increases to \$1,500.
Program Requirements

1. In creditcard.h, declare a base class called CreditCard to handle the standard type of card. It should include 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. Remember to designate 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 math 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 rate or \$10.00.
• Be sure to check for valid input from the user. Negative payments and negative charges are not allowed.
3. In trycredit.cpp, implement a 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. In creditcard.h, declare class called GoldCard that inherits from CreditCard (remember to put a semi-colon at the end). Next in creditcard.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. 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.
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
```
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.