Inheritance
Inheritance is the process of creating a new class from existing class/classes. The existing class is known as the base/super/parent class and newly created class is known as derived/sub/child class.
The derived class will inherits the properties of base class.
Advantages of Inheritance are given below:
Reusability: It helps the code to be reused in derived class. The base class is defined and once it is compiled, it needs not to be reworked.
Transitivity: If class B inherits properties of another class A, then all subclasses of class B will automatically inherits the properties of A. It is called transitive property.
Types of Inheritance:
1. Single inheritance:- When a sub class inherits only form one base class, is known as single inheritance.
2. Multiple Inheritance:- When a sub class inherits from multiple base classes, is known as multiple inheritance.
3. Hierarchical Inheritance:- When many sub classes inherit from a single class, it is known as hierarchical inheritance.
4. Multilevel Inheritance:- When a class inherit from a class that itself inherits from another class it is known as a multilevel inheritance.
5. Hybrid Inheritance: It is a combination of 2 or more of above types of inheritance. There is no pattern of deriving from classes.
Syntax for defining a derived class:
class:
{
//Data members of derived class
//member functions of derived class
};
Visibility modes
The visibility mode in the definition of the derived class specifies whether the features of the base class are privately derived or publicly derived or protected derived.