C/C++ Arena

Step 7 of 8

Composition over inheritance

Inheritance means is-a: a Pistol is a Weapon. For has-a relationships, use a member instead. That's composition:

class Squad : public std::vector<Player> { };   // wrong: a squad isn't a vector
class Squad { std::vector<Player> members_; };   // right: a squad has members

Why composition is the default in professional code:

Your turn: write class Squad that has a std::vector<std::string> of names, with a maximum size of 5:

Previous: Calling the base version, and protected Next: Challenge: program to an interface