SlideShare a Scribd company logo
1
Chapter 8 - Inheritance
Outline
8.1 Introduction
8.2 Defining Derived Classes
8.3 Single Inheritance
8.4 Making Private Member Inhritable
8.5 Multiple Inheritance
8.6 Multiple Inheritance
8.7 Hierarchical Inheritance
8.8 Hybrid Inheritance
8.9 Virtual Base Classes
8.10 Abstract Classes
8.11 Constructors and Destructors in Derived Classes
2
8.1 Introduction
• Inheritance
– Software reusability
– The mechanism of deriving a new class from old one is called
inheritance
– OLD CLASS is called as BASE CLASS
– NEW CLASS is called as DERIVED CLASS or Subclass .
– Create new class from existing class
• Absorb existing class’s data and behaviors
• Enhance with new capabilities
– Derived class inherits from base class
• Derived class
– More specialized group of objects
– Behaviors inherited from base class
• Can customize
– Additional behaviors
3
8.1 Introduction
• Class hierarchy
– Direct base class
• Inherited explicitly (one level up hierarchy)
– Indirect base class
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one base class
– Multiple inheritance
• Inherits from multiple base classes
– Base classes possibly unrelated
• Chapter 22
4
8.1 Introduction
5
8.1 Defining Derived Class
• Three types of inheritance
– public
• Every object of derived class also object of base class
– Base-class objects not objects of derived classes
– Example: All cars vehicles, but not all vehicles cars
• Can access non-private members of base class
– Derived class can effect change to private base-class
members
• Through inherited non-private member functions
– private
• Alternative to composition
– Protected
6
“Protected” Access
• We have seen two access modes in C++ classes:
public and private
– Public members are directly accessible by users of the
class
– Private members are NOT directly accessible by users of
the class, not even by inheritors
• There is a 3rd
access mode: protected
– Protected members are directly accessible by derived
classes but not by other users
7
8.1 Defining Derived Class
• Abstraction
– Focus on commonalities among objects in system
• “is-a” vs. “has-a”
– “is-a”
• Inheritance
• Derived class object treated as base class object
• Example: Car is a vehicle
– Vehicle properties/behaviors also car properties/behaviors
– “has-a”
• Composition
• Object contains one or more objects of other classes as
members
• Example: Car has a steering wheel
8
8.2 Defining Derived Classes
• Base classes and derived classes
– Object of one class “is an” object of another class
• Example: Rectangle is quadrilateral.
– Class Rectangle inherits from class Quadrilateral
– Quadrilateral: base class
– Rectangle: derived class
– Base class typically represents larger set of objects than
derived classes
• Example:
– Base class: Vehicle
• Cars, trucks, boats, bicycles, …
– Derived class: Car
• Smaller, more-specific subset of vehicles
9
Defining Derived Classes
• Inheritance examples
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
10
8.2 Defining Derived Classes
• Inheritance hierarchy
– Inheritance relationships: tree-like hierarchy structure
– Each class becomes
• Base class
– Supply data/behaviors to other classes
OR
• Derived class
– Inherit data/behaviors from other classes
11
Shape
TwoDimensionalShape ThreeDimensionalShape
Circle Square Triangle Sphere Cube Tetrahedron
Inheritance hierarchy for Shapes.
12
8.2 Defining Derived Classes
13
8.3 Single Inheritance
• Single Inheritance Public Example
14
8.3 Single Inheritance
• Single Inheritance Public Example
15
8.3 Single Inheritance
• Single Inheritance Private Example
16
8.3 Single Inheritance
• Single Inheritance Private Example
17
8.4 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
• 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 publicand protected members of the base
class.
• 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
18
8.4 Making a Private Member Inheritable
19
20
8.4 Making a Private Member Inheritable
21
8.5 Multilevel Inheritance
22
8.5 Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class top //base class
{
public :
int a;
void getdata()
{
cout<<"nnEnter first Number :::t";
cin>>a;
}
void putdata()
{
cout<<"nFirst Number Is :::t"<<a;
}
};
//First level inheritance
class middle :public top // class middle is derived_1
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"nnSquare Is :::"<<b;
}
};
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"nnCube :::t"<<c;
}
};
int main()
23
8.6 Multiple Inheritance
24
8.6 Multiple Inheritance
• Example
25
8.7 Hierarchical Inheritance
26
8.8 Hybrid Inheritance
27
8.9 Virtual Base Class
28
8.10 Abstract Class
• Base Class to be inherited by other classes
• No objects of base class
• Example:
29
8.11 Constructors in Derived Classes
• Instantiating derived-class object
– Chain of constructor calls
• Derived-class constructor invokes base class constructor
– Implicitly or explicitly
• Base of inheritance hierarchy
– Last constructor called in chain
– First constructor body to finish executing
• Initializing data members
– Each base-class constructor initializes data members
• Inherited by derived class
30
8.11 Constructors in Derived Classes
31
8.11 Constructors and Destructors in
Derived Classes
• Destroying derived-class object
– Chain of destructor calls
• Reverse order of constructor chain
• Destructor of derived-class called first
• Destructor of next base class up hierarchy next
– Continue up hierarchy until final base reached
• After final base-class destructor, object removed from
memory
32
8.11 Destructors in Derived Classes
• Base-class constructors, destructors, assignment
operators
– Not inherited by derived classes
– Derived class constructors, assignment operators can call
• Constructors
• Assignment operators

More Related Content

PPT
10.Inheritance.ppt for oops programinggg
PPTX
Inheritance in c++
PPT
PPT
Lecturespecial
PPT
Overview of Object Oriented Programming using C++
PPTX
Object Oriented Design and Programming Unit-03
PPT
week14 (1).ppt
PPT
Inheritance in C++
10.Inheritance.ppt for oops programinggg
Inheritance in c++
Lecturespecial
Overview of Object Oriented Programming using C++
Object Oriented Design and Programming Unit-03
week14 (1).ppt
Inheritance in C++

Similar to inheritance final ppvjjfjjdrtdyyrtuytyouijkt.ppt (20)

PPT
session 24_Inheritance.ppt
PPTX
inheritance
PDF
lecture 6.pdf
PPTX
Inheritance
PPTX
Inheritance
PPTX
VB.net&OOP.pptx
PPTX
Inheritance
PPTX
INHERITANCE.pptx
PPTX
simple notes Unit 4-Inheritance (2).pptx
PPTX
INHERITANCES.pptx
PPTX
Inheritance in c++
PPTX
[OOP - Lec 20,21] Inheritance
PDF
lecture-2021inheritance-160705095417.pdf
PPT
Inheritance, Object Oriented Programming
PPTX
PPTX
29csharp
PPTX
PPT
Lecture 08-inheritance-i
PPTX
Inheritance.pptx
PPTX
session 24_Inheritance.ppt
inheritance
lecture 6.pdf
Inheritance
Inheritance
VB.net&OOP.pptx
Inheritance
INHERITANCE.pptx
simple notes Unit 4-Inheritance (2).pptx
INHERITANCES.pptx
Inheritance in c++
[OOP - Lec 20,21] Inheritance
lecture-2021inheritance-160705095417.pdf
Inheritance, Object Oriented Programming
29csharp
Lecture 08-inheritance-i
Inheritance.pptx
Ad

Recently uploaded (20)

PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
PDF
composite construction of structures.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
573137875-Attendance-Management-System-original
Lesson 3_Tessellation.pptx finite Mathematics
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Foundation to blockchain - A guide to Blockchain Tech
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Internet of Things (IOT) - A guide to understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Sustainable Sites - Green Building Construction
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing
composite construction of structures.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Embodied AI: Ushering in the Next Era of Intelligent Systems
Model Code of Practice - Construction Work - 21102022 .pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
573137875-Attendance-Management-System-original
Ad

inheritance final ppvjjfjjdrtdyyrtuytyouijkt.ppt

  • 1. 1 Chapter 8 - Inheritance Outline 8.1 Introduction 8.2 Defining Derived Classes 8.3 Single Inheritance 8.4 Making Private Member Inhritable 8.5 Multiple Inheritance 8.6 Multiple Inheritance 8.7 Hierarchical Inheritance 8.8 Hybrid Inheritance 8.9 Virtual Base Classes 8.10 Abstract Classes 8.11 Constructors and Destructors in Derived Classes
  • 2. 2 8.1 Introduction • Inheritance – Software reusability – The mechanism of deriving a new class from old one is called inheritance – OLD CLASS is called as BASE CLASS – NEW CLASS is called as DERIVED CLASS or Subclass . – Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities – Derived class inherits from base class • Derived class – More specialized group of objects – Behaviors inherited from base class • Can customize – Additional behaviors
  • 3. 3 8.1 Introduction • Class hierarchy – Direct base class • Inherited explicitly (one level up hierarchy) – Indirect base class • Inherited two or more levels up hierarchy – Single inheritance • Inherits from one base class – Multiple inheritance • Inherits from multiple base classes – Base classes possibly unrelated • Chapter 22
  • 5. 5 8.1 Defining Derived Class • Three types of inheritance – public • Every object of derived class also object of base class – Base-class objects not objects of derived classes – Example: All cars vehicles, but not all vehicles cars • Can access non-private members of base class – Derived class can effect change to private base-class members • Through inherited non-private member functions – private • Alternative to composition – Protected
  • 6. 6 “Protected” Access • We have seen two access modes in C++ classes: public and private – Public members are directly accessible by users of the class – Private members are NOT directly accessible by users of the class, not even by inheritors • There is a 3rd access mode: protected – Protected members are directly accessible by derived classes but not by other users
  • 7. 7 8.1 Defining Derived Class • Abstraction – Focus on commonalities among objects in system • “is-a” vs. “has-a” – “is-a” • Inheritance • Derived class object treated as base class object • Example: Car is a vehicle – Vehicle properties/behaviors also car properties/behaviors – “has-a” • Composition • Object contains one or more objects of other classes as members • Example: Car has a steering wheel
  • 8. 8 8.2 Defining Derived Classes • Base classes and derived classes – Object of one class “is an” object of another class • Example: Rectangle is quadrilateral. – Class Rectangle inherits from class Quadrilateral – Quadrilateral: base class – Rectangle: derived class – Base class typically represents larger set of objects than derived classes • Example: – Base class: Vehicle • Cars, trucks, boats, bicycles, … – Derived class: Car • Smaller, more-specific subset of vehicles
  • 9. 9 Defining Derived Classes • Inheritance examples Base class Derived classes Student GraduateStudent UndergraduateStudent Shape Circle Triangle Rectangle Loan CarLoan HomeImprovementLoan MortgageLoan Employee FacultyMember StaffMember Account CheckingAccount SavingsAccount
  • 10. 10 8.2 Defining Derived Classes • Inheritance hierarchy – Inheritance relationships: tree-like hierarchy structure – Each class becomes • Base class – Supply data/behaviors to other classes OR • Derived class – Inherit data/behaviors from other classes
  • 11. 11 Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron Inheritance hierarchy for Shapes.
  • 13. 13 8.3 Single Inheritance • Single Inheritance Public Example
  • 14. 14 8.3 Single Inheritance • Single Inheritance Public Example
  • 15. 15 8.3 Single Inheritance • Single Inheritance Private Example
  • 16. 16 8.3 Single Inheritance • Single Inheritance Private Example
  • 17. 17 8.4 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 • 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 publicand protected members of the base class.
  • 18. • 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 18
  • 19. 8.4 Making a Private Member Inheritable 19
  • 20. 20 8.4 Making a Private Member Inheritable
  • 22. 22 8.5 Multilevel Inheritance #include<iostream.h> #include<conio.h> class top //base class { public : int a; void getdata() { cout<<"nnEnter first Number :::t"; cin>>a; } void putdata() { cout<<"nFirst Number Is :::t"<<a; } }; //First level inheritance class middle :public top // class middle is derived_1 { public: int b; void square() { getdata(); b=a*a; cout<<"nnSquare Is :::"<<b; } }; //Second level inheritance class bottom :public middle // class bottom is derived_2 { public: int c; void cube() { square(); c=b*a; cout<<"nnCube :::t"<<c; } }; int main()
  • 28. 28 8.10 Abstract Class • Base Class to be inherited by other classes • No objects of base class • Example:
  • 29. 29 8.11 Constructors in Derived Classes • Instantiating derived-class object – Chain of constructor calls • Derived-class constructor invokes base class constructor – Implicitly or explicitly • Base of inheritance hierarchy – Last constructor called in chain – First constructor body to finish executing • Initializing data members – Each base-class constructor initializes data members • Inherited by derived class
  • 30. 30 8.11 Constructors in Derived Classes
  • 31. 31 8.11 Constructors and Destructors in Derived Classes • Destroying derived-class object – Chain of destructor calls • Reverse order of constructor chain • Destructor of derived-class called first • Destructor of next base class up hierarchy next – Continue up hierarchy until final base reached • After final base-class destructor, object removed from memory
  • 32. 32 8.11 Destructors in Derived Classes • Base-class constructors, destructors, assignment operators – Not inherited by derived classes – Derived class constructors, assignment operators can call • Constructors • Assignment operators