Skip to content
Exercise · Classes and OOP · lesson 6 of 13

Exercise: bank account

Write the class Account, a bank account; add the fields yourself. The method void deposit(int amount) puts an amount into the account, bool withdraw(int amount) takes it out and returns true, and int balance() const returns the balance.

  • for a new account, balance() → 0
  • after deposit(100) and withdraw(30): balance() → 70
  • with a balance of 70, withdraw(80) → false, and the balance stays the same

Constraints: amounts are positive; if the amount exceeds the balance, withdraw takes nothing out and returns false.

account.cpp — cpp
cpp · 14 lines
Ctrl+Enter run · Esc leave editor