Hierarchical inheritance in C++
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Reading time: 20 minutes
Inheritance is the process of inheriting properties of objects of one class by objects of another class.
The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class.
When more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class.
Problems where hierarchy has to be maintained can be solved easily using this inheritance.
Some examples :
Civil, Computer, Mechanical, Electrical are derived from Engineer.
Natural language, Programming language are derived from Language.
Syntax :
class base_classname
{
properties;
methods;
};
class derived_class1:visibility_mode base_classname
{
properties;
methods;
};
class derived_class2:visibility_mode base_classname
{
properties;
methods;
};
... ... ...
... ... ...
class derived_classN:visibility_mode base_classname
{
properties;
methods;
};
Access Specifiers
There are 3 access specifiers for a class/struct/Union in C++. These access specifiers define how the members of the class can be accessed. Of course, any member of a class is accessible within that class(Inside any member function of that same class). Moving ahead to type of access specifiers, they are:
Public - The members declared as Public are accessible from outside the Class through an object of the class.
Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it.
Private - These members are only accessible from within the class. No outside Access is allowed.
Inheritance and Access Specifiers
Inheritance in C++ can be one of the following types:
1)Public Inheritance
2)Private Inheritance
3)Protected inheritance
Public Inheritance :
All Public members of the Base Class become Public Members of the derived class.
All Protected members of the Base Class become Protected Members of the Derived Class.
Private Inheritance :
All Public members of the Base Class become Private Members of the Derived class.
All Protected members of the Base Class become Private Members of the Derived Class.
Protected Inheritance :
All Public members of the Base Class become Protected Members of the derived class.
All Protected members of the Base Class become Protected Members of the Derived Class.
Example
// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle
{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
Output :
This is a Vehicle
This is a Vehicle
Question
Which type of inheritance leads to diamond problem?
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.