design patterns · high
SOLID Principles — C++ Examples, Violations, and Interview Self-Check
SOLID OOP principles. S (Single Responsibility): one class, one reason to change; split fat classes by domain. O (Open/Closed): extend by adding new class (polymorphism/strategy); never switch-on-type; new feature = new class, no editing existing code. L (Liskov Substitution): derived must honor base contract; no throw/no-op overrides; if D can't honor B's contract — fix the hierarchy (use composition or separate interface). I (Interface Segregation): thin interfaces per role; clients implement only what they use; fat interface = forced stub implementations = ISP violation. D (Dependency Inversion): high-level modules depend on interfaces, not concretes; inject dependencies via constructor; enables mocking and testing. Violation signals: God class, switch-on-type, throw in override, fat interface, new ConcreteDB() in business logic class body.
SOLID: S=one reason to change; O=extend by adding (not editing); L=derived safely replaces base; I=thin interfaces; D=depend on abstractions not concretes (inject via ctor). Violations: God class, switch-on-type, throw in override, fat interface, new ConcreteType in ctor.
The code
// SOLID Principles — C++ examples
// S — Single Responsibility: one class, one reason to change// BAD: one class handles data + formatting + sendingclass Report { void generate() { /* query data */ } void toHTML() { /* format */ } void sendEmail() { /* smtp */ }};// GOOD: split by responsibilityclass ReportGenerator { void generate();};class ReportFormatter { void toHTML();};class ReportMailer { void send();};
// O — Open/Closed: open for extension, closed for modification// BAD: switch on type — must edit when new type addeddouble area(Shape s) { if (s.type == CIRCLE) ... if (s.type == RECT)...}
// GOOD: extend by adding new classstruct Shape { virtual double area() const = 0;};struct Circle : Shape { double area() const override { return pi * r * r; }};struct Rect : Shape { double area() const override { return w * h; }};
// L — Liskov Substitution: derived must safely replace base// BAD: Ostrich can't fly — violates Bird contractstruct Bird { virtual void fly() {}};struct Ostrich : Bird { void fly() override { throw; }}; // VIOLATION
// GOOD: separate interfacestruct Animal { virtual void move() {}};struct FlyingBird : Animal { virtual void fly() {}};struct Ostrich2 : Animal { void move() override {}}; // only move, no fly
// I — Interface Segregation: don't force unused methods// BAD: every device must implement scan AND print AND faxstruct Machine { virtual void print() = 0; virtual void scan() = 0; virtual void fax() = 0;};
// GOOD: split interfacesstruct Printer { virtual void print() = 0;};struct Scanner { virtual void scan() = 0;};struct SimplePrinter : Printer { void print() override {}}; // only what it needs
// D — Dependency Inversion: depend on abstractions, not concretes// BAD: high-level tied to concreteclass OrderService { MySQLDB db; void saveOrder(Order o) { db.insert(o); }};
// GOOD: depend on interfacestruct IDatabase { virtual void insert(Order) = 0;};class OrderService2 { IDatabase& db_; OrderService2(IDatabase& db) : db_(db) {} // inject via ctor void saveOrder(Order o) { db_.insert(o); }};// Now: swap MySQLDB → MockDB → RedisDB without touching OrderService2What this lesson walks through
- 01SOLID — 5 principles that define maintainable OOP design
- 02S — Single Responsibility Principle
- 03O — Open/Closed Principle
- 04L — Liskov Substitution Principle
- 05I — Interface Segregation + D — Dependency Inversion
- 06SOLID interview: spot the violation + quick self-check
SOLID is the most asked design question in senior C++ interviews. Interviewers ask you to explain each principle with a real C++ example and identify violations. The 5 principles together minimize coupling, maximize cohesion, and make code open to extension without modification. Every violation creates a maintenance cost — new features require touching old code, tests break, bugs multiply.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of SOLID Principles — C++ Examples, Violations, and Interview Self-Check and 100+ animated C++ interview lessons.