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
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
Savings
class- has a private member
interest_rate
set 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
Current
classWithdraw(float amount)
deducts amount from balanceDeposit(float amount)
adds amount in balanceprintBalance()
displays the balance in the account`
Input
- In
Savings
class,balance
is set to 50000 in parametrized constructor ofSavings
object called byAccount
class - In
Current
class,balance
is set to 50000 in parametrized constructor ofCurrent
object called byAccount
class
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();
Sample Output
Get hands-on with 1400+ tech skills courses.