SlideShare a Scribd company logo
1 of 16
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/

More Related Content

What's hot

Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdmHarshal Misalkar
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++vivekkumar2938
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++Sujan Mia
 

What's hot (20)

Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
inheritance
inheritanceinheritance
inheritance
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 

Similar to Inheritance in c++

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 PasrichaMananPasricha
 
Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsLalfakawmaKh
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptxTansh5
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Polymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPolymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPriyanshuMittal31
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfitxminahil29
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 

Similar to Inheritance in c++ (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
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
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Polymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPolymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformatics
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdf
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 

More from Vishal Patil

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

More from Vishal Patil (9)

NAAC CSE PRESENTATION
NAAC CSE PRESENTATIONNAAC CSE PRESENTATION
NAAC CSE PRESENTATION
 
Normalization
NormalizationNormalization
Normalization
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by 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
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
 

Recently uploaded

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

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