SlideShare a Scribd company logo
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

PPTX
Inheritance in c++
PPT
6 Inheritance
PDF
lecture 6.pdf
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PPT
Inheritance
PPTX
Inheritance
PPTX
Programming Lesson by Slidesgo.pptx
Inheritance in c++
6 Inheritance
lecture 6.pdf
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheritance_OOPC_datastream.ppttttttttttttttx
Inheritance
Inheritance
Programming Lesson by Slidesgo.pptx

Similar to Lec5.ppt (20)

PPTX
Inheritance
PDF
Inheritance
PPTX
Inheritance
PPTX
Inheritance in C++ (Programming Fundamentals)
PPTX
EASY TO LEARN INHERITANCE IN C++
PPT
Inheritance OOP Concept in C++.
PDF
Chapter25 inheritance-i
PPTX
Inheritance in c++
PDF
inheritance-16031525566nbhij56604452.pdf
PPT
session 24_Inheritance.ppt
PPTX
Inheritance
PPTX
00ps inheritace using c++
PPTX
Inheritance in c++ part1
PPTX
Inheritance (with classifications)
PPTX
Inheritance in oops
PPSX
PPTX
TYPES OF INHERITANCE CONCEPT IN C++.pptx
PPTX
Aryan's pres. entation.pptx
PPT
Inheritance in C++
PPT
11 Inheritance.ppt
Inheritance
Inheritance
Inheritance
Inheritance in C++ (Programming Fundamentals)
EASY TO LEARN INHERITANCE IN C++
Inheritance OOP Concept in C++.
Chapter25 inheritance-i
Inheritance in c++
inheritance-16031525566nbhij56604452.pdf
session 24_Inheritance.ppt
Inheritance
00ps inheritace using c++
Inheritance in c++ part1
Inheritance (with classifications)
Inheritance in oops
TYPES OF INHERITANCE CONCEPT IN C++.pptx
Aryan's pres. entation.pptx
Inheritance in C++
11 Inheritance.ppt
Ad

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
PDF
Basic Mud Logging Guide for educational purpose
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
01-Introduction-to-Information-Management.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Sports Quiz easy sports quiz sports quiz
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study
Basic Mud Logging Guide for educational purpose
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
01-Introduction-to-Information-Management.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Sports Quiz easy sports quiz sports quiz
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
GDM (1) (1).pptx small presentation for students
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Ad

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

  • #7: 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.
  • #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: When we make a class as friend, all its member functions automatically become friend functions.
  • #16: When we make a class as friend, all its member functions automatically become friend functions.
  • #17: When we make a class as friend, all its member functions automatically become friend functions.
  • #18: When we make a class as friend, all its member functions automatically become friend functions.
  • #19: When we make a class as friend, all its member functions automatically become friend functions.
  • #20: When we make a class as friend, all its member functions automatically become friend functions.
  • #21: When we make a class as friend, all its member functions automatically become friend functions.
  • #22: When we make a class as friend, all its member functions automatically become friend functions.
  • #26: 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.
  • #27: 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.