SyntaxPilot
Courses/C++/Lesson 19

C++ · Lesson 19 of 40 · 11 min

OOP project: bank system

queued

These are the notes for C++ episode 19, a project episode. Over episodes 11-18 you learned structs, classes, constructors and destructors, access specifiers and encapsulation, inheritance, virtual functions and abstract classes, operator overloading, and friend, static and this. Here every one of those tools earns its place by fixing a real problem in a bank: a public balance that anyone can wreck, accounts that need unique numbers, a statement to print, two account types with different rules, a transfer that must never create money, and accounts that must be cleaned up correctly. At the end, the whole bank reads commands from input. Every example below was compiled with g++ -O2 -std=c++17 main.cpp -o main (g++ 13.3, the same command as the SyntaxPilot compiler) and run; the outputs and error messages are copied from those runs.

The brief

Requirement Rule
Open accounts savings or current
Unique numbers 1, 2, 3, ... (never repeated)
Deposit positive amounts only
Withdraw within each account type's limit
Transfer from one account to another; money must never appear from nowhere
Statement print every account
Account type Rule Lowest allowed balance (the floor)
Savings must always keep a minimum balance of Rs. 1000 1000
Current (for shops and businesses) may go up to Rs. 5000 below zero: an overdraft -5000

Step 1: a struct with a public balance (the old register)

The branch used to keep a handwritten register: names and balances. Anyone with a pen could change a number. The obvious code copies it:

#include <iostream>
#include <string>
using namespace std;

struct Account {
    string name;
    double balance;
};

int main() {
    Account asha = {"Asha", 5000};
    asha.balance = -50000;   // a typo, or a bad actor
    cout << asha.name << ": Rs. " << asha.balance << endl;
    return 0;
}

Output:

Asha: Rs. -50000

No error, no warning. Why it's dangerous: a public balance has no guard. The bank's rules (no negative deposits, no overdrawing a savings account) live nowhere, so one typo, or one bad actor, can set any value.

Step 2: encapsulation, private data and checked functions

Make the data private, and let only checked functions change it (episode 14):

#include <iostream>
#include <string>
using namespace std;

class Account {
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    bool withdraw(double amt) {
        if (amt <= 0 || amt > balance) return false;
        balance -= amt;
        return true;
    }
    void deposit(double amt) { if (amt > 0) balance += amt; }
    double getBalance() const { return balance; }
};

int main() {
    Account asha("Asha", 5000);
    asha.balance = -50000;
    return 0;
}
error: 'double Account::balance' is private within this context

The attack doesn't even compile. Now the honest way, through withdraw, which returns true or false:

#include <iostream>
#include <string>
using namespace std;

class Account {
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    bool withdraw(double amt) {
        if (amt <= 0 || amt > balance) return false;
        balance -= amt;
        return true;
    }
    void deposit(double amt) { if (amt > 0) balance += amt; }
    double getBalance() const { return balance; }
};

int main() {
    Account asha("Asha", 5000);
    if (!asha.withdraw(9000)) cout << "Refused: not enough money" << endl;
    asha.withdraw(1500);
    cout << "Balance: Rs. " << asha.getBalance() << endl;
    return 0;
}

Output:

Refused: not enough money
Balance: Rs. 3500
  • withdraw refuses zero, negative amounts, and anything more than the balance, and reports whether it worked by returning bool.
  • deposit ignores non-positive amounts.
  • getBalance() is const: reading doesn't change the account.

Step 3: unique account numbers (static) and a statement (friend)

Each account can't count by itself (we saw that with the token machine in episode 18), so one static counter is shared by the class. operator<< is a friend defined inside the class, so it can print the private number, name and balance:

#include <iostream>
#include <string>
using namespace std;

class Account {
    int no;
    string name;
    double balance;
    static int next;
public:
    Account(string n, double b) : no(next++), name(n), balance(b) {}
    friend ostream& operator<<(ostream& out, const Account& a) {
        out << "#" << a.no << " " << a.name << ": Rs. " << a.balance;
        return out;
    }
};
int Account::next = 1001;

int main() {
    Account a("Asha", 5000), b("Ravi", 2000), c("Meena", 800);
    cout << a << endl << b << endl << c << endl;
    return 0;
}

Output:

#1001 Asha: Rs. 5000
#1002 Ravi: Rs. 2000
#1003 Meena: Rs. 800
  • no(next++): the account takes the current value of next, then next goes up by one (post-increment).
  • int Account::next = 1001; defines the static member once, outside the class.

Step 4: savings vs current, one question with two answers

Two account types, two rules. But the cashier really asks one question: how low can this balance go? withdraw is the same for everyone; only that answer differs. So the base class asks floor() as a pure virtual function, and each child answers with override (episode 16):

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    virtual double floor() const = 0;   // lowest allowed balance
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    double getBalance() const { return balance; }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};
class Current : public Account {
public:
    Current(string n, double b) : Account(n, b) {}
    double floor() const override { return -5000; }
};

int main() {
    Savings asha("Asha", 5000);
    Current ravi("Ravi", 2000);
    cout << asha.withdraw(4500) << " " << asha.getBalance() << endl;
    cout << ravi.withdraw(6000) << " " << ravi.getBalance() << endl;
    return 0;
}

Output:

0 5000
1 -4000

(cout prints a bool as 1 or 0.)

  • Asha (savings, 5000) asks for 4500: 500 would be below her floor of 1000 → refused (0), balance stays 5000.
  • Ravi (current, 2000) asks for 6000: -4000 is above his floor of -5000 → allowed (1), balance -4000.

One withdraw function, two rules. Inside withdraw, the call floor() is virtual, so the real object's version runs (dynamic binding). This design is called the template method pattern: the base class fixes the steps; children fill in one detail.

  • balance is protected (episode 14): children could use it, outsiders can't.
  • virtual ~Account() {}: we'll delete accounts through Account*, so the destructor must be virtual (episode 16).

Because Account is abstract, nobody can open a plain Account:

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    virtual double floor() const = 0;   // lowest allowed balance
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    double getBalance() const { return balance; }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};
class Current : public Account {
public:
    Current(string n, double b) : Account(n, b) {}
    double floor() const override { return -5000; }
};

int main() {
    Account* x = new Account("Test", 100);
    return 0;
}
error: invalid new-expression of abstract class type 'Account'

Step 5: a transfer that never creates money

The obvious transfer: withdraw from me, deposit into them.

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    void deposit(double amt) { if (amt > 0) balance += amt; }
    virtual double floor() const = 0;   // lowest allowed balance
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    void transfer(Account& to, double amt) {
        withdraw(amt);
        to.deposit(amt);
    }
    double getBalance() const { return balance; }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};

int main() {
    Savings asha("Asha", 5000), ravi("Ravi", 2000);
    asha.transfer(ravi, 10000);
    cout << asha.getBalance() << " " << ravi.getBalance() << endl;
    return 0;
}

Output:

5000 12000

Asha still has 5000, and Ravi has 12000: ten thousand rupees created from nothing. The withdraw was refused (it returned false), but nobody listened, and the deposit happened anyway. Rule: never ignore a true/false answer.

The fix checks the answer first, and uses this (episode 18) to refuse a transfer to the same account:

bool transfer(Account& to, double amt) {
    if (&to == this || !withdraw(amt)) return false;
    to.deposit(amt);
    return true;
}
#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    void deposit(double amt) { if (amt > 0) balance += amt; }
    virtual double floor() const = 0;   // lowest allowed balance
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    bool transfer(Account& to, double amt) {
        if (&to == this || !withdraw(amt)) return false;
        to.deposit(amt);
        return true;
    }
    double getBalance() const { return balance; }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};

int main() {
    Savings asha("Asha", 5000), ravi("Ravi", 2000);
    if (!asha.transfer(ravi, 10000)) cout << "Transfer refused" << endl;
    cout << asha.getBalance() << " " << ravi.getBalance() << endl;
    return 0;
}

Output:

Transfer refused
5000 2000
  • &to is the address of the target account; this is the address of the account doing the transfer. Equal means "the same account".
  • || short-circuits: if it's a self-transfer, withdraw isn't even called.

The design: where each tool lives

Problem Tool Episode
Anyone could change the balance private data + checked deposit() / withdraw() (encapsulation) 12, 14
Building a valid account a constructor with an initialiser list 13
Unique account numbers a static counter 18
Printing the statement a friend operator<< 17, 18
Two account types, one withdraw inheritance + pure virtual floor() and type() with override 15, 16
No plain "Account" should exist an abstract base class 16
Transfers creating money check withdraw()'s answer —
Transfer to yourself compare with this 18
Accounts made with new delete through Account* + a virtual destructor 10, 13, 16
              Account (abstract)
   private: no, static next   protected: name, balance
   deposit()  withdraw()  transfer()  friend operator<<
   pure virtual: floor(), type()
            /                     \
      Savings                    Current
   floor() = 1000             floor() = -5000
   type() = "Savings"         type() = "Current"

The full bank

The base class has everything we built, plus a second pure virtual function, type(), so the statement can say which kind each account is. Savings and Current only answer the two questions: that's the point of a good base class. main keeps an array of Account*, reads commands (open, deposit, withdraw, transfer), creates accounts with new, prints done or REFUSED for each command, and finally prints the statement and deletes each account.

#include <iostream>
#include <string>
using namespace std;

class Account {
    int no;
    static int next;
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : no(next++), name(n), balance(b) {}
    virtual ~Account() {}
    virtual double floor() const = 0;
    virtual string type() const = 0;
    void deposit(double amt) { if (amt > 0) balance += amt; }
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    bool transfer(Account& to, double amt) {
        if (&to == this || !withdraw(amt)) return false;
        to.deposit(amt);
        return true;
    }
    friend ostream& operator<<(ostream& out, const Account& a) {
        out << "#" << a.no << " " << a.name << " (" << a.type() << "): Rs. " << a.balance;
        return out;
    }
};
int Account::next = 1;
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
    string type() const override { return "Savings"; }
};
class Current : public Account {
public:
    Current(string n, double b) : Account(n, b) {}
    double floor() const override { return -5000; }
    string type() const override { return "Current"; }
};

int main() {
    Account* acc[100];
    int n = 0;
    string cmd;
    while (cin >> cmd) {
        bool ok = true;
        if (cmd == "open") {
            string kind, name; double amt;
            cin >> kind >> name >> amt;
            if (kind == "savings") acc[n++] = new Savings(name, amt);
            else acc[n++] = new Current(name, amt);
        } else if (cmd == "deposit") {
            int no; double amt; cin >> no >> amt;
            acc[no - 1]->deposit(amt);
        } else if (cmd == "withdraw") {
            int no; double amt; cin >> no >> amt;
            ok = acc[no - 1]->withdraw(amt);
        } else if (cmd == "transfer") {
            int from, to; double amt; cin >> from >> to >> amt;
            ok = acc[from - 1]->transfer(*acc[to - 1], amt);
        }
        cout << cmd << (ok ? ": done" : ": REFUSED") << endl;
    }
    for (int i = 0; i < n; i++) {
        cout << *acc[i] << endl;
        delete acc[i];
    }
    return 0;
}

Input:

open savings Asha 5000
open current Ravi 2000
deposit 1 1500
withdraw 1 6000
withdraw 2 6000
transfer 1 2 3000
transfer 2 2 100

Output:

open: done
open: done
deposit: done
withdraw: REFUSED
withdraw: done
transfer: done
transfer: REFUSED
#1 Asha (Savings): Rs. 3500
#2 Ravi (Current): Rs. -1000

Check the maths:

Step Asha (savings, floor 1000) Ravi (current, floor -5000) Result
open 5000 2000 done, done
deposit 1 1500 6500 done
withdraw 1 6000 6500 (500 < 1000) REFUSED
withdraw 2 6000 -4000 (≥ -5000) done
transfer 1 2 3000 3500 -1000 done
transfer 2 2 100 -1000 (same account) REFUSED

Total money: 5000 + 2000 + 1500 deposited - 6000 withdrawn = 2500 = 3500 + (-1000). Nothing appeared, nothing disappeared.

Notes on main:

  • The account number is its position in the array plus one (acc[no - 1]), because next starts at 1.
  • ok starts as true; only withdraw and transfer can refuse.
  • delete acc[i] runs ~Savings() / ~Current() and then ~Account(), because the base destructor is virtual.
  • Homework 3 below makes it safe against account numbers that don't exist (right now, deposit 9 100 would read outside the array: undefined behaviour). In episode 21 you'll replace the fixed array with a vector, and in episode 28 the raw pointers with smart pointers.

Adding a new account type: a fixed deposit

The real test of a design: the bank launches a fixed deposit (money locked in for a year, no withdrawals). How much code? One class. Its floor is its own balance, so any withdrawal would go below it. The first try forgets const:

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    virtual double floor() const = 0;   // lowest allowed balance
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    double getBalance() const { return balance; }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};
class Fixed : public Account {
public:
    Fixed(string n, double b) : Account(n, b) {}
    double floor() { return balance; }
};

int main() {
    Savings asha("Asha", 5000);
    Fixed meena("Meena", 100000);
    cout << asha.withdraw(2000) << " " << asha.getBalance() << endl;
    cout << meena.withdraw(1) << " " << meena.getBalance() << endl;
    return 0;
}
error: cannot declare variable 'meena' to be of abstract type 'Fixed'

Without const, double floor() is a different function: it doesn't override the pure virtual floor() const, so Fixed is still abstract. (With override written, g++ would point straight at the mismatch.) Add const override:

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    virtual double floor() const = 0;   // lowest allowed balance
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    double getBalance() const { return balance; }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};
class Fixed : public Account {
public:
    Fixed(string n, double b) : Account(n, b) {}
    double floor() const override { return balance; }
};

int main() {
    Savings asha("Asha", 5000);
    Fixed meena("Meena", 100000);
    cout << asha.withdraw(2000) << " " << asha.getBalance() << endl;
    cout << meena.withdraw(1) << " " << meena.getBalance() << endl;
    return 0;
}

Output:

1 3000
0 100000

Asha's savings allows 2000 (1, 3000 left); Meena's fixed deposit refuses even 1 rupee (0). withdraw, transfer and the statement work with Fixed without a single change: the open/closed principle.

Common mistakes (and the real g++ messages)

Mistake Code What g++ says / what happens Fix
Touching a private balance asha.balance = -50000; error: 'double Account::balance' is private within this context use deposit() / withdraw()
Opening a plain Account new Account("Test", 100); error: invalid new-expression of abstract class type 'Account' new Savings(...) or new Current(...)
Ignoring withdraw's answer withdraw(amt); to.deposit(amt); no message: money created from nothing (5000 12000) if (!withdraw(amt)) return false;
Non-virtual base destructor ~Account() {} no message: delete acc[i] skips the child destructor (undefined behaviour) virtual ~Account() {}
Static counter never defined static int next; only undefined reference to `Account::next' (linker) int Account::next = 1;
Forgetting override and getting the signature wrong double floor() { ... } (no const) the class stays abstract: invalid new-expression of abstract class type 'Savings' double floor() const override
Public balance in a struct struct Account { double balance; }; no message: any code can set any value private data + checked functions

Practice

  1. Add the Fixed deposit account to the full bank's open command, with a static count of accounts opened.
  2. Add an interest command that adds 4% to every savings account.
  3. Refuse commands for account numbers that don't exist.
  4. Print how many accounts were opened, using a static function.
Solutions

1 and 4. Fixed::floor() returns balance, so any withdrawal would go below it (in the full bank, add else if (kind == "fixed") acc[n++] = new Fixed(name, amt);). A static opened() reports the counter.

#include <iostream>
#include <string>
using namespace std;

class Account {
    static int next;
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) { next++; }
    virtual ~Account() {}
    virtual double floor() const = 0;
    bool withdraw(double amt) {
        if (amt <= 0 || balance - amt < floor()) return false;
        balance -= amt;
        return true;
    }
    double getBalance() const { return balance; }
    static int opened() { return next; }
};
int Account::next = 0;
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    double floor() const override { return 1000; }
};
class Fixed : public Account {
public:
    Fixed(string n, double b) : Account(n, b) {}
    double floor() const override { return balance; }
};

int main() {
    Savings s("Asha", 5000);
    Fixed f("Meena", 100000);
    cout << "Savings withdraw 2000: " << s.withdraw(2000) << endl;
    cout << "Fixed withdraw 1: " << f.withdraw(1) << endl;
    cout << "Accounts opened: " << Account::opened() << endl;
    return 0;
}

Output:

Savings withdraw 2000: 1
Fixed withdraw 1: 0
Accounts opened: 2

2. A virtual addInterest() that does nothing in the base, and adds 4% in Savings.

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    string name;
    double balance;
public:
    Account(string n, double b) : name(n), balance(b) {}
    virtual ~Account() {}
    virtual void addInterest() {}
    friend ostream& operator<<(ostream& out, const Account& a) {
        out << a.name << ": Rs. " << a.balance;
        return out;
    }
};
class Savings : public Account {
public:
    Savings(string n, double b) : Account(n, b) {}
    void addInterest() override { balance += balance * 0.04; }
};
class Current : public Account {
public:
    Current(string n, double b) : Account(n, b) {}
};

int main() {
    Account* acc[2] = {new Savings("Asha", 5000), new Current("Ravi", 2000)};
    for (int i = 0; i < 2; i++) acc[i]->addInterest();
    for (int i = 0; i < 2; i++) {
        cout << *acc[i] << endl;
        delete acc[i];
    }
    return 0;
}

Output:

Asha: Rs. 5200
Ravi: Rs. 2000

3. Check the number before using it.

#include <iostream>
#include <string>
using namespace std;

int main() {
    double balances[2] = {5000, 2000};
    int n = 2;
    string cmd;
    while (cin >> cmd) {
        int no; double amt;
        cin >> no >> amt;
        if (no < 1 || no > n) {
            cout << cmd << ": no account " << no << endl;
            continue;
        }
        balances[no - 1] += amt;
        cout << cmd << ": done, Rs. " << balances[no - 1] << endl;
    }
    return 0;
}

Input:

deposit 1 500
deposit 9 100

Output:

deposit: done, Rs. 5500
deposit: no account 9

In the full bank, put the same if (no < 1 || no > n) check before every acc[no - 1].

Quick quiz

What does this print?

#include <iostream>
#include <string>
using namespace std;

class Account {
protected:
    double balance;
public:
    Account(double b) : balance(b) {}
    virtual double floor() const { return 0; }
    bool withdraw(double amt) {
        if (balance - amt < floor()) return false;
        balance -= amt; return true;
    }
};
class Savings : public Account {
public:
    Savings(double b) : Account(b) {}
    double floor() const override { return 1000; }
};

int main() {
    Savings s(3000);
    Account& a = s;
    cout << a.withdraw(2500) << a.withdraw(2000);
    return 0;
}
  • A. 11
  • B. 10
  • C. 00
  • D. 01
Answer

D: 01 (verified). floor() is virtual, so even through the Account& reference, the Savings version runs: 1000. Taking 2500 from 3000 would leave 500, below 1000: refused (0). Taking 2000 leaves exactly 1000, which is not below the floor: allowed (1).

Output:

01

Interview questions

  • Design a bank account system with OOP. An abstract Account with private/protected data, checked deposit/withdraw, a static ID counter, and pure virtual functions for type-specific rules; Savings, Current (and others) override them; a virtual destructor; accounts used through base pointers or references.
  • Why should balance be private? So every change passes through functions that enforce the rules (encapsulation). A public field can be set to any value from anywhere.
  • Why is withdraw in the base class but floor virtual? The steps of a withdrawal are the same for every account; only the limit differs. Putting the steps in one place avoids duplicated (and diverging) code: the template method pattern.
  • How do you make a transfer safe? Check that the withdrawal succeeded before depositing, refuse self-transfers, and (in real systems) make the two steps atomic: either both happen or neither.
  • Why use double for money here, and what would a real bank use? It keeps the example simple; real systems store an integer number of paise (like episode 17's Money) or a decimal type, because double can't represent most decimal fractions exactly.
  • What does the virtual destructor do in this program? delete acc[i] through an Account* runs the real object's destructor first, then the base one. Without it, deleting a derived object through a base pointer is undefined behaviour.
  • How would you extend this with a new account type? Write one new class that overrides floor() and type(); deposit, withdraw, transfer and the statement need no changes (open/closed principle).

Glossary

Term Plain meaning Example
Savings account A personal account that must keep a minimum balance floor 1000
Current account A business account that may go below zero up to a limit floor -5000
Minimum balance The lowest amount a savings account must keep Rs. 1000
Overdraft Borrowing from the bank by going below zero, up to a limit Ravi at Rs. -4000
Floor In this program: the lowest balance an account may reach virtual double floor() const = 0;
Statement A printout of accounts and their balances #1 Asha (Savings): Rs. 3500
Encapsulation Private data, changed only through the class's checked functions withdraw()
Invariant A rule that must always be true for every object balance never below floor
Abstract base class A class with a pure virtual function; a plan for children only Account
Pure virtual function A virtual function with = 0 and no body floor(), type()
Override A child's version of a virtual function Savings::floor()
Template method pattern The base class fixes the steps of an algorithm; children fill in details withdraw() calls floor()
Static counter One shared variable that numbers objects static int next;
Friend function A non-member trusted with private data friend ostream& operator<<
this Pointer to the object a member function was called on &to == this
Short-circuit evaluation || stops as soon as the left side is true self-transfer skips withdraw
Command loop A loop that reads commands and acts on each one while (cin >> cmd)
Virtual destructor Makes delete through a base pointer run the child's destructor too virtual ~Account() {}
Atomic operation Several steps that either all happen or none do a real bank transfer
Open/closed principle Open to new types, closed to edits of existing code adding Fixed

Summary

  • A public balance in a struct can be set to anything (-50000): encapsulation (private data, checked deposit/withdraw that return bool) makes the attack a compile error.
  • A static counter gives unique account numbers; a friend operator<< prints the statement.
  • Savings and current differ in one answer, the floor: a pure virtual floor() with override in each child keeps a single withdraw. Account is abstract, so new Account(...) is an error.
  • A transfer that ignores withdraw's answer creates money; check it, and compare with this to refuse self-transfers.
  • The full bank reads commands, creates accounts with new, uses them through Account*, and deletes them through a virtual destructor.

Next episode: templates. Our bank stores Account*; what if you want the same box, or the same max function, for int, double and string without writing it three times?