Inheritance allows classes to establish a hierarchical relationship between base and derived classes so that the derived class can inherit attributes and behaviors from the base class. There are several types of inheritance including single, multilevel, and multiple inheritance. Visibility modes like public, private, and protected determine which members are inherited from the base class. Virtual base classes are used to resolve ambiguities that can arise from multiple inheritance hierarchies sharing a common base class.
2. Learning Outcomes
Introduction
Defining Inheritance
Need of Inheritance
Visibility mode
Types of Inheritance
Virtual Base Classes
2
3. Introduction
Inheritance
OOP makes it possible to create full reusable applications
with less code and shorter development time.
Inheritance is another important aspect of object-oriented
programming.
In C++, it is possible to inherit attributes and methods
from one class to another.
3
4. Inheritance
Inheritance:
Inheritance is the capability of one class to inherit
properties from another class.
Base Class:
It is the class whose properties are inherited by another
class.
It is also called Super class.
Derived Class:
The class inherits the properties from base class.
It is also called Sub class
4
5. Inheritance
Need of Inheritance:
If we use the code of X even in Y without rewriting it, then
class Y inherits all the properties of X.
Suppose if we use direct option without using inheritance, it
has following problems.
Code written in X is repeated again in Y which leads to unnecessary
wastage of memory.
Testing to be done separately for both class X and class Y leads to
waste of time.
The above problem can be solved by using the concept of
inheritance.
5
6. Inheritance
Advantages of Inheritance:
Reusing existing code
Faster development time
Easy to maintain
Easy to extend
Memory Utilization
Disadvantage of Inheritance:
Inappropriate use of inheritance makes programs more
complicated.
Calling member functions using objects creates more
compiler overheads.
6
7. Inheritance
Defining Derived Classes:
A derived class is a composite class – it inherits
members from the base class and adds member of its
own.
Syntax given below.
class D_class_name : Visib_Mode B_class_name
Here,
class → Keyword
D_class_name → Name of the derived class
:→ Shows the derivation from the base class.
Visib_Mode → Specifies the type of derivation
B_class_name → Name of the base class.
7
8. Inheritance
Visibility mode:
The private data of base class cannot be inherited.
Visibility mode can be:
Private
Protected
Public
8
9. Visibility mode
Public mode:
If inheritance is done in Public mode:
Public members of the base class become the public
member of derived class
Protected member of base class become the protected
member of derived class.
9
// Sub class inheriting from Base Class(Parent)
class Child : public Parent
{
public:
int id_c;
};
10. Visibility mode
Private mode:
If inheritance is done in a Private mode:
Public members of base class become the private
members of derived class.
Protected members of base class become the private
members of derived class.
10
// Sub class inheriting from Base Class(Parent)
class Child : private Parent
{
public:
int id_c;
};
11. Visibility mode
Protected mode:
If inheritance is done in a Protected mode:
Public members of base class become the Protected members
of the base class.
Protected members of base class become the Protected
members of the base class.
11
// Sub class inheriting from Base Class(Parent)
class Child : protected Parent
{
public:
int id_c;
};
12. Types of Inheritance
Types of Inheritance are:
Based on the relationship, inheritance can be classified
into five forms:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
12
13. Types of Inheritance
Single Inheritance:
Single Inheritance is the process of creating a new class from
existing class base class.
In single inheritance, a class is allowed to inherit from only
one class. i.e. one sub class is inherited by one base class only.
Syntax:
class Base_Class
{ ………..};
class Derived_class : public Base_calss
{ ……. };
13
14. Types of Inheritance
Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can
inherit from more than one classes.
One sub class is inherited from more than one base classes.
Syntax:
class B
{ ………..};
class C
{ ………..};
class A : public B, public C
{ ……. };
14
15. Types of Inheritance
Multilevel Inheritance:
In this type of inheritance, a derived class is created from
another derived class.
Derivation of a class from another derived class is called
multilevel inheritance.
Syntax:
class C
{ ………..};
class B: public C
{ ………..};
class A : public B
{ ……. };
15
16. Types of Inheritance
Hierarchical Inheritance:
If a number of classes are derived from a single base class, it is
called as hierarchical inheritance.
Hierarchical model exhibits top down approach by breaking up
a complex class into simpler class.
Syntax:
class B
{ ………..};
class C: public B
{ ………..};
class A : public B
{ ……. };
16
17. Types of Inheritance
Hybrid (Virtual) Inheritance:
Hybrid Inheritance is implemented by combining more than
one type of inheritance.
Hybrid Inheritance is combination of Hierarchical and
multilevel inheritance.
Syntax:
class A
{ ………..};
class B: virtual public A
{ ………..};
class C : virtual public A
{ ……. };
class D : public B, public C
{ ……. }; 17
18. Virtual Base Classes
Virtual Base Classes:
A derived class with two base classes and these two base
classes have one common base class is called multipath
inheritance. An ambiguity can arise in this type of inheritance.
Such a base class is known as virtual base class.
This can be achieved by preceding the base class name with
the word virtual.
18
class A
{ ………..};
class B: virtual public A
{ ………..};
class C : virtual public A
{ ……. };
class D : public B, public C
{ ……. };
19. Constructors
Constructor and Destructors in derived classes:
If we inherit a class from another class and create an object of
the derived class:
The constructor of base class is called first to initialize all the inherited
members.
Destructors in C++ are called in the opposite order of that of
Constructors
19
20. Constructors
Program to illustrate the use of destructors in C++.
20
class Base
{
public:
Base( ); //Default constructor
{
cout<<”Inside Base Constructor”;
}
~ Base( ); //Destructor
{
cout<<”Inside Base Destructor”;
}
};
class Derived : public Base
{
public:
Derived( ); //Default constructor
{
cout<<”Inside Derived Constructor”;
}
~ Derived( ); //Destructor
{
cout<<”Inside Derived Destructor”;
}
};
void main( )
{
Derived x;
a.display( );
}
21. Discussion
Questions:
What is Inheritance? Explain any two types of Inheritance.
What are visibility modes? Explain.
What is the difference between public, private and protected
access specifiers?
Explain single inheritance with a suitable C++ program.
What is Multilevel Inheritance? Explain with a suitable
program example.
What is virtual base class? Give example.
What are the advantages of Inheritance?
21