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 (20)

[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
polymorphism
polymorphism polymorphism
polymorphism
 
class and objects
class and objectsclass and objects
class and objects
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Access specifier
Access specifierAccess specifier
Access specifier
 
C++ classes
C++ classesC++ classes
C++ classes
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Inheritance
InheritanceInheritance
Inheritance
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor 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
 
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
 

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

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 

Recently uploaded (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 

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