CppInheritance

Inheritance

Inheritance lets one class (the derived class) acquire the members and behavior of another class (the base class). It models an “is-a” relationship — a Circle is a Shape, a SavingsAccount is a BankAccount — and lets shared behavior live in one place instead of being duplicated across similar classes.

Basic syntax

class Derived : public Base

CPP
class Animal {
public:
    std::string name;

    Animal(std::string n) : name(n) {}

    void eat() const {
        std::cout << name << " is eating.\n";
    }
};

class Dog : public Animal {
public:
    Dog(std::string n) : Animal(n) {} // constructor chaining — see below

    void bark() const {
        std::cout << name << " says woof!\n";
    }
};

int main() {
    Dog d("Rex");
    d.eat();  // inherited from Animal
    d.bark(); // defined in Dog
}
What gets inherited

A derived class inherits all of a base class's public and protected members. private members of the base class exist in the derived object's memory layout, but the derived class has no direct access to them — they can only be reached through the base class's own public/protected methods.

Constructor chaining

A derived class doesn't inherit constructors automatically — it must explicitly call a base-class constructor from its own member initializer list, exactly like Dog(std::string n) : Animal(n) does above. If you don't call one explicitly, the base class's default constructor is called implicitly before the derived class's own initializer list runs.

Method overriding

A derived class can provide its own version of a method that a base class already defines, replacing (overriding) that behavior for objects of the derived type.

Overriding a base class method

CPP
class Animal {
public:
    void speak() const {
        std::cout << "Some generic animal sound\n";
    }
};

class Cat : public Animal {
public:
    void speak() const { // overrides Animal::speak for Cat objects
        std::cout << "Meow\n";
    }
};

int main() {
    Cat c;
    c.speak(); // Meow — Cat's own version is used
}
Note
This kind of overriding, based purely on the static type of the variable, is not yet **polymorphic** — calling through a base-class pointer or reference behaves differently unless the method is marked `virtual`. That distinction is the subject of the next two pages, Polymorphism and Virtual Functions.
Types of inheritance

The access specifier used at the point of inheritance (public, protected, or private) controls how the base class's members appear in the derived class.

Inheritance type

public base members become

protected base members become

public

public

protected

protected

protected

protected

private

private

private

Note
`public` inheritance is used in the overwhelming majority of C++ code and is the only kind that faithfully models “is-a” (a `Dog` really is an `Animal`, usable anywhere an `Animal` is expected). `protected` and `private` inheritance model much rarer “implemented-in-terms-of” relationships and are intentionally uncommon — when in doubt, use `public`.
What's next
  • Inheritance is the mechanism; Polymorphism is the payoff — calling the right derived-class behavior through a base-class pointer or reference.