Step 8 of 8
Challenge: program to an interface
In enterprise code, the most valuable use of virtual functions isn't game units: it's dependency injection. A class depends on an abstract interface instead of a concrete service, and whoever creates it passes in the real thing.
class Clock { public: virtual ~Clock() = default; virtual long now() const = 0; };
class Session { public: explicit Session(const Clock& c); /* uses c.now() */ };
Production code passes a clock that reads the system time. Tests pass a fake clock they control, so time-dependent logic can be tested instantly and reliably. The same idea covers databases, network clients and file systems.
Your turn: write class RateLimiter that takes a const Clock& and allows at most limit calls to allow() in any window of window seconds. allow() returns true and records the call if under the limit, otherwise false. A call at time t falls out of the window once now() >= t + window.