SlideShare a Scribd company logo
NAME - RITIK
ROLL NO. - UE198077
SUBJECT - PROGRAMMING FUNDAMENTAL
SUBMITTED TO - SUKHVIR SIR
TOPIC - INHERITANCE: EXTENDING CLASSES
B.E.(IT),1st year
Inheritance:
 The mechanism of deriving a new class from an old one is called Inheritance.
 The old class is referred to as the base class.
 The new one is called the derived class or subclass.
General form of defining a derived class
Syntax:
Class derived-class-name : visibility mode base-class-name
{
……..//
…….// members of derived class
…….//
};
The colon indicate that the derived-class-name is derived from the base-class-name. The visibility mode is
optional and, if present, may be either private or public. The default visibility mode is private.
Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
Types of Inheritance
 Single Inheritance
 Multiple Inheritance
 Hierarchical Inheritance
 Multilevel Inheritance
 Hybrid Inheritance
Single Inheritance
 In single inheritance there exists single
base class and single derived class.
 It is the most simplest form of
inheritance.
Example of single inheritance
Output
Multiple inheritance
 Multiple inheritance occurs when a class
is inherit from more than one base class.
 Derived class can inherit features from
multiple base classes using multiple
inheritance.
Example of multiple inheritance
Output
Hierarchical inheritance
In this type of inheritance, more than one sub
class is inherited from a single base class. i.e.
more than one derived class is created from a
single base class.
Example of hierarchical inheritance
Output
Multilevel inheritance
In this type of inheritance, a derived class is
created from another derived class.
The class C serves as a base class for the
derived class B, which in turn serves as a base
class for the derived class A. The class B as
intermediate base class since it provides a link
for the inheritance between A and C the chain
ABC is known as inheritance path.
Class C {………}; //base class
Class B: public C {………}; //B derived from C
Class A: public B {……}; //A derived form B
Example of Multilevel inheritance
Output
Hybrid inheritance
Hybrid inheritance is implemented by
combining more than one type of
inheritance. For example: combining
hierarchical inheritance and multiple
inheritance.
Example of Hybrid inheritance
Output
Virtual base class
Consider a situation where three kind of inheritance, namely, multilevel, multiple and
hierarchical inheritance, are involved. This is illustrated in the figure.
The ‘child’ has two direct base classes ‘parent1’ and
‘Parent2’ which themselves have a common base class
‘grand parent’. The ‘child’ inherits the traits of ‘grandparent’
via two separate paths. It can also inherits the base class
directly. The ‘grand parent’ is sometimes referred to as the indirect base class.
This means ‘child’ is inherit the same property twice. To remove this duplication we
declare the base class ‘grandparent’ as the virtual while declaring the direct or
intermediate base class as shown:
Syntax
class A //grandparent
{ …….};
Class B1:virtual public A //parent1
{……..};
Class B2:public virtual A //parent2
{…….};
Class C:public B1,public B2 //child
{…….}; //only one copy of A will be inherited
Abstract classes
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.
The concept can be explained through a simple example of an abstract base class
vehicle which may be declared in a program for deriving LMV (light motor vehicle),HMV
(heavy motor vehicle) and TW(two wheeler) derived classes. A pure virtual function
spec() may be set the specification of LMV as per its four wheel and smaller engine
and that of HMV as per its eight or more wheels and relatively larger engine.
Example
Class vehicle //abstract base class
{ private:
Datatype d1,d2;
public:
virtual void spec()=0; //pure virtual funtion
};
Class LMV : public vehicle
{ public:
void spec(){ ……. } //LMV definition of spec function
};
Example
Class HMV : public vehicle
{ public:
void spec(){ …….. } HMV definition of spec function
};
Class TW : public vehicle
{ public:
void spec(){ …….. } //TW definition of spec function
};
Advantages of inheritance
 Inheritance promote reusability. When a class inherits or derives another class, it
can access all functionality of inherited class.
 Reusability enhanced reliability. the base class code is already tested and
debugged.
 Inheritance helps to reduce code redundancy and supports code extensibility.
Disadvantages of inheritance
 Inherited functions work slower than the normal function as there is indirection.
 Improper use of inheritance lead to wrong solution.
 Inheritance increases the coupling between base class and derived class. A change
in base class will affects all the child classes.
Ritik (inheritance.cpp)

More Related Content

What's hot

Inheritance
InheritanceInheritance
Inheritance
Padma Kannan
 
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
deepakskb2013
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
Sujan Mia
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
Mirza Hussain
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
Zubair CH
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
abhilashagupta
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
OOP
OOPOOP
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Inheritance access specifier in oop
Inheritance access specifier in oopInheritance access specifier in oop
Inheritance access specifier in oop
Uttam Patole
 
Inheritance
InheritanceInheritance
Inheritance
Padma Kannan
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Inheritance
InheritanceInheritance
Inheritance
Jancirani Selvam
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Inheritance
Inheritance Inheritance
Inheritance
sourav verma
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 

What's hot (20)

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
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
OOP
OOPOOP
OOP
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance access specifier in oop
Inheritance access specifier in oopInheritance access specifier in oop
Inheritance access specifier in oop
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance
Inheritance Inheritance
Inheritance
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 

Similar to Ritik (inheritance.cpp)

Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
Inheritance
InheritanceInheritance
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
JP2B1197685ARamSaiPM
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
classall
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
Inheritance
InheritanceInheritance
Inheritance
prabhat kumar
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
Redwan Islam
 
Inheritance
InheritanceInheritance
Inheritance
SangeethaSasi1
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
MASQ Technologies
 
iheritence.pptx
iheritence.pptxiheritence.pptx
iheritence.pptx
RijuMandal11
 
Inheritance
InheritanceInheritance
Inheritance
Burhan Ahmed
 
inheritence
inheritenceinheritence
inheritence
Adil Memon
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance
InheritanceInheritance
Inheritance
rajshreemuthiah
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
LalfakawmaKh
 

Similar to Ritik (inheritance.cpp) (20)

Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
iheritence.pptx
iheritence.pptxiheritence.pptx
iheritence.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritence
inheritenceinheritence
inheritence
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
itech2017
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
An Approach to Detecting Writing Styles Based on Clustering Techniques
An Approach to Detecting Writing Styles Based on Clustering TechniquesAn Approach to Detecting Writing Styles Based on Clustering Techniques
An Approach to Detecting Writing Styles Based on Clustering Techniques
ambekarshweta25
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 

Recently uploaded (20)

Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
An Approach to Detecting Writing Styles Based on Clustering Techniques
An Approach to Detecting Writing Styles Based on Clustering TechniquesAn Approach to Detecting Writing Styles Based on Clustering Techniques
An Approach to Detecting Writing Styles Based on Clustering Techniques
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 

Ritik (inheritance.cpp)

  • 1. NAME - RITIK ROLL NO. - UE198077 SUBJECT - PROGRAMMING FUNDAMENTAL SUBMITTED TO - SUKHVIR SIR TOPIC - INHERITANCE: EXTENDING CLASSES B.E.(IT),1st year
  • 2. Inheritance:  The mechanism of deriving a new class from an old one is called Inheritance.  The old class is referred to as the base class.  The new one is called the derived class or subclass.
  • 3. General form of defining a derived class Syntax: Class derived-class-name : visibility mode base-class-name { ……..// …….// members of derived class …….// }; The colon indicate that the derived-class-name is derived from the base-class-name. The visibility mode is optional and, if present, may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
  • 4. Types of Inheritance  Single Inheritance  Multiple Inheritance  Hierarchical Inheritance  Multilevel Inheritance  Hybrid Inheritance
  • 5. Single Inheritance  In single inheritance there exists single base class and single derived class.  It is the most simplest form of inheritance.
  • 6. Example of single inheritance
  • 8. Multiple inheritance  Multiple inheritance occurs when a class is inherit from more than one base class.  Derived class can inherit features from multiple base classes using multiple inheritance.
  • 9. Example of multiple inheritance
  • 11. Hierarchical inheritance In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.
  • 12. Example of hierarchical inheritance
  • 14. Multilevel inheritance In this type of inheritance, a derived class is created from another derived class. The class C serves as a base class for the derived class B, which in turn serves as a base class for the derived class A. The class B as intermediate base class since it provides a link for the inheritance between A and C the chain ABC is known as inheritance path. Class C {………}; //base class Class B: public C {………}; //B derived from C Class A: public B {……}; //A derived form B
  • 15. Example of Multilevel inheritance
  • 17. Hybrid inheritance Hybrid inheritance is implemented by combining more than one type of inheritance. For example: combining hierarchical inheritance and multiple inheritance.
  • 18. Example of Hybrid inheritance
  • 20. Virtual base class Consider a situation where three kind of inheritance, namely, multilevel, multiple and hierarchical inheritance, are involved. This is illustrated in the figure. The ‘child’ has two direct base classes ‘parent1’ and ‘Parent2’ which themselves have a common base class ‘grand parent’. The ‘child’ inherits the traits of ‘grandparent’ via two separate paths. It can also inherits the base class directly. The ‘grand parent’ is sometimes referred to as the indirect base class. This means ‘child’ is inherit the same property twice. To remove this duplication we declare the base class ‘grandparent’ as the virtual while declaring the direct or intermediate base class as shown:
  • 21. Syntax class A //grandparent { …….}; Class B1:virtual public A //parent1 {……..}; Class B2:public virtual A //parent2 {…….}; Class C:public B1,public B2 //child {…….}; //only one copy of A will be inherited
  • 22. Abstract classes 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. The concept can be explained through a simple example of an abstract base class vehicle which may be declared in a program for deriving LMV (light motor vehicle),HMV (heavy motor vehicle) and TW(two wheeler) derived classes. A pure virtual function spec() may be set the specification of LMV as per its four wheel and smaller engine and that of HMV as per its eight or more wheels and relatively larger engine.
  • 23. Example Class vehicle //abstract base class { private: Datatype d1,d2; public: virtual void spec()=0; //pure virtual funtion }; Class LMV : public vehicle { public: void spec(){ ……. } //LMV definition of spec function };
  • 24. Example Class HMV : public vehicle { public: void spec(){ …….. } HMV definition of spec function }; Class TW : public vehicle { public: void spec(){ …….. } //TW definition of spec function };
  • 25. Advantages of inheritance  Inheritance promote reusability. When a class inherits or derives another class, it can access all functionality of inherited class.  Reusability enhanced reliability. the base class code is already tested and debugged.  Inheritance helps to reduce code redundancy and supports code extensibility.
  • 26. Disadvantages of inheritance  Inherited functions work slower than the normal function as there is indirection.  Improper use of inheritance lead to wrong solution.  Inheritance increases the coupling between base class and derived class. A change in base class will affects all the child classes.

Editor's Notes

  1. NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image.