SlideShare a Scribd company logo
1 of 11
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Subject- Object Oriented Programming (CO212)
Unit 2 – Overloading and Inheritance
Topic – 2.6 Types of Inheritance
Prof. I. B. Tirse
Assistant Professor
E-mail : tirseishwaricomp@sanjivani.org.in
Contact No: 7507113718
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2
Types of Inheritance
Inheritance are of the following types:
• Single Inheritance
• Multi level Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3
Single Inheritance
superclass(baseclass)
subclass(derived class)
Defining the simple Inheritance
class vehicle
{
…..
};
class car : visibility-mode vehicle
{
…………
};
• Single Inheritance is a process in which a sub class is derived from only one superclass.
• A class Car is derived from the class Vehicle
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4
Multi level Inheritance
• C++ also provides the facility of multilevel inheritance, according to which the derived class
can also be derived by an another class, which in turn can further be inherited by another
and so on called as Multilevel or Varied Inheritance.
• In the above figure, class Base represents the base class. The class Derive1 that is called
first level of inheritance, inherits the class Base.
• The derived class Derive1 is further inherited by the class Derive2, which is called second
level of inheritance.
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5
Example of Multi level Inheritance
#include <iostream>
using namespace std;
class base
{
public:
int x;
void getdata()
{
cout << "Enter value of x= ";
cin >> x;
}
};
class derive1 : public base
{
public:
int y;
void readdata()
{
cout << "Enter value of y= ";
cin >> y;
}
};
class derive2 : public derive1
{
private:
int z;
public:
void indata()
{
cout << "Enter value of z= ";
cin >> z;
}
void product()
{
cout << "Product= " << x * y * z;
}
};
int main()
{
derive2 a;
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
Output
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6
MultipleInheritance
• Deriving a class from more than one direct base classes is called multiple
inheritance.
Defining the Multiple Inheritance
class A { /* ... */ };
class B { /* ... */ };
class C { /* ... */ };
class X :visibilty-mode A, visibilty-mode B, visibilty-mode C
{ /* ... */ };
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7
Example of Multiple Inheritance
class Circle // First base class
{
protected:
float radius ;
public:
void Enter_r()
{
cout << "Enter the radius: ";
cin >> radius ;
}
void Display_CA()
{
cout << "Area = "<<(3.14 *radius*radius) ;
}
};
class Rectangle // Second baseclass
{
protected:
float length, breadth;
public:
void Enter_lb()
{
cout << "Enter the length: ";
cin >> length ;
cout << “Enter the breadth : ” ;
cin >> breadth ;
}
void Display_RA()
{
cout <<"Area =" <<(length * breadth);
}
};
class Cylinder:public Circle,public Rectangle
{
public:
void volume_cy()
{
cout << "Volume of cylinder is: "<<
(3.14*radius*radius*length) ;
}
};
int main()
{
Cylinder c,r,cy ;
c.Enter_r( );
c.Display_CA();
r.Enter_lb( );
r.Display_RA( );
cy.Enter_r( );
cy.Enter_lb( );
cy.volume_cy();
return 0;
}
Output:
Enter the radius: 2
Area=12.56
Enter the length: 2
Enter the breadth : 3
Area= 6
Enter the radius: 5
Enter the length: 6
Enter the breadth : 7
Volume of cylinder is:471
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8
Hierarchical Inheritance
• If a number of classes are derived from a single base class, it is called as hierarchical
inheritance
• Defining the Hierarchical Inheritance :
Class A
{…………….};
Class B: visibility-mode A
{………..…..};
Class C: visibility-mode A
{…………....};
Class D: visibility-mode A
{…………….};
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9
Example using Hierarchical Inheritance
#include <iostream>
using namespace std;
class A
{
public:
int x, y;
void getdata()
{
cout << "Enter value of x & y:";
cin >> x >> y;
}
};
class B : public A
{
public:
void product()
{
cout << "Product= " << x * y;
}
};
class C : public A
{
public:
void sum()
{
cout<<"Sum= "<< x + y;
}
};
int main()
{
B obj1;
C obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Output
Enter value of x & y: 2 3
Product= 6
Enter value of x & y: 2 3
Sum= 5
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10
Hybrid Inheritance
• In this type, more than one type of inheritance are used to derive a new sub-class.
• Multiple and Multilevel type of inheritances are used to derive a class PG-Student.
class Person
{ ……};
class Student : public Person
{ ……};
class GateScore
{…….};
class PG-Student : public Student, public GateScore
{………};
Person
Student
PG - Student
GateScore
Thank you..
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11

More Related Content

Similar to 2.6 Types of Inheritance in OOP C++.pptx

Similar to 2.6 Types of Inheritance in OOP C++.pptx (20)

inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPM
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
JAVAPGMS.docx
JAVAPGMS.docxJAVAPGMS.docx
JAVAPGMS.docx
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Inheritance_PART2.pptx
Inheritance_PART2.pptxInheritance_PART2.pptx
Inheritance_PART2.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGEPROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
CS3391 -OOP -UNIT – II  NOTES FINAL.pdfCS3391 -OOP -UNIT – II  NOTES FINAL.pdf
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
 
What is inheritance
What is inheritanceWhat is inheritance
What is inheritance
 
Unit 4
Unit 4Unit 4
Unit 4
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
11slide
11slide11slide
11slide
 

Recently uploaded

Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
IJECEIAES
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
Kamal Acharya
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
BalamuruganV28
 
Performance enhancement of machine learning algorithm for breast cancer diagn...
Performance enhancement of machine learning algorithm for breast cancer diagn...Performance enhancement of machine learning algorithm for breast cancer diagn...
Performance enhancement of machine learning algorithm for breast cancer diagn...
IJECEIAES
 
Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
Sampad Kar
 

Recently uploaded (20)

Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
Performance enhancement of machine learning algorithm for breast cancer diagn...
Performance enhancement of machine learning algorithm for breast cancer diagn...Performance enhancement of machine learning algorithm for breast cancer diagn...
Performance enhancement of machine learning algorithm for breast cancer diagn...
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
"United Nations Park" Site Visit Report.
"United Nations Park" Site  Visit Report."United Nations Park" Site  Visit Report.
"United Nations Park" Site Visit Report.
 

2.6 Types of Inheritance in OOP C++.pptx

  • 1. Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423 603 (An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune) NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified Department of Computer Engineering (NBA Accredited) Subject- Object Oriented Programming (CO212) Unit 2 – Overloading and Inheritance Topic – 2.6 Types of Inheritance Prof. I. B. Tirse Assistant Professor E-mail : tirseishwaricomp@sanjivani.org.in Contact No: 7507113718
  • 2. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2 Types of Inheritance Inheritance are of the following types: • Single Inheritance • Multi level Inheritance • Multiple Inheritance • Hierarchical Inheritance • Hybrid Inheritance
  • 3. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3 Single Inheritance superclass(baseclass) subclass(derived class) Defining the simple Inheritance class vehicle { ….. }; class car : visibility-mode vehicle { ………… }; • Single Inheritance is a process in which a sub class is derived from only one superclass. • A class Car is derived from the class Vehicle
  • 4. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4 Multi level Inheritance • C++ also provides the facility of multilevel inheritance, according to which the derived class can also be derived by an another class, which in turn can further be inherited by another and so on called as Multilevel or Varied Inheritance. • In the above figure, class Base represents the base class. The class Derive1 that is called first level of inheritance, inherits the class Base. • The derived class Derive1 is further inherited by the class Derive2, which is called second level of inheritance.
  • 5. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5 Example of Multi level Inheritance #include <iostream> using namespace std; class base { public: int x; void getdata() { cout << "Enter value of x= "; cin >> x; } }; class derive1 : public base { public: int y; void readdata() { cout << "Enter value of y= "; cin >> y; } }; class derive2 : public derive1 { private: int z; public: void indata() { cout << "Enter value of z= "; cin >> z; } void product() { cout << "Product= " << x * y * z; } }; int main() { derive2 a; a.getdata(); a.readdata(); a.indata(); a.product(); return 0; } Output Enter value of x= 2 Enter value of y= 3 Enter value of z= 3 Product= 18
  • 6. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6 MultipleInheritance • Deriving a class from more than one direct base classes is called multiple inheritance. Defining the Multiple Inheritance class A { /* ... */ }; class B { /* ... */ }; class C { /* ... */ }; class X :visibilty-mode A, visibilty-mode B, visibilty-mode C { /* ... */ };
  • 7. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7 Example of Multiple Inheritance class Circle // First base class { protected: float radius ; public: void Enter_r() { cout << "Enter the radius: "; cin >> radius ; } void Display_CA() { cout << "Area = "<<(3.14 *radius*radius) ; } }; class Rectangle // Second baseclass { protected: float length, breadth; public: void Enter_lb() { cout << "Enter the length: "; cin >> length ; cout << “Enter the breadth : ” ; cin >> breadth ; } void Display_RA() { cout <<"Area =" <<(length * breadth); } }; class Cylinder:public Circle,public Rectangle { public: void volume_cy() { cout << "Volume of cylinder is: "<< (3.14*radius*radius*length) ; } }; int main() { Cylinder c,r,cy ; c.Enter_r( ); c.Display_CA(); r.Enter_lb( ); r.Display_RA( ); cy.Enter_r( ); cy.Enter_lb( ); cy.volume_cy(); return 0; } Output: Enter the radius: 2 Area=12.56 Enter the length: 2 Enter the breadth : 3 Area= 6 Enter the radius: 5 Enter the length: 6 Enter the breadth : 7 Volume of cylinder is:471
  • 8. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8 Hierarchical Inheritance • If a number of classes are derived from a single base class, it is called as hierarchical inheritance • Defining the Hierarchical Inheritance : Class A {…………….}; Class B: visibility-mode A {………..…..}; Class C: visibility-mode A {…………....}; Class D: visibility-mode A {…………….};
  • 9. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9 Example using Hierarchical Inheritance #include <iostream> using namespace std; class A { public: int x, y; void getdata() { cout << "Enter value of x & y:"; cin >> x >> y; } }; class B : public A { public: void product() { cout << "Product= " << x * y; } }; class C : public A { public: void sum() { cout<<"Sum= "<< x + y; } }; int main() { B obj1; C obj2; obj1.getdata(); obj1.product(); obj2.getdata(); obj2.sum(); return 0; } Output Enter value of x & y: 2 3 Product= 6 Enter value of x & y: 2 3 Sum= 5
  • 10. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10 Hybrid Inheritance • In this type, more than one type of inheritance are used to derive a new sub-class. • Multiple and Multilevel type of inheritances are used to derive a class PG-Student. class Person { ……}; class Student : public Person { ……}; class GateScore {…….}; class PG-Student : public Student, public GateScore {………}; Person Student PG - Student GateScore
  • 11. Thank you.. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11