Inheritance in c++ is a process of reusing and extending existing classes without modifying them .
It Is The Process Of Creating A New Class From an Existing Class (Derived Class).
Derived Class Inherits All The Property Of Base Class.
Most Features Of Inheritance. ” Code Reusability “.
syntax of inheritance in c++:
Class Derived Classname : Visibility_mode baseclass name { variable declaration function declaration };
Visibility_mode:
There are three types of visibility:
- Private
- Public
- Protected
1) Private
Accessible In Own Class.
2) Public
Accessible In Whole Program.
3) Protected
Accessible In Own class & It’s Derived Class.
If class B inherits from class A, this means that B has as a parent A. We say that B is a derived class from A, and A is the base class.
Also Read This :- Access specifiers in c++
class A //base class { public: int p1; protected: int p2; private: int p3; }; //Make B inherit publicly (default) from A class B : A //derived class { };
Types Of Inheritance in c++ :
- Single Inheritance.
- Multilevel Inheritance.
- Multiple Inheritance.
- Hierarchical Inheritance.
- Hybrid Inheritance.
1) single inheritance
When a class inherits from a single base/parent class, it is known as single inheritance. The following program shows the single inheritance.
class base_class1 { . //members of base_class1 . }; class base_class2: access_specifier1 base_class1 { . //members of base_class2 . };
access_specifier1 :- public, private, Protected
2) Multilevel inheritance
When one class is inherited from another class, which in turn is inherited from some other class, it is referred to as multilevel inheritance.
Multilevel inheritance comprises two or more levels.
Syntax of Multilevel inheritance
class base_class1 { . //members of base_class1 . }; class base_class2: access_specifier1 base_class1 { . //members of base_class2 . }; class derived_class: access_specifier2 base_class2 { . //members of derived_class . };
3) Multiple Inheritance
When a derived class inherits from more than one base class, it is multiple inheritance.
In multiple inheritance, the derived class inherits the members of all its base classes and can directly access the public and protected members of its base classes.
class base_class1 { . //members of base_class1 . }; class base_class2 { . //members of base_class2 . }; class derived_class: access_specifier1 base_class1, access_specifier2 base_class2 { . //members of derived_class . };
4) Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance in which more than one class is
derived from a single base class.
In hierarchical inheritance, a base class provides members that are common to all of its derived classes.
class base_class { . //members of base_class . }; class derived_class1: access_specifer1 base_class { . //members of derived_class1 . }; class derived_class2: access_specfier2 base_class { . //members of derived_class2 . };
5) Hybrid Inheritance
the various types of inheritance can be combined in a program, it is called hybrid inheritance