Inheritance allows one class to incorporate another class into its declaration. In a situation where two or more classes might have similar attributes, inheritance allows this sharing of common hierarchical functionality. This is usually achieved by creating a base class containing all the common attributes and then allowing the derived class to inherit these shared attributes and define its unique attributes.
When defining a new derived class a colon is placed after the derived class name followed by the level of access ( public, protected, or private ) and then the name of the base class.
In the worked example below two derived classes: company and personnel, are derived from the base class company. Instantiating the derived class results in the instantiation of the base class -
#include <iostream> using namespace std; class company//define base class { public: company() { cout << "base class" << endl; } }; class personnel: public company//define inherited class personnel { public: personnel() { cout << "derived class personnel" << endl; } }; class sales: public company//define inherited sales { public: sales() { cout << "derived class sales" << endl; } }; int main() { sales sales_y1;//instantiate inherited class sales personnel personnel_y1;//instantiate inherited class personnel return 0; }
Public, Protected and Private Inheritance
When one class inherits another, the base class members become members of the derived class. These base class members can only be accessed from within the derived class if the access specifier allows them to be inherited from the base class. The type of inheritance is specified by the access specifier which may be public, protected, or private
- Public Inheritance − public members of the base class become public members of the derived class, protected members of the base class become protected members of the derived class, and private base class members are never directly accessible from the derived class.
- Protected Inheritance − public and protected members of the base class become protected members of the derived class.
- Private Inheritance − public and protected base class members become private members of the derived class.
Multiple Inheritance
A C++ class can inherit members from more than one class with each base class being separated in the derived class declaration by a comma -
class BaseA { }; class BaseB { }; Class Derived: public BaseA,public BaseB { }
Passing Parameters during Base Class Initialisation
If a base class contains an overloaded constructor that requires arguments at the time of instantiation then the base class can be initialised by invoking the appropriate base class constructor via the constructor of the derived class-
class Base { public: Base(int value) // overloaded constructor { } }; Class Derived: public Base { public: Derived(): Base(5) // instantiate Base with argument 5 { // derived class constructor code } }
Avoiding Inheritance Using final
In C++11 the ability to prevent inheriting from a class or to prevent the overriding of a single method is done with the special identifier final. Any attempts to derive a class from a base class marked as final will result in a compiler error -
class Base final// no class can be derived from class Base { method(); }; class Base { virtual void method() final;// no class can override method() };
Override
The override identifier makes the compiler check the base class(es) to see if there is an equivalent virtual function to that in the derived class. If there is not, the compiler will indicate an error.
class Base { virtual void method(int); }; class Derived : public Base { virtual void method(float) override; // This will produce an error };
Constructors and Destructors
In C++ more than one constructor is called when an object of a derived class is created. When any derived class is instantiated the constructor and deconstructor of the base call will also be called. Base class objects are instantiated before the derived class. The destruction sequence is the reverse order to construction with the derived class objects going out of scope before the base class.