Step 2 of 9
public and private
A class is a struct whose members are private by default. Private members can only be touched by the class's own member functions. That lets the class protect its rules (its invariants):
class Account {
public:
void deposit(int amount) { if (amount > 0) balance_ += amount; }
int balance() const { return balance_; }
private:
int balance_ = 0; // nobody outside can break this
};
Your turn: write a class Wallet with a private int money_ starting at 800, and public methods:
int money() constreturns itbool buy(int price)subtracts the price and returnstrueif there's enough money, otherwise changes nothing and returnsfalsevoid earn(int amount)adds money, but the total can never exceed 16000