SlideShare a Scribd company logo
Protected Inheritance − When deriving from a
protected base class, public and protected members
of the base class become protected members of the
derived class.
Private Inheritance − When deriving from a private
base class, public and protected members of the base
class become private members of the derived class.
 Inheritance is one of the key features of Object-
oriented programming in C++. It allows user to
create a new class (derived class) from an existing
class(base class).
 The derived class inherits all the features from the
base class and can have additional features of its
own.
 Suppose, in your game, you want three characters - a maths
teacher, a footballer and a businessman.
 Since, all of the characters are persons, they can walk and
talk. However, they also have some special skills. A math
teacher can teach maths, a footballer can play football and a
businessman can run a business.
 You can individually create three classes who can walk, talk
and perform their special skill as shown in the figure below.
 If you want to add a new feature - eat, you
need to implement the If you want to add a
new feature - eat, you need to implement
the same code for each character. This can
easily become error prone (when copying)
and duplicate codes.
 It'd be a lot easier if we had a Person class
with basic features like talk, walk, eat,
sleep, and add special skills to those features
as per our characters. This is done using
inheritance.
Using inheritance, now you don't implement the same
code for walk and talk for each class. You just need
to inherit them.
So, for Maths teacher (derived class), you inherit all
features of a Person (base class) and add a new
feature TeachMaths. Likewise, for a footballer, you
inherit all the features of a Person and add a new
feature PlayFootball and so on.
This makes your code cleaner, understandable and
extendable.
It is important to remember: When working with
inheritance, each derived class should satisfy the
condition whether it "is a" base class or not. In the
example above, Maths teacher is a Person, Footballer is
a Person. You cannot have: Businessman is a Business.
 class Person
 {
 ... .. ...
 };
 class MathsTeacher : public Person
 {
 ... .. ...
 };
 class Footballer : public Person
 {
 .... .. ...
 };
 In the previous example, class Person is a base class and
classes MathsTeacher and Footballer are the derived
from Person.
 The derived class appears with the declaration of a
class followed by a colon, the
keyword public and the name of base class from which
it is derived.
 Since, MathsTeacher and Footballer are derived
from Person, all data member and member function
of Person can be accessible from them.
 Example 1: Write a coding
in C++ to Create game characters using the
concept of inheritance.
 In this program, Person is a base class, while MathsTeacher and Footballer are derived
from Person.
 Person class has two data members - profession and age. It also has two member
functions - walk() and talk().
 Both MathsTeacher and Footballer can access all data members and member functions
of Person.
 However, MathsTeacher and Footballer have their own member functions as
well: teachMaths() and playFootball() respectively. These functions are only accessed
by their own class.
 In the main() function, a new MathsTeacher object teacher is created.
 Since, it has access to Person's data members, profession and age of teacher is set. This
data is displayed using the display()function defined in the Person class. Also,
the teachMaths()function is called, defined in the MathsTeacher class.
 Likewise, a new Footballer object footballer is also created. It has access to Person's
data members as well, which is displayed by invoking the display() function.
The playFootball() function only accessible by the footballer is called then after.
 When creating a derived class from a base
class, you can use different access specifiers
to inherit the data members of the base
class.
 These can be public, protected or private.
 In the above example, the base
class Person has been inherited public-ly
by MathsTeacher and Footballer.
 Where access-specifier is one of public,
protected, or private, and base-class is the name of a
previously defined class. If the access-specifier is not
used, then it is private by default.
 Consider a group of vehicles. You need to
create classes for Bus, Car and Truck. The
methods fuelAmount(), capacity(),
applyBrakes() will be same for all of the
three classes. If we create these classes
avoiding inheritance then we have to write
all of these functions in each of the three
classes as shown in below figure:
 You can clearly see that above process results in
duplication of same code 3 times. This increases
the chances of error and data redundancy. To
avoid this type of situation, inheritance is used.
If we create a class Vehicle and write these
three functions in it and inherit the rest of the
classes from the vehicle class, then we can
simply avoid the duplication of data and increase
re-usability. Look at the below diagram in which
the three classes are inherited from vehicle
class:
 Person er poriborte Vehicle: Car, bus, truck
 General DaTa MEMBER: fuel (),capacity(),brakes()
 General characteristics:
 I have a specific fuel amount (Instead of I can walk)
 I have a specific capacity (Instead of I can walk)
 I apply brakes (Instead of I can sleep)
Special characteristics (member function)
 TeachMaths er poriborte-BusCarryPublic--Bus can carry general
public
 PlayFopotbsll er poriborte---CarCarryPrivate-Car can carry private
people
 TruckCarryGoods-Truck can carry important goods
 Using inheritance, we have to write the functions only
one time instead of three times as we have inherited
rest of the three classes from base class(Vehicle).
 Implementing inheritance in C++:
 For creating a sub-class it is inherited from the base
class
Note: A derived class doesn’t inherit access to private
data members. However, it does inherit a full parent
object, which contains any private members which that
class declares.
 Output:
 Child id is 7 Parent id is 91
 In the above program the ‘Child’ class is
publicly inherited from the ‘Parent’ class so
the public data members of the class
‘Parent’ will also be inherited by the class
‘Child’.


More Related Content

Similar to Lecture 5.mte 407

Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
bunnykhan
 
L9
L9L9
L9
lksoo
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
Ganesh Karthik
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
Bca winter 2013 2nd sem
Bca winter 2013 2nd semBca winter 2013 2nd sem
Bca winter 2013 2nd sem
smumbahelp
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Swarup Boro
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
DeepasCSE
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
Jussi Pohjolainen
 
Object Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. PolymorphismObject Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. Polymorphism
AndiNurkholis1
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
zahid khan
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Saleh
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
MuhammadHuzaifa981023
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Boro
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
Syeful Islam
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
Amrit Kaur
 

Similar to Lecture 5.mte 407 (20)

Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
L9
L9L9
L9
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Access Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and EncapsulationAccess Modifiers in C# ,Inheritance and Encapsulation
Access Modifiers in C# ,Inheritance and Encapsulation
 
Bca winter 2013 2nd sem
Bca winter 2013 2nd semBca winter 2013 2nd sem
Bca winter 2013 2nd sem
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Object Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. PolymorphismObject Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. Polymorphism
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOP
 

Recently uploaded

The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
shahdabdulbaset
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 

Recently uploaded (20)

The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Hematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood CountHematology Analyzer Machine - Complete Blood Count
Hematology Analyzer Machine - Complete Blood Count
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 

Lecture 5.mte 407

  • 1.
  • 2. Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • 3.  Inheritance is one of the key features of Object- oriented programming in C++. It allows user to create a new class (derived class) from an existing class(base class).  The derived class inherits all the features from the base class and can have additional features of its own.
  • 4.  Suppose, in your game, you want three characters - a maths teacher, a footballer and a businessman.  Since, all of the characters are persons, they can walk and talk. However, they also have some special skills. A math teacher can teach maths, a footballer can play football and a businessman can run a business.  You can individually create three classes who can walk, talk and perform their special skill as shown in the figure below.
  • 5.
  • 6.  If you want to add a new feature - eat, you need to implement the If you want to add a new feature - eat, you need to implement the same code for each character. This can easily become error prone (when copying) and duplicate codes.  It'd be a lot easier if we had a Person class with basic features like talk, walk, eat, sleep, and add special skills to those features as per our characters. This is done using inheritance.
  • 7.
  • 8. Using inheritance, now you don't implement the same code for walk and talk for each class. You just need to inherit them. So, for Maths teacher (derived class), you inherit all features of a Person (base class) and add a new feature TeachMaths. Likewise, for a footballer, you inherit all the features of a Person and add a new feature PlayFootball and so on. This makes your code cleaner, understandable and extendable. It is important to remember: When working with inheritance, each derived class should satisfy the condition whether it "is a" base class or not. In the example above, Maths teacher is a Person, Footballer is a Person. You cannot have: Businessman is a Business.
  • 9.  class Person  {  ... .. ...  };  class MathsTeacher : public Person  {  ... .. ...  };  class Footballer : public Person  {  .... .. ...  };
  • 10.  In the previous example, class Person is a base class and classes MathsTeacher and Footballer are the derived from Person.  The derived class appears with the declaration of a class followed by a colon, the keyword public and the name of base class from which it is derived.  Since, MathsTeacher and Footballer are derived from Person, all data member and member function of Person can be accessible from them.
  • 11.  Example 1: Write a coding in C++ to Create game characters using the concept of inheritance.
  • 12.  In this program, Person is a base class, while MathsTeacher and Footballer are derived from Person.  Person class has two data members - profession and age. It also has two member functions - walk() and talk().  Both MathsTeacher and Footballer can access all data members and member functions of Person.  However, MathsTeacher and Footballer have their own member functions as well: teachMaths() and playFootball() respectively. These functions are only accessed by their own class.  In the main() function, a new MathsTeacher object teacher is created.  Since, it has access to Person's data members, profession and age of teacher is set. This data is displayed using the display()function defined in the Person class. Also, the teachMaths()function is called, defined in the MathsTeacher class.  Likewise, a new Footballer object footballer is also created. It has access to Person's data members as well, which is displayed by invoking the display() function. The playFootball() function only accessible by the footballer is called then after.
  • 13.  When creating a derived class from a base class, you can use different access specifiers to inherit the data members of the base class.  These can be public, protected or private.  In the above example, the base class Person has been inherited public-ly by MathsTeacher and Footballer.
  • 14.  Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
  • 15.  Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown in below figure:
  • 16.
  • 17.  You can clearly see that above process results in duplication of same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. Look at the below diagram in which the three classes are inherited from vehicle class:
  • 18.
  • 19.  Person er poriborte Vehicle: Car, bus, truck  General DaTa MEMBER: fuel (),capacity(),brakes()  General characteristics:  I have a specific fuel amount (Instead of I can walk)  I have a specific capacity (Instead of I can walk)  I apply brakes (Instead of I can sleep) Special characteristics (member function)  TeachMaths er poriborte-BusCarryPublic--Bus can carry general public  PlayFopotbsll er poriborte---CarCarryPrivate-Car can carry private people  TruckCarryGoods-Truck can carry important goods
  • 20.  Using inheritance, we have to write the functions only one time instead of three times as we have inherited rest of the three classes from base class(Vehicle).  Implementing inheritance in C++:  For creating a sub-class it is inherited from the base class Note: A derived class doesn’t inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.
  • 21.
  • 22.  Output:  Child id is 7 Parent id is 91  In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the public data members of the class ‘Parent’ will also be inherited by the class ‘Child’. 