LOGBOOK

HELP

Quiz Entry - updated: 2026.07.06

How do you inherit from a class in C++?

Write class Derived : public Base { … };, and call the base constructor in the initializer list.

Vehicle base class with Truck and Bicycle deriving from it

* Public inheritance is an "is-a" relationship: Truck and Bicycle each derive from Vehicle, call its constructor, and override getName(). *

class Truck : public Vehicle {
public:
    Truck() : Vehicle(6) {}          // call base constructor
private:
    virtual string getName() { return "Truck"; }
};

class Bicycle : public Vehicle {
public:
    Bicycle() : Vehicle(2) {}
private:
    virtual string getName() { return "Bicycle"; }
};

Key points:

  • : public Base is public inheritance (the most common kind) — it models an "is-a" relationship (a Truck is a Vehicle)
  • The derived constructor must initialize the base part by calling the base constructor in its initializer list
  • Marking methods virtual lets a subclass override them and enables polymorphism

Go deeper:

From Quiz: REVE1 / C++ Programming | Updated: Jul 06, 2026