C/C++ Arena

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:

Previous: Member functions Next: Constructors