Classes and basic OOP

A class is a blueprint for objects that bundle state (data) and behaviour (methods) together.

Implement a BankAccount class with:

  • A constructor that takes an initial balance
  • A deposit(amount) method that adds to the balance
  • A withdraw(amount) method that subtracts from the balance (do nothing if amount > balance)
  • A getBalance() method that returns the current balance

Your program receives a comma-separated sequence of operations:

  • start:<n> — create account with balance n
  • deposit:<n> — deposit n
  • withdraw:<n> — withdraw n
  • balance — print the current balance
Input:  start:100,deposit:50,withdraw:30,balance
Output: 120

Constraints

  • Withdraw is a no-op if amount exceeds balance (balance doesn't go negative).
  • Only print output for balance operations.
Ad SpaceGoogle AdSense active for guests & free-tier learners.