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.

Problem Statement

Write a code that has:

  • A parent class named Account.
    • Inside it define:
      • a protected float member balance
    • We have three pure virtual functions:
      • void Withdraw(float amount)
      • void Deposit(float amount)
      • void printBalance()
  • 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_rate
        • Deposit(float amount) adds amount in balance with interest_rate
        • printBalance() displays the balance in the account
    • Current class
      • Withdraw(float amount) deducts amount from balance
        • Deposit(float amount) adds amount in balance
        • printBalance() displays the balance in the account`

Input

  • In Savings class, balance is set to 50000 in parametrized constructor of Savings object called by Account class
  • In Current class, balance is set to 50000 in parametrized constructor of Current object called by Account 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 1200+ tech skills courses.