Inheritance in C++
From the C++ cheat sheet · Inheritance & Polymorphism · verified Jul 2026
Inheritance
Deriving classes and reusing behavior.
cpp
// Quick Reference
class Animal {
public:
Animal(std::string n) : name(n) {}
protected:
std::string name;
};
class Dog : public Animal {
public:
Dog(std::string n) : Animal(n) {}
};💡 A derived class must initialize its base via the base constructor in the init list.
⚡ protected members are visible to derived classes but not to outside code.
📌 public inheritance models "is-a"; prefer composition ("has-a") when that fits better.
🟢 C++ supports multiple inheritance, but keep hierarchies shallow to avoid ambiguity.
inheritance