SlideShare a Scribd company logo
1 of 26
INHERENTANCE
OBJECT ORIENTED
PROGRAMMING WITH
C++
LECTURE FIVE
Lesson Objectives
 Definition of Inheritance
 Hierarchy of Inheritance
 Type of Inheritance
 C++ Inheritance Implementation
Introduction
 Inheritance denotes an is-a relationship.
 The natural phenomenon where one concept can be
viewed as a sub-concept of another
Mammal
Human
More General class
More Specific class
is-a
Inheritance
 What do the sub-class inherit from the Main class? All
Features!
 Can there be sub-class have feature not in the main
class? Yes!
 Human inherits all features in
the mammal class in addition to
his own features!
Mammal
Mammal Glands
Vertebral Column
Human
Mammalian glans
Vertebral column
is-a
Examples of Inheritance
 A car is a vehicle.
 Earth is a planet
 Orange is a fruit.
 A surgeon is a doctor.
 A dog is an animal.
Hierarchy of Inheritances
Mammal
Mammal Glands
Vertebral Column
Human
Vertical orientation
Dog
Non Vertical orientation
Homo
Sapien
Homo
Erectus
Dalmatian Doodle
Inheritance: Adopted in OOP
 The process of creating new classes, from existing
ones.
 The existing class is called base/Super classes
 The new class is called derived/Sub classes
 A Derived class has the potential to inherit all the
capabilities of the base class in addition to its own.
Inheritance - Illustration
 A Derived class has the potential to inherit all the
capabilities of the base class in addition to its own.
Base Class
Feature A
Feature B
Subclass
Feature A
Feature B
Feature C
Inherits From
Advantages of Inheritance
 Permits code reusability
 Once a class has been written to perform a given function, it
can always be adapted to perform other functions through
inheritance instead of modifying its code.
 Reusing existing code saves time and money and increases a
program’s reliability.
 Reusability ensures the ease of distributing class libraries.
Specifying the Derived Class
 Inheritance is specified using the colon (:) as
follows:
class Subclass: Baseclass
 If Baseclass has already been declared and accessible
Type of Inheritance (Access )
 When deriving a class from a base class, the base class may
be inherited through public, protected or private inheritance.
The type of inheritance is specified by the access- specifier.
 We hardly use protected or private inheritance,
but public inheritance is commonly used. While using
different type of inheritance, following rules are applied:
Type of Inheritance
 Public Inheritance: When deriving a class from
a public base class, public members of the base class
become public members of the derived class
and protected members of the base class
become protected members of the derived class.
 A base class's private members are never accessible directly
from a derived class, but can be accessed through calls to
the public and protected members of the base class.
Type of Inheritance
 Protected Inheritance: When deriving from a protected base
class, public and protected members of the base class
become protected members of the derived class.
 Private Inheritance: When deriving from a private base
class, public and protected members of the base class
become private members of the derived class
C++ Inheritance
 General Format for implementing the concept of Inheritance:
 class derived_classname: access specifier baseclassname
 For example, if the base class is MyClass and the derived class is
sample it is specified as:
 class sample: public MyClass
 The above makes sample have access to both public and protected
variables of base class MyClass
C++ Inheritance
 Inheritance Example:
 class MyClass
 { public:
MyClass(void)
 { x=0; }
void f(int n1)
{ x= n1*5;}
 void output(void)
 { cout<<x; }
 private:
int x;
};
C++ Inheritance
 Inheritance Example:
 class sample: public MyClass
{
 public:
sample(void) { s1=0; }
 void f1(int n1)
{ s1=n1*10;}
 void output(void)
{
 MyClass::output(); cout << s1; }
 private:
int s1;
C++ Inheritance
 Inheritance Example:
 int main(void)
{ sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}
 The output of the above program is
 50
200
Types of Inheritance (Hierarchy)
1. Single class Inheritance:
Single inheritance is the one where you have a single base class and a
single derived class.
Class Employee
Class Manager
It is a Base class (super)
it is a sub class (derived)
Types of Inheritance (Hierarchy)
2. Multilevel Inheritance:
In Multi level inheritance, a class inherits its properties from
another derived class.
Class A
Class B
it is a Base class
(super) of B
it is a sub class (derived) of
A and base class of class C
Class C derived class(sub) of
class B
Types of Inheritance (Hierarchy)
3. Multiple Inheritances:
In Multiple inheritances, a derived class inherits from multiple
base classes. It has properties of both the base classes.
Class A Class B Base class
Class C Derived class
Types of Inheritance
5. Hybrid Inheritance:
Class A
Class B
Class D
Class C
Summary
Definition of Inheritance
Hierarchy of Inheritance
Type of Inheritance
C++ Inheritance Implementation
Question Time
Individual Assignment
Brain Teaser
• After her death, a mother left her sons her entire £5000
fortune.
• The eldest son received £3000.
• The rest was shared equally between whomever
remained.
• How much did they each get?
Brain Teaser
• A man left an inheritance of $10,000 to three relatives
and their wives. Together the wives received $3960.
Janine received $100 more than Cathy, and Maria
received $100 more than Janine. Joe received the same
amount as his wife, Hary got half as much again as his
wife, and Tom received twice as much as his wife.
• How much did each receive?

More Related Content

Similar to Lec5.ppt

Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
Deepak Singh
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 

Similar to Lec5.ppt (20)

Inheritance
InheritanceInheritance
Inheritance
 
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 OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Inheritance and its types explained.ppt
Inheritance and its types  explained.pptInheritance and its types  explained.ppt
Inheritance and its types explained.ppt
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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Ă...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
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
 
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
 
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
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
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.
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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
 

Lec5.ppt

  • 2. Lesson Objectives  Definition of Inheritance  Hierarchy of Inheritance  Type of Inheritance  C++ Inheritance Implementation
  • 3. Introduction  Inheritance denotes an is-a relationship.  The natural phenomenon where one concept can be viewed as a sub-concept of another Mammal Human More General class More Specific class is-a
  • 4. Inheritance  What do the sub-class inherit from the Main class? All Features!  Can there be sub-class have feature not in the main class? Yes!  Human inherits all features in the mammal class in addition to his own features! Mammal Mammal Glands Vertebral Column Human Mammalian glans Vertebral column is-a
  • 5. Examples of Inheritance  A car is a vehicle.  Earth is a planet  Orange is a fruit.  A surgeon is a doctor.  A dog is an animal.
  • 6. Hierarchy of Inheritances Mammal Mammal Glands Vertebral Column Human Vertical orientation Dog Non Vertical orientation Homo Sapien Homo Erectus Dalmatian Doodle
  • 7. Inheritance: Adopted in OOP  The process of creating new classes, from existing ones.  The existing class is called base/Super classes  The new class is called derived/Sub classes  A Derived class has the potential to inherit all the capabilities of the base class in addition to its own.
  • 8. Inheritance - Illustration  A Derived class has the potential to inherit all the capabilities of the base class in addition to its own. Base Class Feature A Feature B Subclass Feature A Feature B Feature C Inherits From
  • 9. Advantages of Inheritance  Permits code reusability  Once a class has been written to perform a given function, it can always be adapted to perform other functions through inheritance instead of modifying its code.  Reusing existing code saves time and money and increases a program’s reliability.  Reusability ensures the ease of distributing class libraries.
  • 10. Specifying the Derived Class  Inheritance is specified using the colon (:) as follows: class Subclass: Baseclass  If Baseclass has already been declared and accessible
  • 11. Type of Inheritance (Access )  When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access- specifier.  We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:
  • 12. Type of Inheritance  Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class.  A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • 13. Type of Inheritance  Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.  Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class
  • 14. C++ Inheritance  General Format for implementing the concept of Inheritance:  class derived_classname: access specifier baseclassname  For example, if the base class is MyClass and the derived class is sample it is specified as:  class sample: public MyClass  The above makes sample have access to both public and protected variables of base class MyClass
  • 15. C++ Inheritance  Inheritance Example:  class MyClass  { public: MyClass(void)  { x=0; } void f(int n1) { x= n1*5;}  void output(void)  { cout<<x; }  private: int x; };
  • 16. C++ Inheritance  Inheritance Example:  class sample: public MyClass {  public: sample(void) { s1=0; }  void f1(int n1) { s1=n1*10;}  void output(void) {  MyClass::output(); cout << s1; }  private: int s1;
  • 17. C++ Inheritance  Inheritance Example:  int main(void) { sample s; s.f(10); s.output(); s.f1(20); s.output(); }  The output of the above program is  50 200
  • 18. Types of Inheritance (Hierarchy) 1. Single class Inheritance: Single inheritance is the one where you have a single base class and a single derived class. Class Employee Class Manager It is a Base class (super) it is a sub class (derived)
  • 19. Types of Inheritance (Hierarchy) 2. Multilevel Inheritance: In Multi level inheritance, a class inherits its properties from another derived class. Class A Class B it is a Base class (super) of B it is a sub class (derived) of A and base class of class C Class C derived class(sub) of class B
  • 20. Types of Inheritance (Hierarchy) 3. Multiple Inheritances: In Multiple inheritances, a derived class inherits from multiple base classes. It has properties of both the base classes. Class A Class B Base class Class C Derived class
  • 21. Types of Inheritance 5. Hybrid Inheritance: Class A Class B Class D Class C
  • 22. Summary Definition of Inheritance Hierarchy of Inheritance Type of Inheritance C++ Inheritance Implementation
  • 25. Brain Teaser • After her death, a mother left her sons her entire £5000 fortune. • The eldest son received £3000. • The rest was shared equally between whomever remained. • How much did they each get?
  • 26. Brain Teaser • A man left an inheritance of $10,000 to three relatives and their wives. Together the wives received $3960. Janine received $100 more than Cathy, and Maria received $100 more than Janine. Joe received the same amount as his wife, Hary got half as much again as his wife, and Tom received twice as much as his wife. • How much did each receive?

Editor's Notes

  1. If more than one class is inherited from the base class, it's known as hierarchical inheritance. In hierarchical inheritance, all features that are common in child classes are included in the base class. For example, Physics, Chemistry, Biology are derived from Science class. Similarly, Dog, Cat, Horse are derived from Animal class.
  2. When we make a class as friend, all its member functions automatically become friend functions.
  3. When we make a class as friend, all its member functions automatically become friend functions.
  4. When we make a class as friend, all its member functions automatically become friend functions.
  5. When we make a class as friend, all its member functions automatically become friend functions.
  6. When we make a class as friend, all its member functions automatically become friend functions.
  7. When we make a class as friend, all its member functions automatically become friend functions.
  8. When we make a class as friend, all its member functions automatically become friend functions.
  9. When we make a class as friend, all its member functions automatically become friend functions.
  10. When we make a class as friend, all its member functions automatically become friend functions.
  11. When we make a class as friend, all its member functions automatically become friend functions.
  12. When we make a class as friend, all its member functions automatically become friend functions.
  13. When we make a class as friend, all its member functions automatically become friend functions.
  14. When we make a class as friend, all its member functions automatically become friend functions.
  15. Answer There is just one other son who takes the remaining £2000. The word elder means there are two. If there were more than two it would have been eldest.
  16. Answer Carthy, Janine and Maria received $1220, $1320, and $1420 respectively. Joe received the same as his wife Carol, $1220. Hary received half again what his wife Janine did, $1980. Tommy got twice as much as his wife Maria, $2840. The word elder means there are two. If there were more than two it would have been eldest.