SlideShare una empresa de Scribd logo
1 de 16
• IN C++, INHERITANCE IS A PROCESS IN WHICH ONE OBJECT ACQUIRES ALL THE PROPERTIES AND BEHAVIORS OF ITS
PARENT OBJECT AUTOMATICALLY. IN SUCH WAY, YOU CAN REUSE, EXTEND OR MODIFY THE ATTRIBUTES AND
BEHAVIORS WHICH ARE DEFINED IN OTHER CLASS.
• IN C++, THE CLASS WHICH INHERITS THE MEMBERS OF ANOTHER CLASS IS CALLED DERIVED CLASS AND THE CLASS
WHOSE MEMBERS ARE INHERITED IS CALLED BASE CLASS. THE DERIVED CLASS IS THE SPECIALIZED CLASS FOR THE
BASE CLASS.
• THE PROCESS OF OBTAINING THE DATA MEMBERS AND METHODS FROM ONE CLASS TO ANOTHER CLASS IS KNOWN
AS INHERITANCE. IT IS ONE OF THE FUNDAMENTAL FEATURES OF OBJECT-ORIENTED PROGRAMMING.
SYNTAX
class derived_class : visibility_mode base_class
{
//members of the derived class
};
The existing class from which the derived class gets inherited is known as the base
class. It acts as a parent for its child class and all its properties i.e. public and
protected members get inherited to its derived class.
EXAMPLES:
#include <iostream>
class Teacher {
public:
Teacher()
{
std::cout<<"Hey Guys, I am a teacher"<<endl;
}
string collegeName = "Beginnersbook";
};
//This class inherits Teacher class
class MathTeacher: public Teacher
{
public:
MathTeacher()
{
std::cout<<"I am a Math Teacher"<<endl;
}
string mainSub = "Math";
string name = "Negan";
};
int main()
{
MathTeacher obj;
std::cout<<"Name: "<<obj.name<<endl;
std:: cout<<"College Name: "<<obj.collegeName<<endl;
std:: cout<<"Main Subject: "<<obj.mainSub<<endl;
return 0;
}
OUTPUT :
Hey Guys, I am a teacher
I am a Math Teacher
Name: Negan College
Name: Beginnersbook
Main Subject: Math
BENEFITS :
• If we develop any application using this concept than that application have
following advantages.
• Application development time is less.
• Application take less memory.
• Application execution time is less.
• Application performance is enhance (improved).
• Redundancy (repetition) of the code is reduced or minimized so that we
get consistence results and less storage cost.
• Use of Virtual Keyword
TYPES :
1. SINGLE INHERITANCE :
When a single class is derived from a single parent class, it is called Single inheritance. It is
the simplest of all inheritance.
In single inheritance, there is only one base class and one derived class. The Derived class gets
inherited from its base class.
Base Class
Derived Class
SYNTAX :
class base_classname
{
properties...
methods...
};
class derived_classname :
visibility_mode base_classname
{
properties...
methods...
};
EXAMPLE :
#include <iostream>
class Vehicle {
public:
Vehicle()
{
std::cout << "This is a Vehicle" << endl;
}
};
class Car: public Vehicle{
};
// main function
int main()
{
Car obj;
return 0;
}
OUTPUT :
This is a vehicle
2. 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..
Derived Class
SYNTAX :
class base_class1
{
properties;
methods;
};
class base_class2
{
properties;
methods;
};
class derived_classname : visibility_mode base_class1, visibility_mode
base_class2
{
properties;
methods;
};
Base Class
1
Base Class 2
EXAMPLE :
#include <iostream>
class Vehicle {
public:
Vehicle()
{
std::cout << "This is a Vehicle" << endl;
}
};
class FourWheeler {
public:
FourWheeler()
{
std::cout << "This is a 4 wheeler Vehicle"
<< endl;
}
};
class Car: public Vehicle, public FourWheeler
{
};
int main()
{
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.
CLASS B
CLASS A
CLASS C Derived
Class
SYNTAX :
class base_classname
{
properties; methods;
};
class intermediate_classname:visibility_mode base_classname
{
properties;
methods;
};
class child_classname:visibility_mode intermediate_classname
{
properties;
methods;
};
Base Class
1
Base Class
Intermediate
Derived Class
EXAMPLE :
#include <iostream>
class Vehicle
{
public:
Vehicle()
{
std::cout << "This is a Vehicle" << endl;
}
};
class fourWheeler: public Vehicle
{
public:
fourWheeler()
{
std::cout<<"Objects with 4 wheels are
vehicles"<<endl;
}
};
fourWheeler
class Car: public fourWheeler{
public:
car()
{
std::cout<<"Car has 4 Wheels"<<endl;
}
};
int main()
{
Car obj;
return 0;
}
:
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.
CLASS A
CLASS D
CLASS C
CLASS B
Derived
Class
SYNTAX :
class base_classname {
properties;
methods;
};
class derived_class1:visibility_mode base_classname {
properties; methods;
};
class derived_class2:visibility_mode base_classname {
properties;
methods;
}; ... ... ...
class derived_classN:visibility_mode base_classname {
properties;
methods; };
Base Class
1
Base Class
Base Class Base Class
EXAMPLE :
#include <iostream>
class Vehicle
{
public:
Vehicle()
{
std::cout << "This is a Vehicle" << endl;
}
};
class Car: public Vehicle
{
};
class Bus: public Vehicle
{
};
int main()
{
Car obj1;
Bus obj2;
return 0;
}
:
This is a Vehicle
This is a Vehicle
5. HYBRID INHERITANCE :
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example:
Combining Hierarchical inheritance and Multiple Inheritance.
SYNTAX :
class A
{
};
class B: public A
{
};
class C: public A
{
};
class D: public B, public C
{
};
CLASS A
CLASS C
CLASS D
CLASS B
Most Parent
class
Parent class/
Child class
Parent class/
Child class
Child class
EXAMPLE :
#include <iostream>
class Vehicle
{
public:
Vehicle()
{
std::cout << "This is a Vehicle" << endl;
}
};
class Fare
{
public:
Fare()
{
std::cout<<"Fare of Vehiclen";
}
};
{
};
class Bus: public Vehicle, public Fare
{
};
int main()
{
Bus obj2;
return 0;
}
:
This is a Vehicle
Fare of Vehicle
THANK
YOU

Más contenido relacionado

La actualidad más candente

Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingArslan Waseem
 
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++Nikunj Patel
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)Redwan Islam
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++Sujan Mia
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 

La actualidad más candente (20)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Inheritance
Inheritance Inheritance
Inheritance
 
Introduction to Object-Oriented Concepts and Java
Introduction to Object-Oriented Concepts and Java Introduction to Object-Oriented Concepts and Java
Introduction to Object-Oriented Concepts and Java
 

Similar a Inheritance in c++

Similar a Inheritance in c++ (20)

Inheritance and Polymorphism in Oops
Inheritance and Polymorphism in OopsInheritance and Polymorphism in Oops
Inheritance and Polymorphism in Oops
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
ABAP Object oriented concepts
ABAP Object oriented conceptsABAP Object oriented concepts
ABAP Object oriented concepts
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Only oop
Only oopOnly oop
Only oop
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
 
Inheritance
InheritanceInheritance
Inheritance
 
C++Presentation 2.PPT
C++Presentation 2.PPTC++Presentation 2.PPT
C++Presentation 2.PPT
 

Último

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
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...Amil baba
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
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.pdfDr Vijay Vishwakarma
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
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.pptxMaritesTamaniVerdade
 

Último (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
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...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
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
 

Inheritance in c++

  • 1.
  • 2. • IN C++, INHERITANCE IS A PROCESS IN WHICH ONE OBJECT ACQUIRES ALL THE PROPERTIES AND BEHAVIORS OF ITS PARENT OBJECT AUTOMATICALLY. IN SUCH WAY, YOU CAN REUSE, EXTEND OR MODIFY THE ATTRIBUTES AND BEHAVIORS WHICH ARE DEFINED IN OTHER CLASS. • IN C++, THE CLASS WHICH INHERITS THE MEMBERS OF ANOTHER CLASS IS CALLED DERIVED CLASS AND THE CLASS WHOSE MEMBERS ARE INHERITED IS CALLED BASE CLASS. THE DERIVED CLASS IS THE SPECIALIZED CLASS FOR THE BASE CLASS. • THE PROCESS OF OBTAINING THE DATA MEMBERS AND METHODS FROM ONE CLASS TO ANOTHER CLASS IS KNOWN AS INHERITANCE. IT IS ONE OF THE FUNDAMENTAL FEATURES OF OBJECT-ORIENTED PROGRAMMING.
  • 3. SYNTAX class derived_class : visibility_mode base_class { //members of the derived class }; The existing class from which the derived class gets inherited is known as the base class. It acts as a parent for its child class and all its properties i.e. public and protected members get inherited to its derived class.
  • 4. EXAMPLES: #include <iostream> class Teacher { public: Teacher() { std::cout<<"Hey Guys, I am a teacher"<<endl; } string collegeName = "Beginnersbook"; }; //This class inherits Teacher class class MathTeacher: public Teacher { public: MathTeacher() { std::cout<<"I am a Math Teacher"<<endl; } string mainSub = "Math"; string name = "Negan"; }; int main() { MathTeacher obj; std::cout<<"Name: "<<obj.name<<endl; std:: cout<<"College Name: "<<obj.collegeName<<endl; std:: cout<<"Main Subject: "<<obj.mainSub<<endl; return 0; } OUTPUT : Hey Guys, I am a teacher I am a Math Teacher Name: Negan College Name: Beginnersbook Main Subject: Math
  • 5. BENEFITS : • If we develop any application using this concept than that application have following advantages. • Application development time is less. • Application take less memory. • Application execution time is less. • Application performance is enhance (improved). • Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost. • Use of Virtual Keyword
  • 6. TYPES : 1. SINGLE INHERITANCE : When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance. In single inheritance, there is only one base class and one derived class. The Derived class gets inherited from its base class. Base Class Derived Class SYNTAX : class base_classname { properties... methods... }; class derived_classname : visibility_mode base_classname { properties... methods... };
  • 7. EXAMPLE : #include <iostream> class Vehicle { public: Vehicle() { std::cout << "This is a Vehicle" << endl; } }; class Car: public Vehicle{ }; // main function int main() { Car obj; return 0; } OUTPUT : This is a vehicle
  • 8. 2. 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.. Derived Class SYNTAX : class base_class1 { properties; methods; }; class base_class2 { properties; methods; }; class derived_classname : visibility_mode base_class1, visibility_mode base_class2 { properties; methods; }; Base Class 1 Base Class 2
  • 9. EXAMPLE : #include <iostream> class Vehicle { public: Vehicle() { std::cout << "This is a Vehicle" << endl; } }; class FourWheeler { public: FourWheeler() { std::cout << "This is a 4 wheeler Vehicle" << endl; } }; class Car: public Vehicle, public FourWheeler { }; int main() { 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. CLASS B CLASS A CLASS C Derived Class SYNTAX : class base_classname { properties; methods; }; class intermediate_classname:visibility_mode base_classname { properties; methods; }; class child_classname:visibility_mode intermediate_classname { properties; methods; }; Base Class 1 Base Class Intermediate Derived Class
  • 11. EXAMPLE : #include <iostream> class Vehicle { public: Vehicle() { std::cout << "This is a Vehicle" << endl; } }; class fourWheeler: public Vehicle { public: fourWheeler() { std::cout<<"Objects with 4 wheels are vehicles"<<endl; } }; fourWheeler class Car: public fourWheeler{ public: car() { std::cout<<"Car has 4 Wheels"<<endl; } }; int main() { Car obj; return 0; } : 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. CLASS A CLASS D CLASS C CLASS B Derived Class SYNTAX : class base_classname { properties; methods; }; class derived_class1:visibility_mode base_classname { properties; methods; }; class derived_class2:visibility_mode base_classname { properties; methods; }; ... ... ... class derived_classN:visibility_mode base_classname { properties; methods; }; Base Class 1 Base Class Base Class Base Class
  • 13. EXAMPLE : #include <iostream> class Vehicle { public: Vehicle() { std::cout << "This is a Vehicle" << endl; } }; class Car: public Vehicle { }; class Bus: public Vehicle { }; int main() { Car obj1; Bus obj2; return 0; } : This is a Vehicle This is a Vehicle
  • 14. 5. HYBRID INHERITANCE : Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance. SYNTAX : class A { }; class B: public A { }; class C: public A { }; class D: public B, public C { }; CLASS A CLASS C CLASS D CLASS B Most Parent class Parent class/ Child class Parent class/ Child class Child class
  • 15. EXAMPLE : #include <iostream> class Vehicle { public: Vehicle() { std::cout << "This is a Vehicle" << endl; } }; class Fare { public: Fare() { std::cout<<"Fare of Vehiclen"; } }; { }; class Bus: public Vehicle, public Fare { }; int main() { Bus obj2; return 0; } : This is a Vehicle Fare of Vehicle