Pages

Wednesday, June 29, 2011

Types Of Inheritance

Inheritance Types

There are many different types of inheritances each with its own features and benefits. The following are the types of inheritances available in C++.
  •  Single Inheritance
Single Inheritance (Base Class - A, Child Class B)

When a child class is declared using the properties of a single base class, it is known as Single Inheritance. In this type of inheritance, the child class can access all the public and protected members of the parent as if they are its own.
Syntax
class child : access_specifier baseclass

Example
class student : public person

  •  Multiple Inheritance
Multiple Inheritance : Base Classes A & B
Child Class - C
When a child class is declared using the properties of more than one base class, it is termed as Multiple inheritance. In this case, the child class benefits more than it does in Single Inheritance since now it can access the members of more the one class. A class can inherit from as many classes as required. Similar to Single Inheritance, the child class can access only the  public and protected members of its parents.


Syntax
class child : access_specifier baseclass1, access_specifier baseclass2....
Example
class bike : public vehicle, public automobile

  • Multilevel Inheritance
B is both a parent and
a child
If a child class also acts as a parent class for another class, it is called Multilevel inheritance. In this type of Inheritance the intermediate class is both a child and a parent. 
In this type of inheritance, there are number of level and it has used in that cases where we want to use all properties in number of levels according to the requirement. For example, class A inherited in class b and class b has inherited in class c for class b so on. Where class A is base class c. In another way we can say b is derived class a base class for c and a indirect base class for c is indirect base class for c and c indirect derived class for class A.



  • Hierarchical Inheritance:
Class A has multiple child classes
This type of inheritance helps us to create a baseless for number of classes and those numbers of classes can have further their branches of number of class.

  • Hybrid Inheritance:
In this type of inheritance, we can have combinations of different types of inheritances. For example if there are many classes in a program some of them single inheritance while others can have multiple inheritance.

0 comments:

Post a Comment