Challenge 1: Implement an Account Class Using 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 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 - In
Current
class,balance
is set to 50000 in parametrized constructor ofCurrent
object
Here’s a sample result which you should get.
Sample Input
Savings s1(50000);
Account * acc = &s1;
acc->Deposit(1000);
acc->printBalance();
acc->Withdraw(3000);
acc->printBalance();
Current c1(50000);
acc = &c1;
acc->Deposit(1000);
acc->printBalance();
acc->Withdraw(3000);
acc->printBalance();
Sample Output
Get hands-on with 1400+ tech skills courses.