One of the four foundations of object-oriented programming (OOP) is inheritance. Inheritance is the ability for one class to acquire properties and characteristics of another class. Inheritance allows derived or child classes to inherit and reuse members of the base class, allowing web developers to reuse code.
To understand the concept of inheritance,
consider a real-world example. For example, speaking, walking, and eating.
However, these properties are not specifically inherited only by his parents.
His parents inherit these properties from another class: mammals. This mammal
class also inherits these properties from the animal class. Inheritance works
the same way.
Single
Inheritance
Of all the inheritance kinds in C++, single
inheritance is the most basic. The properties of a base class are passed down
to a single class in this inheritance scheme. The derived class can access all
of the base class's data members in accordance with the visibility
mode—private, protected, or public—that was specified during inheritance.
Example:
class base_class_one
{
// class
definition
};
class derived_class: visibility_mode base_class_one
{
// class
definition
};
Multiple
Inheritances
Multiple inheritance is a type of inheritance
where a class can have multiple base classes or inherit or derive properties
from many classes. In inheritance, access specifiers are specified separately
for each base class. A derived or child class can access data members of all
base classes according to access specifiers, and a derived class can derive
combined characteristics of all these classes.
Example:
class base_class_one
{
// class definition
};
class base_class_two
{
// class definition
};
class derived_class:
visibility_mode_one base_class_one, visibility_mode_two base_class_two
{
// class definition
};
Multilevel
Inheritance
Inheritance that allows a class to be derived
from another derived class is called multilevel inheritance. Well, let's say we
have three classes A, B and C.
A is a base class derived from class B.
Therefore, B is a derived class of A. Now C is a class derived from class B.
This makes class B a base class for class C, but a derived class for class A.
This scenario is called multilevel inheritance.
Data members of each base class are accessed by
their respective derived classes according to the specified visibility mode.
Example:
class class_DemoA
{
// class definition
};
class class_DemoB: visibility_mode
class_DemoA
{
// class definition
};
class class_DemoC:
visibility_mode class_DemoB
{
// class definition
};
Hybrid
Inheritance
As the name suggests, a hybrid inheritance
consists in two or more types of inheritance. For example, classes in a program
are arranged so that they have both simple inheritance and hierarchical
inheritance. Such an arrangement is known as hybrid inheritance. This is
probably the most complex of all types of inheritance in C++. Data members of
the base class are accessed according to the specified visibility mode.
Example:
class class_DemoA
{
// class definition
};
class class_DemoB:
visibility_mode class_DemoA
{
// class definition
};
class class_DemoC :
visibility_mode class_DemoA
{
// class definition
};
class class_DemoD:
visibility_mode class_DemoB
{
// class definition
};
class class_DemoE:
visibility_mode class_DemoC
{
// class definition
};
Hierarchical
Inheritance
Inheritance where a single base class inherits
multiple derived classes is called hierarchical inheritance. This inheritance
has a tree-like structure because each class acts as a base class for one or
more of his child classes. The visibility mode of each derived class is
specified separately during inheritance and accesses
data members accordingly.
Example:-
class class_DemoA
{
// class definition
};
class class_DemoB
{
// class
definition
};
class class_DemoC: visibility_mode class_DemoA,
visibility_mode class_DemoB
{
// class definition
};
class class_DemoD: visibility_mode class_DemoC
{
// class definition
};
class class_DemoE: visibility_mode class_DemoC
{
// class definition
};
For More Information:
Call us @ +91 9825618292
Visit us @ http://tccicomputercoaching.com
No comments:
Post a Comment