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.

🔑 Key line

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 + sending
class Report {
void generate() { /* query data */ }
void toHTML() { /* format */ }
void sendEmail() { /* smtp */ }
};
// GOOD: split by responsibility
class 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 added
double area(Shape s) {
if (s.type == CIRCLE)
... if (s.type == RECT)...
}
// GOOD: extend by adding new class
struct 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 contract
struct Bird {
virtual void fly() {}
};
struct Ostrich : Bird {
void fly() override {
throw;
}
}; // VIOLATION
// GOOD: separate interface
struct 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 fax
struct Machine {
virtual void print() = 0;
virtual void scan() = 0;
virtual void fax() = 0;
};
// GOOD: split interfaces
struct 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 concrete
class OrderService {
MySQLDB db;
void saveOrder(Order o) {
db.insert(o);
}
};
// GOOD: depend on interface
struct 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 OrderService2

What this lesson walks through

  1. 01SOLID — 5 principles that define maintainable OOP design
  2. 02S — Single Responsibility Principle
  3. 03O — Open/Closed Principle
  4. 04L — Liskov Substitution Principle
  5. 05I — Interface Segregation + D — Dependency Inversion
  6. 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.

← Previous
Debugging: Service Isn't Receiving Data — Trace the Receive Path
Next →
Thread-Safe Singleton