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.
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#
Accountcannot be created directly - it is abstract.Bankonly knows aboutAccount&, yet each call runs the rightwithdraw(polymorphism).using Account::Account;lets the derived classes reuse the base constructor.transfercallswithdrawfirst; if it throws,depositnever runs, so money is never created or lost.unique_ptrowns each account; whenBankis destroyed, every account is deleted automatically.- All errors are reported through exceptions and handled in one place in
main.
Extend it#
- Save all accounts to a file at exit and load them at start (
ofstream/ifstream). - Add a
FixedDepositaccount (see the exercise). - Add a PIN to each account and check it before withdrawals.
- Overload
operator<<to print an account. - Add a
search by holder namecommand usingstd::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.