Build a Bank Account System
Time to put your OOP knowledge to the test! You'll build two classes that model
a real banking system — a base Account class and a
SavingsAccount class that extends it.
YOUR TASK
Part A — Account class
Private members: string owner, double balance,
vector<string> history
Constructor: Account(string owner, double initialBalance)
void deposit(double amount) — adds to balance, records
"Deposit: X" in history
void withdraw(double amount) — subtracts from balance only if
funds are sufficient, records "Withdrawal: X"; otherwise prints
"Insufficient funds"
void printStatement() const — prints owner, balance, then every
history entry
Part B — SavingsAccount class (inherits Account)
Additional private member: double interestRate
Constructor: SavingsAccount(string owner, double balance, double rate)
void applyInterest() — deposits balance * interestRate
(call deposit() with that amount)
In main, run exactly this sequence:
Create Account acc("Alice", 1000)
acc.deposit(500)
acc.withdraw(200)
acc.withdraw(2000) — should print Insufficient funds
acc.printStatement()
Create SavingsAccount sav("Bob", 2000, 0.05)
sav.applyInterest()
sav.printStatement()
class Account {
protected:
string owner;
double balance;
vector<string> history;
public:
Account(string o, double b) : owner(o), balance(b) {}
void deposit(double amount) {
balance += amount;
history.push_back("Deposit: " + to_string((int)amount));
}
void withdraw(double amount) {
if (amount > balance) { cout << "Insufficient funds" << endl; return; }
balance -= amount;
history.push_back("Withdrawal: " + to_string((int)amount));
}
void printStatement() const { ... }
};
Use protected (not private) on Account members so
SavingsAccount can access balance directly for the interest calculation.
class SavingsAccount : public Account {
private:
double interestRate;
public:
SavingsAccount(string o, double b, double r)
: Account(o, b), interestRate(r) {}
void applyInterest() {
deposit(balance * interestRate);
}
};
void printStatement() const {
cout << "Owner: " << owner << endl;
cout << "Balance: " << balance << endl;
cout << "--- Transactions ---" << endl;
for (const string& t : history)
cout << t << endl;
}
Note: to_string((int)amount) converts the deposit/withdrawal
amount to a whole-number string to keep the output clean.
Insufficient funds
Owner: Alice
Balance: 1300
--- Transactions ---
Deposit: 500
Withdrawal: 200
Owner: Bob
Balance: 2100
--- Transactions ---
Deposit: 100
Check My Code