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

Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++vivekkumar2938
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 

What's hot (20)

Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
structure and union
structure and unionstructure and union
structure and union
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Inline function
Inline functionInline function
Inline function
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 

Similar to Inheritance in C++ Explained with Examples

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 in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
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
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
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
 

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

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

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