Skip to content
Exercise · Classes and OOP · lesson 7 of 15

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.

Do not add extra #include lines unless the task says so explicitly.

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

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way