Challenge 2: Implement a Class Using Pure Virtual Functions
In this challenge, we'll implement an account class along with two derived classes saving and current.
We'll cover the following...
We'll cover the following...
Problem Statement
Write a code that has:
- A parent class named
Account.- Inside it define:
- a protected float member
balance
- a protected float member
- We have three pure virtual functions:
void Withdraw(float amount)void Deposit(float amount)void printBalance()
- Inside it define:
- Then, there are two derived classes
Savingsclass- has a private member
interest_rateset to 0.8 Withdraw(float amount)deducts amount from balance with interest_rateDeposit(float amount)adds amount in balance with interest_rateprintBalance()displays the balance in the account
- has a private member
CurrentclassWithdraw(float amount)deducts amount from balanceDeposit(float amount)adds amount in balanceprintBalance()displays the balance in the account`
Input
- In
Savingsclass,balanceis set to 50000 in parametrized constructor ofSavingsobject called byAccountclass - In
Currentclass,balanceis set to 50000 in parametrized constructor ofCurrentobject called byAccountclass
Here’s a sample result which you should get.
Sample Input
Account * acc[2];
acc[0] = new Savings(50000);
acc[0]->Deposit(1000);
acc[0]->printBalance();
acc[0]->Withdraw(3000);
acc[0]->printBalance();
acc[1] = new Current(50000);
acc[1]->Deposit(1000);
acc[1]->printBalance();
acc[1]->Withdraw(3000);
acc[1]->printBalance();