SlideShare a Scribd company logo
1 of 22
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 csKALAISELVI P
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritancebunnykhan
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh 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 EncapsulationAbid Kohistani
 
Bca winter 2013 2nd sem
Bca winter 2013 2nd semBca winter 2013 2nd sem
Bca winter 2013 2nd semsmumbahelp
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxDeepasCSE
 
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 conceptsMaryo Manjaruni
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
Object Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. PolymorphismObject Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. PolymorphismAndiNurkholis1
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
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 3C# 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
 
Chapter 7 C++ As OOP
Chapter 7 C++ As OOPChapter 7 C++ As OOP
Chapter 7 C++ As OOPAmrit 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

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 

Recently uploaded (20)

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

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’. 