Hierarchical inheritance
In hierarchical inheritance, we have one base class and two or more derived classes. All the derived classes inherit the properties of the single base class.
Class “A” is the base class and classes “B”, “C” and “D” are the derived classes. Classes “B”, ”C” and “D” inherit the properties of the base class “A”.
The syntax for defining a base class and derived class
//base class
class classA
{
private:
Variables/methods;
protected:
Variables/methods;
public:
Variables/methods;
};
//base class
class classB:private/protected/public classA
{
private:
Variables/methods;
protected:
Variables/methods;
public:
Variables/methods;
};
//derived class
class classC : private/protected/public clasA
{
private:
Variables/methods;
protected:
Variables/methods;
public:
Variables/methods;
};
class classD : private/protected/public clasA
{
private:
Variables/methods;
protected:
Variables/methods;
public:
Variables/methods;
};