SlideShare a Scribd company logo
INHERITANCE IN C++
Introduction to Inheritance
โ€ขDefinition: Inheritance allows a new class (derived class) to
acquire the properties and behaviors (methods) of an existing
class (base class).
โ€ขWhy Inheritance?
โ€ข Code Reusability
โ€ข Extensibility
โ€ข Maintainability
Key Terminology
โ€ขBase Class (Parent Class): The class whose
properties are inherited.
โ€ขDerived Class (Child Class): The class that inherits
from the base class.
Syntax:
Types of Inheritance in C++
1.Single Inheritance:
1. Derived class inherits from a single base class.
2.Multiple Inheritance:
1. Derived class inherits from more than one base class.
3.Multilevel Inheritance:
1. A class derived from another derived class.
4.Hierarchical Inheritance:
1. One base class, multiple derived classes.
5.Hybrid Inheritance:
1. Combination of more than one type of inheritance.
TYPES OF INHERITANCE CONCEPT IN C++.pptx
A Special Case of Hybrid Inheritance: Multipath Inheritance
In multipath inheritance, a class is derived from two base classes and these two base
classes in turn are derived from one common base class. An ambiguity can arise in this
type of inheritance in the most derived class. This problem is also called diamond problem
due to the diamond shape formed in the UML inheritance diagram.
Single inheritance example
Access Specifiers in Inheritance
โ€ขPublic Inheritance: Public members of the base class
become public in the derived class.
โ€ขProtected Inheritance: Public and protected
members of the base class become protected in the
derived class.
โ€ขPrivate Inheritance: Public and protected members
of the base class become private in the derived class.
TYPES OF INHERITANCE CONCEPT IN C++.pptx
Multilevel Inheritance Example
Advantages of Inheritance
โ€ขCode Reusability: Reduce redundancy.
โ€ขEasy Maintenance: Changes in the base class reflect in
derived classes.
โ€ขExtensibility: New features can be added to derived
classes without modifying base class.
Modes of Inheritance in C++
Public Inheritance Mode
If we derive a subclass from a public base class. Then the public member of the base class
will become public in the derived class and protected members of the base class will
become protected in the derived class.
Protected Inheritance Mode
If we derive a subclass from a Protected base class. Then both public members and
protected members of the base class will become protected in the derived class.
Private Inheritance Mode
If we derive a subclass from a Private base class. Then both public members
and protected members of the base class will become private in the derived
class. They can only be accessed by the member functions of the derived class.
Private mode is the default mode that is applied when we donโ€™t specify any
mode.
Note: The private members in the base class cannot be directly accessed in the
derived class, while protected and public members can be directly accessed. To
access or update the private members of the base class in derived class, we have to
use the corresponding getter and setter functions of the base class or declare the
derived class as friend class.
COURTESY: GEEKS FOR GEEKS
TYPES OF INHERITANCE CONCEPT IN C++.pptx
Program to Access the Private Members of the Base Class in Derived Class
TYPES OF INHERITANCE CONCEPT IN C++.pptx
CAN YOU GUYS GUESS THE OUTPUT??
The above program shows the method in which the private members of the
base class remain encapsulated and are only accessible through controlled
public or protected member functions.
We can also access the private members of the base class by declaring the
derived class as friend class in the base class
EXAMPLE: SEE NEXT EXAMPLE.
TYPES OF INHERITANCE CONCEPT IN C++.pptx
TYPES OF INHERITANCE CONCEPT IN C++.pptx
TYPES OF INHERITANCE CONCEPT IN C++.pptx
Constructors and Destructors in Inheritance
Constructors and Destructors are generally defined by the programmer and if not, the compiler
automatically creates them so they are present in every class in C++. Now, the question arises what
happens to the constructor and destructor when a class is inherited by another class.
In C++ inheritance, the constructors and destructors are not inherited by the derived class, but
we can call the constructor of the base class in derived class.
โ€ขThe constructors will be called by the complier in the order in which they are inherited. It means
that base class constructors will be called first, then derived class constructors will be called.
โ€ขThe destructors will be called in reverse order in which the compiler is declared.
โ€ขWe can also call the constructors and destructors manually in the derived class.
Polymorphism in Inheritance
In Inheritance, we can redefine the base class member function in the derived
class.
This type of inheritance is called Function Overriding. Generally, in other
programming languages, function overriding is runtime polymorphism but in C+
+, we can do it at both runtime and complile time. For runtime polymorphism, we
have to use the virtual functions.
CAN YOU WRITE AN EXAMPLE FOR THIS??
INHERITANCE MEME โ€“ ITโ€™S TIME TO LAUGH

More Related Content

PPTX
Object oriented programming new syllabus presentation
PDF
lecture 6.pdf
PPT
Inheritance in C++
PDF
Inheritance
PPTX
Inheritance in C++ (Programming Fundamentals)
ย 
PPTX
C++ Inheritance.pptx
PDF
Inheritance
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Object oriented programming new syllabus presentation
lecture 6.pdf
Inheritance in C++
Inheritance
Inheritance in C++ (Programming Fundamentals)
ย 
C++ Inheritance.pptx
Inheritance
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx

Similar to TYPES OF INHERITANCE CONCEPT IN C++.pptx (20)

PPT
Inheritance.ppt
PPT
6 Inheritance
PPTX
Inheritance in c++ by Manan Pasricha
PPSX
Inheritance and Polymorphism in Oops
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
PPTX
00ps inheritace using c++
PPT
Inheritance
PPTX
Introduction to inheritance and different types of inheritance
PPTX
INHERITANCE.pptx
PPT
Inheritance and its types explained.ppt
PPT
Lab3
PPT
Inheritance
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PPT
week14 (1).ppt
PPT
session 24_Inheritance.ppt
PPTX
inheritance
PPTX
Inheritance
PPTX
Inheritance in c++
PDF
inheritance-16031525566nbhij56604452.pdf
PPT
inheritence
Inheritance.ppt
6 Inheritance
Inheritance in c++ by Manan Pasricha
Inheritance and Polymorphism in Oops
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
00ps inheritace using c++
Inheritance
Introduction to inheritance and different types of inheritance
INHERITANCE.pptx
Inheritance and its types explained.ppt
Lab3
Inheritance
inheritance_OOPC_datastream.ppttttttttttttttx
week14 (1).ppt
session 24_Inheritance.ppt
inheritance
Inheritance
Inheritance in c++
inheritance-16031525566nbhij56604452.pdf
inheritence
Ad

Recently uploaded (20)

PPTX
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
PPT
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
PPT
2- CELL INJURY L1 Medical (2) gggggggggg
PPTX
STS CHAP 4 human development as reflected
PDF
iTop VPN Crack Latest Version 2025 Free Download With Keygen
PPTX
The-Scope-of-Food-Quality-and-Safety.pptx managemement
PDF
APNCET2025RESULT Result Result 2025 2025
PDF
Entrepreneurship PowerPoint for students
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
PPTX
Prokaryotes v Eukaryotes PowerPoint.pptx
PPTX
Job-opportunities lecture about it skills
PPTX
Surgical thesis protocol formation ppt.pptx
ย 
PPTX
_Dispute Resolution_July 2022.pptxmhhghhhh
PDF
CV of Architect Professor A F M Mohiuddin Akhand.pdf
PDF
LSR CASEBOOK 2024-25.pdf. very nice casbook
PPT
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PDF
Beginnerโ€™s Guide to Digital Marketing.pdf
PDF
Blue-Modern-Elegant-Presentation (1).pdf
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
2- CELL INJURY L1 Medical (2) gggggggggg
STS CHAP 4 human development as reflected
iTop VPN Crack Latest Version 2025 Free Download With Keygen
The-Scope-of-Food-Quality-and-Safety.pptx managemement
APNCET2025RESULT Result Result 2025 2025
Entrepreneurship PowerPoint for students
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
Prokaryotes v Eukaryotes PowerPoint.pptx
Job-opportunities lecture about it skills
Surgical thesis protocol formation ppt.pptx
ย 
_Dispute Resolution_July 2022.pptxmhhghhhh
CV of Architect Professor A F M Mohiuddin Akhand.pdf
LSR CASEBOOK 2024-25.pdf. very nice casbook
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
Beginnerโ€™s Guide to Digital Marketing.pdf
Blue-Modern-Elegant-Presentation (1).pdf
Ad

TYPES OF INHERITANCE CONCEPT IN C++.pptx

  • 2. Introduction to Inheritance โ€ขDefinition: Inheritance allows a new class (derived class) to acquire the properties and behaviors (methods) of an existing class (base class). โ€ขWhy Inheritance? โ€ข Code Reusability โ€ข Extensibility โ€ข Maintainability
  • 3. Key Terminology โ€ขBase Class (Parent Class): The class whose properties are inherited. โ€ขDerived Class (Child Class): The class that inherits from the base class. Syntax:
  • 4. Types of Inheritance in C++ 1.Single Inheritance: 1. Derived class inherits from a single base class. 2.Multiple Inheritance: 1. Derived class inherits from more than one base class. 3.Multilevel Inheritance: 1. A class derived from another derived class. 4.Hierarchical Inheritance: 1. One base class, multiple derived classes. 5.Hybrid Inheritance: 1. Combination of more than one type of inheritance.
  • 6. A Special Case of Hybrid Inheritance: Multipath Inheritance In multipath inheritance, a class is derived from two base classes and these two base classes in turn are derived from one common base class. An ambiguity can arise in this type of inheritance in the most derived class. This problem is also called diamond problem due to the diamond shape formed in the UML inheritance diagram.
  • 8. Access Specifiers in Inheritance โ€ขPublic Inheritance: Public members of the base class become public in the derived class. โ€ขProtected Inheritance: Public and protected members of the base class become protected in the derived class. โ€ขPrivate Inheritance: Public and protected members of the base class become private in the derived class.
  • 11. Advantages of Inheritance โ€ขCode Reusability: Reduce redundancy. โ€ขEasy Maintenance: Changes in the base class reflect in derived classes. โ€ขExtensibility: New features can be added to derived classes without modifying base class.
  • 12. Modes of Inheritance in C++ Public Inheritance Mode If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class. Protected Inheritance Mode If we derive a subclass from a Protected base class. Then both public members and protected members of the base class will become protected in the derived class.
  • 13. Private Inheritance Mode If we derive a subclass from a Private base class. Then both public members and protected members of the base class will become private in the derived class. They can only be accessed by the member functions of the derived class. Private mode is the default mode that is applied when we donโ€™t specify any mode. Note: The private members in the base class cannot be directly accessed in the derived class, while protected and public members can be directly accessed. To access or update the private members of the base class in derived class, we have to use the corresponding getter and setter functions of the base class or declare the derived class as friend class.
  • 16. Program to Access the Private Members of the Base Class in Derived Class
  • 18. CAN YOU GUYS GUESS THE OUTPUT??
  • 19. The above program shows the method in which the private members of the base class remain encapsulated and are only accessible through controlled public or protected member functions. We can also access the private members of the base class by declaring the derived class as friend class in the base class EXAMPLE: SEE NEXT EXAMPLE.
  • 23. Constructors and Destructors in Inheritance Constructors and Destructors are generally defined by the programmer and if not, the compiler automatically creates them so they are present in every class in C++. Now, the question arises what happens to the constructor and destructor when a class is inherited by another class. In C++ inheritance, the constructors and destructors are not inherited by the derived class, but we can call the constructor of the base class in derived class. โ€ขThe constructors will be called by the complier in the order in which they are inherited. It means that base class constructors will be called first, then derived class constructors will be called. โ€ขThe destructors will be called in reverse order in which the compiler is declared. โ€ขWe can also call the constructors and destructors manually in the derived class.
  • 24. Polymorphism in Inheritance In Inheritance, we can redefine the base class member function in the derived class. This type of inheritance is called Function Overriding. Generally, in other programming languages, function overriding is runtime polymorphism but in C+ +, we can do it at both runtime and complile time. For runtime polymorphism, we have to use the virtual functions. CAN YOU WRITE AN EXAMPLE FOR THIS??
  • 25. INHERITANCE MEME โ€“ ITโ€™S TIME TO LAUGH