SlideShare a Scribd company logo
Object Oriented Programming
Unit III
Operators & Inheritance In C++
Prepared By
Prof. Vishal S Patil
Asst. Prof AEC/CSE Dept.
Topics
 Inheritance in C++
 Types of Inheritance
Inheritance in C++
 The mechanism of deriving a class from another class is
known as Inheritance.
 Inheritance is the most importance concept of object
oriented programming.
 It allows us to define a class in terms of another class,
which helps to create and maintain an application.
 The main advantage of Inheritance is, it provides an
opportunity to reuse the code functionality and fast
implementation time.
 The members of the class can be Public, Private or
Protected.
 Syntax:
class DerivedClass : AccessSpecifier BaseClass
 The default access specifier is Private.
 Inheritance helps user to create a new class (derived
class) from a existing class (base class).
 Derived class inherits all the features from a Base class
including additional feature of its own.
Types of Inheritance.
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
1. Single Inheritance
 In Single Inheritance, one class is derived from another
class.
 It represents a form of inheritance where there is only one
base and derived class.
// C++ program to explain Single inheritance
#include <iostream.h>
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Car: public Vehicle{
};
int main()
{
Car obj; // creating object of sub class will invoke the constructor of base classes
return 0;
}
 Multiple Inheritance: Multiple Inheritance is a feature
of C++ where a class can inherit from more than one
classes. i.e one sub class is inherited from more than
one base classes. Syntax
class subclass_name : access_mode base_class1, access_mode
base_class2, ....
{ //body of subclass
};
C++ program to explain multiple inheritance
#include <iostream.h>
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle“;
}
};
class Car: public Vehicle, public FourWheeler
{
};
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
3.Multilevel Inheritance
In this type of inheritance, a derived class is
created from another derived class.
C++ program to implement Multilevel Inheritance
#include <iostream.h>
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are
vehicles"<<endl;
}
};
class Car: public fourWheeler{
public:
car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};
int main()
{
Car obj;
return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
4 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.
C++ program to implement Hierarchical Inheritance
#include <iostream>
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" <<
endl;
}
};
class Car: public Vehicle
{
};
class Bus: public Vehicle
{
};
int main()
{
Car obj1;
Bus obj2;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle
5 Hybrid (Virtual) Inheritance:
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance. Below image shows the combination of
hierarchical and multiple inheritance:
// C++ program for Hybrid Inheritance
#include <iostream.h>
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehiclen";
}
};
class Car: public Vehicle
{
};
class Bus: public Vehicle, public Fare
{
};
int main()
{
Bus obj2;
return 0;
}
Output:
This is a Vehicle
Fare of Vehicle
Reference
https://www.geeksforgeeks.org/inheritance-in-cpp/
Ad

More Related Content

What's hot (20)

C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
NikitaKaur10
 

Similar to Inheritance in c++ (20)

Week 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptxWeek 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptx
NajamUlHassan73
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
VishnuSupa
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
JP2B1197685ARamSaiPM
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Introduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritanceIntroduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritance
huzaifaakram12
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
LalfakawmaKh
 
Inheritance
InheritanceInheritance
Inheritance
zindadili
 
Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03
Sivakumar M
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
Tansh5
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
Praveen M Jigajinni
 
Inheritance
InheritanceInheritance
Inheritance
Prof. Dr. K. Adisesha
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
Redwan Islam
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
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
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
Sujan Mia
 
OOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHatOOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHat
Scholarhat
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
Ganesh Amirineni
 
Week 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptxWeek 8 - OOP Inheritance11111111111.pptx
Week 8 - OOP Inheritance11111111111.pptx
NajamUlHassan73
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
VishnuSupa
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
MananPasricha
 
Introduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritanceIntroduction to inheritance and different types of inheritance
Introduction to inheritance and different types of inheritance
huzaifaakram12
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
LalfakawmaKh
 
Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03Object Oriented Design and Programming Unit-03
Object Oriented Design and Programming Unit-03
Sivakumar M
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
Tansh5
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
Redwan Islam
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
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
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
Sujan Mia
 
OOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHatOOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHat
Scholarhat
 
Ad

More from Vishal Patil (9)

NAAC CSE PRESENTATION
NAAC CSE PRESENTATIONNAAC CSE PRESENTATION
NAAC CSE PRESENTATION
Vishal Patil
 
Normalization
NormalizationNormalization
Normalization
Vishal Patil
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
Vishal Patil
 
NAAC CSE PRESENTATION
NAAC CSE PRESENTATIONNAAC CSE PRESENTATION
NAAC CSE PRESENTATION
Vishal Patil
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
Vishal Patil
 
Ad

Recently uploaded (20)

Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........
jinny kaur
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Upstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptxUpstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptx
KshitijJayswal2
 
Fourth Semester BE CSE BCS401 ADA Module 3 PPT.pptx
Fourth Semester BE CSE BCS401 ADA Module 3 PPT.pptxFourth Semester BE CSE BCS401 ADA Module 3 PPT.pptx
Fourth Semester BE CSE BCS401 ADA Module 3 PPT.pptx
VENKATESHBHAT25
 
Gas Power Plant for Power Generation System
Gas Power Plant for Power Generation SystemGas Power Plant for Power Generation System
Gas Power Plant for Power Generation System
JourneyWithMe1
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
vlsi digital circuits full power point presentation
vlsi digital circuits full power point presentationvlsi digital circuits full power point presentation
vlsi digital circuits full power point presentation
DrSunitaPatilUgaleKK
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...
Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...
Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...
LiyaShaji4
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Elevate Your Workflow
Elevate Your WorkflowElevate Your Workflow
Elevate Your Workflow
NickHuld
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptxLecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
huzaifabilalshams
 
comparison of motors.pptx 1. Motor Terminology.ppt
comparison of motors.pptx 1. Motor Terminology.pptcomparison of motors.pptx 1. Motor Terminology.ppt
comparison of motors.pptx 1. Motor Terminology.ppt
yadavmrr7
 
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis""Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
Infopitaara
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........BTech_CSE_LPU_Presentation.pptx.........
BTech_CSE_LPU_Presentation.pptx.........
jinny kaur
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Upstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptxUpstream_processing of industrial products.pptx
Upstream_processing of industrial products.pptx
KshitijJayswal2
 
Fourth Semester BE CSE BCS401 ADA Module 3 PPT.pptx
Fourth Semester BE CSE BCS401 ADA Module 3 PPT.pptxFourth Semester BE CSE BCS401 ADA Module 3 PPT.pptx
Fourth Semester BE CSE BCS401 ADA Module 3 PPT.pptx
VENKATESHBHAT25
 
Gas Power Plant for Power Generation System
Gas Power Plant for Power Generation SystemGas Power Plant for Power Generation System
Gas Power Plant for Power Generation System
JourneyWithMe1
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
vlsi digital circuits full power point presentation
vlsi digital circuits full power point presentationvlsi digital circuits full power point presentation
vlsi digital circuits full power point presentation
DrSunitaPatilUgaleKK
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...
Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...
Explainable-Artificial-Intelligence-in-Disaster-Risk-Management (2).pptx_2024...
LiyaShaji4
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Elevate Your Workflow
Elevate Your WorkflowElevate Your Workflow
Elevate Your Workflow
NickHuld
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptxLecture 13 (Air and Noise Pollution and their Control) (1).pptx
Lecture 13 (Air and Noise Pollution and their Control) (1).pptx
huzaifabilalshams
 
comparison of motors.pptx 1. Motor Terminology.ppt
comparison of motors.pptx 1. Motor Terminology.pptcomparison of motors.pptx 1. Motor Terminology.ppt
comparison of motors.pptx 1. Motor Terminology.ppt
yadavmrr7
 
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis""Heaters in Power Plants: Types, Functions, and Performance Analysis"
"Heaters in Power Plants: Types, Functions, and Performance Analysis"
Infopitaara
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 

Inheritance in c++

  • 1. Object Oriented Programming Unit III Operators & Inheritance In C++ Prepared By Prof. Vishal S Patil Asst. Prof AEC/CSE Dept.
  • 2. Topics  Inheritance in C++  Types of Inheritance
  • 3. Inheritance in C++  The mechanism of deriving a class from another class is known as Inheritance.  Inheritance is the most importance concept of object oriented programming.  It allows us to define a class in terms of another class, which helps to create and maintain an application.  The main advantage of Inheritance is, it provides an opportunity to reuse the code functionality and fast implementation time.  The members of the class can be Public, Private or Protected.
  • 4.  Syntax: class DerivedClass : AccessSpecifier BaseClass  The default access specifier is Private.  Inheritance helps user to create a new class (derived class) from a existing class (base class).  Derived class inherits all the features from a Base class including additional feature of its own.
  • 5. Types of Inheritance. 1. Single Inheritance 2. Multiple Inheritance 3. Multilevel Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
  • 6. 1. Single Inheritance  In Single Inheritance, one class is derived from another class.  It represents a form of inheritance where there is only one base and derived class.
  • 7. // C++ program to explain Single inheritance #include <iostream.h> class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class Car: public Vehicle{ }; int main() { Car obj; // creating object of sub class will invoke the constructor of base classes return 0; }
  • 8.  Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes. Syntax class subclass_name : access_mode base_class1, access_mode base_class2, .... { //body of subclass };
  • 9. C++ program to explain multiple inheritance #include <iostream.h> class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class FourWheeler { public: FourWheeler() { cout << "This is a 4 wheeler Vehicle“; } }; class Car: public Vehicle, public FourWheeler { }; int main() { // creating object of sub class will // invoke the constructor of base classes Car obj; return 0; } Output: This is a Vehicle This is a 4 wheeler Vehicle
  • 10. 3.Multilevel Inheritance In this type of inheritance, a derived class is created from another derived class.
  • 11. C++ program to implement Multilevel Inheritance #include <iostream.h> class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class fourWheeler: public Vehicle { public: fourWheeler() { cout<<"Objects with 4 wheels are vehicles"<<endl; } }; class Car: public fourWheeler{ public: car() { cout<<"Car has 4 Wheels"<<endl; } }; int main() { Car obj; return 0; } output: This is a Vehicle Objects with 4 wheels are vehicles Car has 4 Wheels
  • 12. 4 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.
  • 13. C++ program to implement Hierarchical Inheritance #include <iostream> class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class Car: public Vehicle { }; class Bus: public Vehicle { }; int main() { Car obj1; Bus obj2; return 0; } Output: This is a Vehicle This is a Vehicle
  • 14. 5 Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance. Below image shows the combination of hierarchical and multiple inheritance:
  • 15. // C++ program for Hybrid Inheritance #include <iostream.h> class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class Fare { public: Fare() { cout<<"Fare of Vehiclen"; } }; class Car: public Vehicle { }; class Bus: public Vehicle, public Fare { }; int main() { Bus obj2; return 0; } Output: This is a Vehicle Fare of Vehicle