⚡0 XP🔥0

Project: Bank Account System

Lesson 12 of 12 ⏱ 6 min read ✎ 1 exercises ❓ 3-question quiz ▶ Runs C++

Build an object-oriented banking program with an abstract Account class, Savings and Current accounts, a Bank that manages them, exceptions for errors and a transaction log.

This project brings together everything from the course: classes, constructors, inheritance, virtual functions, abstract classes, STL containers, smart pointers and exceptions. It is a great project to show in interviews because it demonstrates real object-oriented design.

Design#

             Account (abstract)
        - number, holder, balance
        - deposit(), virtual withdraw() = 0, virtual type() = 0
        - history (vector<string>)
            /                    \
   SavingsAccount           CurrentAccount
   min balance 1000         overdraft up to 20000
   addInterest() 4%

   Bank
   - map<int, unique_ptr<Account>>
   - open(), find(), transfer(), report()
हिंदी में समझें

पहले design सोचिए, फिर code लिखिए। Account base class में वो चीज़ें हैं जो हर account में होती हैं (number, holder, balance, deposit)। Withdraw के नियम हर account के लिए अलग हैं, इसलिए वो pure virtual है - Savings और Current उसे अपने तरीके से लिखते हैं। Bank class सारे accounts को एक map में संभालती है।

Rules#

  • Savings: balance may not go below Rs 1,000; earns 4% interest.
  • Current: can go negative down to an overdraft limit of Rs 20,000; no interest.
  • Every deposit/withdrawal/transfer is recorded in the account's history.
  • Any rule violation throws an exception that the caller catches and reports.

The complete program#

The input simulates a teller's commands. Read the commands at the bottom of the code and the input box, then run it.

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Opened savings account 5001 for Asha
Opened current account 5002 for Ravi
deposit 2500 on 5001 OK
ERROR: Savings must keep a minimum balance of 1000
withdraw 22000 on 5002 OK
Transferred 4000 from 5001 to 5002
ERROR: No account 5003
Month-end processing done
Savings #5001 (Asha) balance 13545.00
   - Opened with 15000
   - Deposit 2500
   - Withdraw 4000
   - Interest 45
Current #5002 (Ravi) balance -13000.00
   - Opened with 5000
   - Withdraw 22000
   - Deposit 4000
Bank total: 545.00

What to notice#

  • Account cannot be created directly - it is abstract. Bank only knows about Account&, yet each call runs the right withdraw (polymorphism).
  • using Account::Account; lets the derived classes reuse the base constructor.
  • transfer calls withdraw first; if it throws, deposit never runs, so money is never created or lost.
  • unique_ptr owns each account; when Bank is destroyed, every account is deleted automatically.
  • All errors are reported through exceptions and handled in one place in main.

Extend it#

  1. Save all accounts to a file at exit and load them at start (ofstream/ifstream).
  2. Add a FixedDeposit account (see the exercise).
  3. Add a PIN to each account and check it before withdrawals.
  4. Overload operator<< to print an account.
  5. Add a search by holder name command using std::find_if.

Summary#

You designed a small but realistic system with an abstract base class, two concrete subclasses, a manager class with an STL map of smart pointers, and exceptions for business-rule errors. This is exactly the structure used in real banking, inventory and booking software.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Add a FixedDeposit account

Complete FixedDeposit, which does not allow withdrawals (throw runtime_error("FD cannot be withdrawn before maturity")) and gives 7% yearly interest from addInterest(). The main given must print:

FD balance after interest: 107000
Error: FD cannot be withdrawn before maturity
Expected output:
FD balance after interest: 107000
Error: FD cannot be withdrawn before maturity

Quick Quiz

Q1.

Why is Account an abstract class in this project?

Q2.

Why does the Bank store unique_ptr<Account> instead of Account objects?

Q3.

What happens when a withdrawal breaks the rules?