Inheritance

Lesson 14 of 20

class Dog : public Animal — children inherit everything the parent has, then add their own twist on top.

The big picture

Reuse and extend

Inheritance lets a class start from another class: keep what exists, add what is new.

Base class

The parent (Animal) — it holds the members every child should share, like name and eat().

Derived class

class Dog : public Animal — Dog begins with all of Animal’s public members, already written.

The is-a rule

Every Dog is an Animal. Inheritance models "is-a"; a Dog can go anywhere an Animal is expected.

protected:

Members marked protected are visible to child classes but hidden from outside code.