Inheritance
Ø Single level inheritance : In single level inheritance, there is only one base class and
has only one derived class.
Ø Multi-level inheritance : In multi level inheritance, there will be a chain of inheritance
with a class derived from only one parent and will have only one child class.
Example of multiple
inheritance:
Inheritance is the process of forming a new class
from an existing class or base class.
The base class is also known as parent class or
super class, the new class that is formed is called derived class.
Derived class is also known as a child class or
sub class. Inheritance helps in reducing the overall code size of the program,
which is an important concept in object-oriented programming.
It is classifieds into different types, they are
Ø Single level inheritance :
Example
of single level inheritance:
class Base
{
};
class Derv: public Base
{
{
};
class Derv: public Base
{
};
Ø Multi-level inheritance :
Example
of multi level inheritance:
class A
{
};
class B: public A
{
{
};
class B: public A
{
};
class C: public B
{
{
};
Ø Multiple
inheritance : One class being derived from multiple parent classes.
Example of multiple
inheritance:
class
A
{
};
class B
{
{
};
class B
{
};
class C: public B, public A
{
{
};
Ø Hybrid
inheritance : It is a
mixture of 2 or more of above types of inheritance. There is no pattern of
deriving from classes.
Example of hybrid inheritance:
Example of hybrid inheritance:
class
A
{
};
class B
{
{
};
class B
{
};
class C: public A, public B
{
{
};
class D: public A
{
{
};
class X: public D, public C
{
{
};
Ø Hierarchial
inheritance : Many
classes deriving from one class.
Example of hierarchical inheritance:
Example of hierarchical inheritance:
class
A
{
};
class B: public A
{
{
};
class B: public A
{
};
class C: public A
{
{
};
No comments:
Post a Comment