SlideShare a Scribd company logo
1 of 21
Download to read offline
INHERITANCE
Prof. K. Adisesha
Learning Outcomes
 Introduction
 Defining Inheritance
 Need of Inheritance
 Visibility mode
 Types of Inheritance
 Virtual Base Classes
2
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
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
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
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
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
Inheritance
Visibility mode:
 The private data of base class cannot be inherited.
 Visibility mode can be:
 Private
 Protected
 Public
8
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;
};
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;
};
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;
};
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
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
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
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
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
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
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
{ ……. };
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
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( );
}
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

More Related Content

What's hot

C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientationHoang Nguyen
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++Sujan Mia
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 

What's hot (20)

C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Association agggregation and composition
Association agggregation and compositionAssociation agggregation and composition
Association agggregation and composition
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 

Similar to Inheritance in C++ Explained with Examples

chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optdeepakskb2013
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfitxminahil29
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfAnant240318
 
Programming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxProgramming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxDilanAlmsa
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaMananPasricha
 

Similar to Inheritance in C++ Explained with Examples (20)

Inheritance
InheritanceInheritance
Inheritance
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Inheritance
InheritanceInheritance
Inheritance
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdf
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritence
inheritenceinheritence
inheritence
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Programming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxProgramming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptx
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 

More from Prof. Dr. K. Adisesha

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfProf. Dr. K. Adisesha
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaProf. Dr. K. Adisesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfProf. Dr. K. Adisesha
 

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 

Recently uploaded (20)

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 

Inheritance in C++ Explained with Examples

  • 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