TExcelTAD
Home › C++ › C++ Inheritance

C++ Inheritance

Inheritance lets you create a new class from an existing one. The new derived class (child) automatically gets the members of the base class (parent) and can add its own or change inherited behaviour. Inheritance models an "is-a" relationship: a Car is a Vehicle, a Manager is an Employee.

Basic syntax

cpp · try it yourself
#include <iostream>
#include <string>
using namespace std;

class Vehicle {                  // base class
public:
    string brand = "Generic";
    void honk() const {
        cout << brand << ": Beep beep!\n";
    }
};

class Car : public Vehicle {     // Car inherits from Vehicle
public:
    int doors = 4;
};

int main() {
    Car car;
    car.brand = "Toyota";        // inherited member
    car.honk();                  // inherited method
    cout << "Doors: " << car.doors << "\n";
}
Toyota: Beep beep!
Doors: 4

class Car : public Vehicle means "Car is a Vehicle, and the public members of Vehicle stay public in Car". This public inheritance is what you want in almost every case.

Constructors in derived classes

The base part of an object is built first. A derived constructor passes arguments to the base constructor in its initializer list. Destruction happens in the reverse order:

cpp · try it yourself
#include <iostream>
#include <string>
using namespace std;

class Animal {
public:
    Animal(string n) : name(n) { cout << "Animal(" << name << ")\n"; }
    ~Animal() { cout << "~Animal(" << name << ")\n"; }
protected:
    string name;
};

class Dog : public Animal {
public:
    Dog(string n, string b) : Animal(n), breed(b) { cout << "Dog(" << breed << ")\n"; }
    ~Dog() { cout << "~Dog(" << breed << ")\n"; }
    void describe() const { cout << name << " is a " << breed << "\n"; }
private:
    string breed;
};

int main() {
    Dog d("Bella", "beagle");
    d.describe();
}
Animal(Bella)
Dog(beagle)
Bella is a beagle
~Dog(beagle)
~Animal(Bella)

Because name is protected, the derived class Dog can use it, but outside code cannot.

Overriding (hiding) inherited behaviour

A derived class can define a method with the same name to replace the inherited version. It can still call the base version with Base::method():

cpp · try it yourself
#include <iostream>
#include <string>
using namespace std;

class Employee {
public:
    Employee(string n, double s) : name(n), salary(s) {}
    double pay() const { return salary; }
    void print() const { cout << name << " earns " << pay() << "\n"; }
protected:
    string name;
    double salary;
};

class Manager : public Employee {
public:
    Manager(string n, double s, double b) : Employee(n, s), bonus(b) {}
    double pay() const { return Employee::pay() + bonus; }   // extends base behaviour
    void print() const { cout << name << " (manager) earns " << pay() << "\n"; }
private:
    double bonus;
};

int main() {
    Employee e("Noor", 3000);
    Manager m("Ivan", 4000, 1500);
    e.print();
    m.print();
}
Noor earns 3000
Ivan (manager) earns 5500

Note: Without the virtual keyword this only works when the compiler knows the object's real type. If you handle a Manager through an Employee&, the base pay() would be called. The fix — virtual functions — is the subject of the Polymorphism lesson.

Multilevel and multiple inheritance

Inheritance can go several levels deep (Puppy → Dog → Animal). C++ also allows a class to inherit from several base classes at once:

cpp · try it yourself
#include <iostream>
using namespace std;

class PagePrinter {
public:
    void print() const { cout << "printing page\n"; }
};

class PageScanner {
public:
    void scan() const { cout << "scanning page\n"; }
};

class OfficeMachine : public PagePrinter, public PageScanner {
public:
    void copy() const {
        scan();
        print();
    }
};

int main() {
    OfficeMachine m;
    m.copy();
}
scanning page
printing page

Multiple inheritance is powerful but can get complicated (for example, when two bases share a common ancestor — the "diamond problem"). It is most commonly used to combine interfaces.

Access in inheritance

Base member is… Accessible in derived class? Accessible from outside (public inheritance)?
public yes yes
protected yes no
private no no

Inheritance vs composition

Inheritance is for "is-a". For "has-a" relationships use composition — make the other object a member. A Car has an Engine; it is not an engine. When in doubt, prefer composition: it couples classes less tightly.

Test yourself

Which constructor runs first when creating a derived object?

  1. The derived constructor
  2. The base constructor
  3. Whichever is declared first in the file
  4. They run in parallel

Can a derived class access private members of its base class?

  1. Yes, always
  2. No, only public and protected members
  3. Only with public inheritance
  4. Only in the constructor

Which relationship is best modelled with composition rather than inheritance?

  1. Cat / Animal
  2. Square / Shape
  3. Car / Engine
  4. Manager / Employee