Polymorphism lets code invoke a common interface while the object supplies its own implementation. In the blob-racing example, every blob can step and report its total steps, even when the blobs use different movement rules. section 6.1.1
The C++ pattern works through a small set of rules:
- Declare shared operations as virtual pure functions using = 0; this makes Blob abstract, so it cannot be instantiated directly. section 6.1.1
- Implement the operations in derived classes, allowing each blob to define its own step behavior. section 6.1.1
- Call a virtual function through a pointer or reference to Blob; the v-table selects the overridden function for the actual object. section 6.1.1
- Store different derived blobs behind a common base-pointer type to make them compete in one race; smart pointers avoid manual deletion. section 6.2.1
Different movement rules, one race
RandomBlob derives from Blob and receives a generator and distribution. One instance can use a uniform distribution from 0 to 4, while another uses a normal distribution with mean 2.0 and standard deviation 1.0. Both still respond to step() and total_steps(). A deterministic generator returning zero provides a simple test of the same interface.








