Checkpoint: The Code So Far (After OCP)

Everything covered through the Open-Closed Principle: a bird hierarchy that's now open for extension, one abstract class and one subclass per bird.

September 14, 20264 min read6 / 20

This is a running snapshot, not a new lesson. It's the full Bird hierarchy as it stands after the previous post: one abstract Bird class, and one subclass per bird that can actually fly.

This design fixes both problems from before: single responsibility and open-closed. But it breaks again the moment a bird that can't fly shows up. A penguin still has to write a fly() method, and there's no honest way to write one. That's the next principle's problem.

main.cpp
#include <vector> #include <memory> #include "bird/Bird.h" #include "bird/Crow.h" #include "bird/Sparrow.h" #include "bird/Owl.h" int main() { std::vector<std::unique_ptr<Bird>> birds; birds.push_back(std::make_unique<Crow>()); birds.push_back(std::make_unique<Sparrow>()); birds.push_back(std::make_unique<Owl>()); for (auto& bird : birds) { bird->walk(); bird->fly(); } return 0; }