June 2025
Intermediate to advanced
1093 pages
33h 24m
English
In C++, you represent inheritance as follows:
class Car { // Base or superclasspublic: Windscreen windscreen_; std::vector<Wheel> wheels_; // …};class VwBus : public Car { // VwBulli is a subclasspublic: Flowers flowers_; // …};
So you write the name of the base class separated by a colon : and public after the name of the current class:
class Subclass : public Baseclass { …
Now you can happily create new VwBus instances. And each of them automatically has a windscreen_ and wheels_, even though you didn't explicitly mention it in the VwBus class:
Vwbus vw{};cout << vw.windscreen_;cout << vw.flowers_;
If you create a pure Car car;, then car.windscreen_ will be a valid access, but in the class Car, car.flowers_ does ...
Read now
Unlock full access