Reference solution
One correct implementation. Alternative approaches that satisfy the constraints are equally valid — the reviewer judges behaviour, not style.
#include <iostream>
using namespace std;
class BankAccount {
public:
void deposit(int amount) {
if (amount > 0) {
balance_ += amount;
}
}
bool withdraw(int amount) {
if (amount <= 0 || amount > balance_) {
return false;
}
balance_ -= amount;
return true;
}
int balance() const { return balance_; }
private:
int balance_ = 0;
};
int main() {
BankAccount a;
a.deposit(100);
a.withdraw(30);
cout << a.balance() << '\n';
return 0;
}