SlideShare une entreprise Scribd logo
1  sur  21
Inheritance
Ronak Chhajed
What is Inheritance ?
Inheritance is a relationship between two or more
classes where derived class inherits properties of pre-
existing (base) classes.
Base Class: It is the class whose properties are inherited
by another class. It is also called Super Class or Parent
Class.
Derived Class: It is the class that inherit properties from
base class(es). It is also called Sub Class or Child Class.
Concept of
Inheritance
Base Class
Property A
Property B
Property C
Property D
Property C
Property B
Property A
Derived
Class
Defined in
Derived Class
Defined in
Base Class
But accessible
from Derived
Class
Defining Derived Classes
A derived class can be defined by specifying its relationship
with the base class in addition to its own details.
The general form of defining a derived class is :
The colon indicates that the derived_class_name is derived
from the base_class_name.
The visibility mode (access specifier) is optional, if present,
may be private, public, or protected.
Class derived_class_name : visibility_mode base_class_name
{
………..// members of derived class
};
Visibility Mode
The visibility mode specifies how the features of the base
class are visible to the derived class.
Private : When a derived class privately inherits a base
class, the protected and public members of base class
become private members of the derived class.
Public : In Public mode, the protected and public members of
base class become protected and public members of derived
class respectively.
Protected : In Protected mode, the protected and public
members of base class become protected members of the
derived class.
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance
In Single Inheritance, one derived class inherits from one
base class
Class A
Class B
Single Inheritance
class A
{
…….. // Members of base class
};
class B : public A
{
…….. // Members of derived class
};
Single Inheritance is declared as follows :
Multilevel Inheritance
In Multiple Inheritance, a class (Inter-mediate base class)
is derived from base class, which is again derived into
some other class
The class A serves as a
base class for the derived
class B, which in turn
serves as a base class
for the derived class C.
Class A
Class B
Class C
Multilevel Inheritance
class A
{
……… //Members of base class
{;
class B : public A
{
……… //Members of derived class, B derived from A
};
class C : public B
………. //Members of derived class, C derived from B
};
Multilevel Inheritance is declared as follows :
Multiple Inheritance
In Multiple Inheritance, a class is derived from more than
one base classes
Class C
Class BClass A
Base
Classes
Derived Class
Multiple Inheritance
class A
{
……… //Members of base class A
{;
class B
{
……… //Members of base class B
};
class C : public A, public B
{
………. //Members of derived class
};
Multiple Inheritance is defined as follows :
Hierarchical Inheritance
In Hierarchical Inheritance, one base class is inherited into
two or more Derived Classes
Class A
Class B Class C
Base Class
Derived Class
Hierarchical Inheritance
class A
{
……… //Members of base class A
{;
class B : public A
{
……… //Members of Derived class , B derived from A
};
class C : public A
{
………. //Members of derived class , C derived from A
};
Hierarchical Inheritance is declared as follows
Hybrid Inheritance
In Hybrid Inheritance, more than one type of inheritances
are used to derive a new Sub Class.
For example, inheriting a class from two different classes,
which in turn have been derived from the same base class.
Any legal combination of other four types of inheritance
comes under Hybrid Inheritance
Hybrid Inheritance
Class A
Class B Class C
Base Class
Class D Derived Class
Intermediate
Base Class
Hybrid Inheritance
class A
{
………..
………..
};
class B : public A
{
……….
};
class C : public A
{
………..
};
class D : public B, public C
{
………
};
A basic form of Hybrid Inheritance :
Virtual Base Class
Virtual base class is a way of preventing multiple
instances(duplicate copies) of a given class appearing in
an inheritance hierarchy when using multiple/hybrid
inheritance.
Suppose we have a base class called ‘A’ and two classes
derived from it ‘B’ and ‘C’ and we derived class ‘D’ from
class ‘B’ and ‘C’.
That means all the members of class ‘A’ are inherited into
‘D’ twice, first via ‘B’ and again via ‘C’ This means, class
‘D’ would have duplicate sets of members inherited from
‘A’. This is an ambiguous situation
Virtual Base Class
class A
{
………..
………..
};
class B : virtual public A
{
……….
};
class C : virtual public A
{
………..
};
class D : public B, public C
{
………
};
This is how we create Virtual Base Class
Abstract Class
An abstract class is one that is not used to create
objects.
An abstract class is designed only to act as a base class
to be inherited by other classes.
It is a design concept in program development and
provides a base upon which other classes may be built
Advantages of Inheritance
Reusability : Inheritance helps the code to be reused in
many situations. The base class is defines and once it is
compiled, it need not be reworked. Using the concept of
Inheritance, the programmer can create as many derived
classes from the base class as needed while adding
specific features to each derived class as needed
Saves Time and Effort : The above concept of reusability
achieved by inheritance saves the programmer’s time
and effort. Since the main code written can be reused in
various situations as needed

Contenu connexe

Tendances

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Gajendra Singh Thakur
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 

Tendances (20)

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Java package
Java packageJava package
Java package
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Abstract class
Abstract classAbstract class
Abstract class
 

Similaire à Inheritance in OOPS

E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.pptclassall
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)RitikAhlawat1
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
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
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerssuser6f3c8a
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).pptamal68766
 
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
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaMananPasricha
 

Similaire à Inheritance in OOPS (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritence
inheritenceinheritence
inheritence
 
iheritence.pptx
iheritence.pptxiheritence.pptx
iheritence.pptx
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Single inheritance
Single inheritanceSingle inheritance
Single 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
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
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 c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 

Dernier

Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 

Dernier (20)

Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 

Inheritance in OOPS

  • 2. What is Inheritance ? Inheritance is a relationship between two or more classes where derived class inherits properties of pre- existing (base) classes. Base Class: It is the class whose properties are inherited by another class. It is also called Super Class or Parent Class. Derived Class: It is the class that inherit properties from base class(es). It is also called Sub Class or Child Class.
  • 3. Concept of Inheritance Base Class Property A Property B Property C Property D Property C Property B Property A Derived Class Defined in Derived Class Defined in Base Class But accessible from Derived Class
  • 4. Defining Derived Classes A derived class can be defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is : The colon indicates that the derived_class_name is derived from the base_class_name. The visibility mode (access specifier) is optional, if present, may be private, public, or protected. Class derived_class_name : visibility_mode base_class_name { ………..// members of derived class };
  • 5. Visibility Mode The visibility mode specifies how the features of the base class are visible to the derived class. Private : When a derived class privately inherits a base class, the protected and public members of base class become private members of the derived class. Public : In Public mode, the protected and public members of base class become protected and public members of derived class respectively. Protected : In Protected mode, the protected and public members of base class become protected members of the derived class.
  • 6. Types of Inheritance 1. Single Inheritance 2. Multilevel Inheritance 3. Multiple Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
  • 7. Single Inheritance In Single Inheritance, one derived class inherits from one base class Class A Class B
  • 8. Single Inheritance class A { …….. // Members of base class }; class B : public A { …….. // Members of derived class }; Single Inheritance is declared as follows :
  • 9. Multilevel Inheritance In Multiple Inheritance, a class (Inter-mediate base class) is derived from base class, which is again derived into some other class The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. Class A Class B Class C
  • 10. Multilevel Inheritance class A { ……… //Members of base class {; class B : public A { ……… //Members of derived class, B derived from A }; class C : public B ………. //Members of derived class, C derived from B }; Multilevel Inheritance is declared as follows :
  • 11. Multiple Inheritance In Multiple Inheritance, a class is derived from more than one base classes Class C Class BClass A Base Classes Derived Class
  • 12. Multiple Inheritance class A { ……… //Members of base class A {; class B { ……… //Members of base class B }; class C : public A, public B { ………. //Members of derived class }; Multiple Inheritance is defined as follows :
  • 13. Hierarchical Inheritance In Hierarchical Inheritance, one base class is inherited into two or more Derived Classes Class A Class B Class C Base Class Derived Class
  • 14. Hierarchical Inheritance class A { ……… //Members of base class A {; class B : public A { ……… //Members of Derived class , B derived from A }; class C : public A { ………. //Members of derived class , C derived from A }; Hierarchical Inheritance is declared as follows
  • 15. Hybrid Inheritance In Hybrid Inheritance, more than one type of inheritances are used to derive a new Sub Class. For example, inheriting a class from two different classes, which in turn have been derived from the same base class. Any legal combination of other four types of inheritance comes under Hybrid Inheritance
  • 16. Hybrid Inheritance Class A Class B Class C Base Class Class D Derived Class Intermediate Base Class
  • 17. Hybrid Inheritance class A { ……….. ……….. }; class B : public A { ………. }; class C : public A { ……….. }; class D : public B, public C { ……… }; A basic form of Hybrid Inheritance :
  • 18. Virtual Base Class Virtual base class is a way of preventing multiple instances(duplicate copies) of a given class appearing in an inheritance hierarchy when using multiple/hybrid inheritance. Suppose we have a base class called ‘A’ and two classes derived from it ‘B’ and ‘C’ and we derived class ‘D’ from class ‘B’ and ‘C’. That means all the members of class ‘A’ are inherited into ‘D’ twice, first via ‘B’ and again via ‘C’ This means, class ‘D’ would have duplicate sets of members inherited from ‘A’. This is an ambiguous situation
  • 19. Virtual Base Class class A { ……….. ……….. }; class B : virtual public A { ………. }; class C : virtual public A { ……….. }; class D : public B, public C { ……… }; This is how we create Virtual Base Class
  • 20. Abstract Class An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class to be inherited by other classes. It is a design concept in program development and provides a base upon which other classes may be built
  • 21. Advantages of Inheritance Reusability : Inheritance helps the code to be reused in many situations. The base class is defines and once it is compiled, it need not be reworked. Using the concept of Inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed Saves Time and Effort : The above concept of reusability achieved by inheritance saves the programmer’s time and effort. Since the main code written can be reused in various situations as needed

Notes de l'éditeur

  1. This is just opposite of Multiple Inheritance
  2. This is a combine form of Multiple and Hierarchical Inheritance.