SlideShare a Scribd company logo
1 of 15
INHERITANCE OOPS Concept
WHAT IS INHERITANCE?
• Inheritance is a feature or a process in which, new classes are created from the
existing classes.
• The new class created is called “derived class” or “child class” and the existing class
is known as the “base class” or “parent class”.
• The derived class now is said to be inherited from the base class.
NEED OF INHERITANCE?
• Inheritance enables code reusability and saves time.
• Inheritance is used to declare characteristics of classes inheriting it , without giving
its implementation.
• It is one of the most important concept of OOPS.
TYPES
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance (AKA Virtual Inheritance)
SINGLE INHERITANCE
• In single inheritance, a class is allowed to inherit from only one
class. i.e. one subclass is inherited by one base class only.
• It is the simplest among all the types of inheritance since it
does not include any kind of inheritance combination or
different levels of inheritance.
CODE - SINGLE INHERITANCE
#include<iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehiclen";
}
};
// sub class derived from a single base classes
class Car : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output
This is a Vehicle
MULTIPLE INHERITANCE
• Multiple Inheritance is a feature of C++ where a class can
inherit from more than one class. i.e one subclass is inherited
from more than one base class.
• It is distinct from single inheritance, where an object or class
may only inherit from one particular object or class.
CODE - MULTIPLE INHERITANCE
#include <iostream>
using namespace std;
// first base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehiclen";
}
};
// sub class derived from two base classes
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
MULTILEVEL INHERITANCE
• In this type of inheritance, a derived class is created from
another derived class.
CODE - MULTIPLE INHERITANCE
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// first sub_class derived from class vehicle
class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehiclesn";
}
};
// sub class derived from the derived base class fourWheeler
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheelsn"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
HIERARCHICAL INHERITANCE
• In this type of inheritance, more than one subclass is inherited
from a single base class. i.e. more than one derived class is
created from a single base class.
CODE – HIERARCHICAL
INHERITANCE
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
HYBRID (VIRTUAL) INHERITANCE
• Hybrid Inheritance is implemented by combining more than
one type of inheritance. For example: Combining Hierarchical
inheritance and Multiple Inheritance.
CODE – HYBRID (VIRTUAL)
INHERITANCE
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehiclen"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public Fare {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj1;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
ACCESS MODIFIERS IN INHERITANCE
THANK YOU

More Related Content

Similar to Inheritance.pptx

Similar to Inheritance.pptx (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Programming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxProgramming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptx
 
Ritik (inheritance.cpp)
Ritik (inheritance.cpp)Ritik (inheritance.cpp)
Ritik (inheritance.cpp)
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Polymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPolymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformatics
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 

Recently uploaded

Recently uploaded (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Inheritance.pptx

  • 2. WHAT IS INHERITANCE? • Inheritance is a feature or a process in which, new classes are created from the existing classes. • The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. • The derived class now is said to be inherited from the base class. NEED OF INHERITANCE? • Inheritance enables code reusability and saves time. • Inheritance is used to declare characteristics of classes inheriting it , without giving its implementation. • It is one of the most important concept of OOPS.
  • 3. TYPES 1. Single inheritance 2. Multiple inheritance 3. Multilevel inheritance 4. Hierarchical inheritance 5. Hybrid inheritance (AKA Virtual Inheritance)
  • 4. SINGLE INHERITANCE • In single inheritance, a class is allowed to inherit from only one class. i.e. one subclass is inherited by one base class only. • It is the simplest among all the types of inheritance since it does not include any kind of inheritance combination or different levels of inheritance.
  • 5. CODE - SINGLE INHERITANCE #include<iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // sub class derived from a single base classes class Car : public Vehicle { }; // main function int main() { // Creating object of sub class will // invoke the constructor of base classes Car obj; return 0; } Output This is a Vehicle
  • 6. MULTIPLE INHERITANCE • Multiple Inheritance is a feature of C++ where a class can inherit from more than one class. i.e one subclass is inherited from more than one base class. • It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.
  • 7. CODE - MULTIPLE INHERITANCE #include <iostream> using namespace std; // first base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // second base class class FourWheeler { public: FourWheeler() { cout << "This is a 4 wheeler Vehiclen"; } }; // sub class derived from two base classes 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
  • 8. MULTILEVEL INHERITANCE • In this type of inheritance, a derived class is created from another derived class.
  • 9. CODE - MULTIPLE INHERITANCE #include <iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // first sub_class derived from class vehicle class fourWheeler : public Vehicle { public: fourWheeler() { cout << "Objects with 4 wheels are vehiclesn"; } }; // sub class derived from the derived base class fourWheeler class Car : public fourWheeler { public: Car() { cout << "Car has 4 Wheelsn"; } }; // main function int main() { // Creating object of sub class will // invoke the constructor of base classes. Car obj; return 0; } Output This is a Vehicle Objects with 4 wheels are vehicles Car has 4 Wheels
  • 10. HIERARCHICAL INHERITANCE • In this type of inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class.
  • 11. CODE – HIERARCHICAL INHERITANCE #include <iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // first sub class class Car : public Vehicle { }; // second sub class class Bus : public Vehicle { }; // main function int main() { // Creating object of sub class will // invoke the constructor of base class. Car obj1; Bus obj2; return 0; } Output This is a Vehicle This is a Vehicle
  • 12. HYBRID (VIRTUAL) INHERITANCE • Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.
  • 13. CODE – HYBRID (VIRTUAL) INHERITANCE #include <iostream> using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehiclen"; } }; // base class class Fare { public: Fare() { cout << "Fare of Vehiclen"; } }; // first sub class class Car : public Vehicle { }; // second sub class class Bus : public Vehicle, public Fare { }; // main function int main() { // Creating object of sub class will // invoke the constructor of base class. Bus obj1; return 0; } Output This is a Vehicle Fare of Vehicle
  • 14. ACCESS MODIFIERS IN INHERITANCE