Inheritance allows a new class (subclass) to adopt the features of an existing (base) class. The subclass can extend the base with new features or override behaviour without modifying the base class. It is a core concept of object-oriented programming (OOP) which promotes code reusability.
Inheritance is enabled using the : syntax:
class Base {};
class Derived : public Base {};Base classes with non-default constructors must be called in the derived class constructor initialiser list:
class Base {
public:
Base(int) {}
};
class Derived : public Base {
public:
Derived() : Base(0) {}
Derived(int x) : Base(val) {}
};Note
If the base class has a default constructor, it is called implicitly and does not need to be listed in the initialiser list.
Order
The order of construction/destruction is:
- Construct base object.
- Construct derived object.
- Destroy derived object.
- Destroy base object.
Access Specifier
The access specifier caps the access of members of base class members in the derived class:
class PublicDerived : public Base {};
class ProtectedDerived : protected Base {};
class PrivateDerived : private Base {};- In all three access types of inheritance, the private members are inaccessible to derived classes.
- Public inheritance does not change access to members of the base class.
- Protected inheritance causes public members of the base class to become protected.
- Private inheritance causes public and protected members of the base class to become private.
If the access specifier is omitted:
class Derived : Base {}; // Private inheritance by default.
struct Derived : Base {}; // Public inheritance by default.Final
The final specifier stops other classes from deriving that class:
class LastDerived final : public Base {};
class TryDerived : public LastDerived {}; // Compiler error.
Dan