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. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Single and Multiple Inheritance” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Single and Multiple Inheritance”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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