Single and Multiple Inheritance
Model is-a relationships with single inheritance and tame multiple inheritance pitfalls.
Single and Multiple Inheritance is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Inheritance?
Inheritance lets one class (the derived class) reuse and extend another (the base class). It models "is-a" relationships.
class Animal {
public:
void breathe() { std::cout << "breathing\n"; }
};
class Dog : public Animal {
public:
void bark() { std::cout << "Woof!\n"; }
};Public Inheritance
Use public to model "is-a" — a Dog is an Animal. Members keep their access level in the derived class.
Protected and Private Inheritance
protected and private inheritance model "is-implemented-in-terms-of" — rarely used. Prefer composition for those cases.
Accessing Base Members
The derived class can use any public/protected member of the base directly.
Dog d;
d.breathe(); // inherited from Animal
d.bark(); // own memberConstructor Order
Base constructors run before derived constructors. Use the member initializer list to pass arguments to the base.
class Pet : public Animal {
std::string name_;
public:
Pet(std::string n) : Animal(), name_(std::move(n)) {}
};Destructor Order
Destructors run in reverse order — derived first, then base. Make base destructors virtual if you delete via a base pointer.
Multiple Inheritance
C++ allows a class to inherit from more than one base. Use it sparingly — it complicates the design.
class Printable { public: virtual void print() = 0; };
class Serializable { public: virtual std::string serialize() = 0; };
class Document : public Printable, public Serializable {
// implements both interfaces
};The Diamond Problem
If two base classes share a common base, the derived class ends up with two copies of that base. Resolve with virtual inheritance.
class A {};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {}; // D has ONE A, not twoVirtual Inheritance Cost
Virtual base classes add overhead — an extra pointer per object and an extra indirection on each base member access. Avoid when you can.
Composition vs Inheritance
Often, "has-a" composition is simpler than "is-a" inheritance. A Car has an Engine — do not inherit Car from Engine.
class Engine { /* ... */ };
class Car {
Engine engine_; // composition
};Interface Inheritance vs Implementation Inheritance
Two common uses:
- Interface — derive from a pure-virtual base to fulfill a contract
- Implementation — derive to reuse code (CRTP, mixins)
When to Use Inheritance
Use inheritance when:
- A real "is-a" relationship exists
- You need runtime polymorphism via virtual functions
- The base class is designed for inheritance
Quick Check
Which technique prevents two copies of a common base class when multiple inheritance is used?
Recap
Inheritance models "is-a" relationships with public inheritance. Multiple inheritance is allowed but the diamond problem requires virtual inheritance. Prefer composition for "has-a" relationships and code reuse.
Frequently asked questions
Is the “Single and Multiple Inheritance” lesson free?
Yes — the full text of “Single and Multiple Inheritance” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Single and Multiple Inheritance”?
Model is-a relationships with single inheritance and tame multiple inheritance pitfalls. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Single and Multiple Inheritance” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Single and Multiple Inheritance
- Virtual Functions and the vtable
- Abstract Classes and Pure Virtual Methods
- Inheritance vs Composition Trade-offs