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.
* 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 Baseis public inheritance (the most common kind) — it models an "is-a" relationship (aTruckis aVehicle)- The derived constructor must initialize the base part by calling the base constructor in its initializer list
- Marking methods
virtuallets a subclass override them and enables polymorphism
Go deeper:
Derived classes — cppreference — inheritance and how the access specifier changes what's inherited.
Inheritance (OOP) — Wikipedia — the "is-a" concept across languages.