0% found this document useful (0 votes)
21 views2 pages

Inheritance

The document outlines five types of inheritance in C++: Single, Multiple, Hierarchical, Multilevel, and Hybrid (Virtual) Inheritance. Each type is defined with examples, illustrating how classes can inherit properties from one or more base classes. The document emphasizes the structural relationships between classes in each inheritance type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Inheritance

The document outlines five types of inheritance in C++: Single, Multiple, Hierarchical, Multilevel, and Hybrid (Virtual) Inheritance. Each type is defined with examples, illustrating how classes can inherit properties from one or more base classes. The document emphasizes the structural relationships between classes in each inheritance type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

In C++, we have 5 different types of Inheritance.

Namely,

Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance (also known as Virtual Inheritance)

1. Single Inheritance in C++


This is the simplest type of inheritance. In the single inheritance, one derived
class can inherit from only one base class.
For example, the class Derived is inheriting from only one Class Base.

class A
{
... .. ...
};
class B: public A
{
... .. ...
};
Here, class B is derived from the base class A.

2. Multiple Inheritance in C++


Multiple Inheritance is a feature of C++ where a single derived class can inherit
property from more than one base class. For example, as explained below, class
Derived inherits property from both Class Base1 and Class Base2.

class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public A, public B
{
... .. ...
};

3. Hierarchical Inheritance in C++


In hierarchical inheritance, more than one sub class is inherited from a single
base class.
In hierarchical inheritance, more than one sub class is inherited from a single
base class. In this, all features that are common in child classes are included in
the base class.

For example: Football, Cricket, Badminton are derived from Sports class.

Syntax of Hierarchical Inheritance


class base_class {
... .. ...
}

class first_derived_class: public base_class {


... .. ...
}
class second_derived_class: public base_class {
... .. ...
}

4. Multilevel Inheritance in C++


In multilevel inheritance, the derived class inherits property from another derived
class.
For example, class B inherits from class A and class C inherits property from class
B.

class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public B
{
... ... ...
};

5. Hybrid (Virtual) Inheritance in C++


Hybrid inheritance is a combination of more than one type of inheritance (Combining
Hierarchical inheritance and Multiple Inheritance.).
For example:- A child and parent class relationship that follows multiple and
hierarchical inheritance both can be called hybrid inheritance.

You might also like