SlideShare a Scribd company logo
Object Oriented Programming
(CS1143)
Department of Computer Science
Capital University of Science and Technology (CUST)
Week 7
Outline
 Single Public, Private, Protected Inheritance
 Constructor and Destructor Chaining
2
Inheritance
 Inheritance enables you to define a general class (i.e., a base class)
and later extend it to more specialized classes (i.e., derived classes).
 For example we can give the definition of an animal; we can then add
to the definition to create the definition of a horse.
 There is an is-a relation from the more specific to the more general.
 All horses are animals.
3
4
Base Class and Derived Class
 In C++, the most general class is called the base class (or super class)
and a more specific class is called the derived class (or subclass).
 The derived class inherits all of the data members and member
functions of the base class (with the exception of constructors and
destructors), and it can create new data members and member
functions.
5
Private, Public and Protected Inheritance
 To create a derived class from a base class, we have three choices in
C++: private inheritance, protected inheritance, and public
inheritance.
 To show the type of the inheritance we want to use, we insert a colon
after the class, followed by one of the keywords (private protected,
or public)
 The most common is the public inheritance.
 In the figure below, B is the base class and D is the derived class.
6
Public Inheritance
 The most common type of inheritance is public inheritance. The
other two types of inheritance are rarely used.
 Some other object-oriented languages, like Java, have only public
inheritance
 We will discuss it using an example
7
Example
8
Example
 We design two classes, Person and Student
 The class Student inherits from the class Person.
 Person class uses only one data member: identity.
 Student class needs two data members: identity and gpa.
 However, since the identity data member is already defined in the
class Person, it does not need to be defined in the class Student
because of inheritance.
 Similarly the Student class does not need to define its own set and
get functions for identity.
9
10
Person Class
11
Student Class
main
12
13
Output
Description
 On line 54, an object of the student class calls the member function
of its base class (Person) as if it were its own function
14
Private Members in Public Inheritance
 A private data member in the base class is inherited in the derived
class, but it becomes inaccessible in the derived class; it must be
accessed only through its own class member functions.
15
Public and Protected Members in Public
Inheritance
 A public member in the base class becomes a public member in the
derived class.
 A protected member in the base class becomes a protected member
in the derived class.
 The functions defined in the derived class can easily access a
protected member without having to call a function inherited from
the base class
16
17
18
Week3. Slide 6.
Three Types of Inheritance
 Although public inheritance is by far the most common type of
derivation, C++ allows us to use two other types of derivation: private
and protected.
19
Outline
 Single Public, Private, Protected Inheritance
 Constructor and Destructor Chaining
20
Constructors and Destructors
 Unlike data fields and functions, the constructors and the destructor
of a base class are not inherited in the derived class.
 The constructors and the destructor are not inherited because an
object of a derived class naturally has more data members than a
corresponding base class.
 The constructor of a derived class must construct more; the destructor of a
derived class must destruct more.
21
Calling Base Class’s Constructors
 You can invoke the base class’s constructor from the constructor
initializer list of the derived class.
 If a base constructor is not invoked explicitly, the base class’s no-
argument constructor (default constructor) is invoked by default.
22
Constructor Chaining
 When constructing an object of a derived class, the derived class
constructor first invokes its base class constructor before performing
its own tasks.
 This is called constructor chaining.
23
Destructor Chaining
 The destructors are automatically invoked in reverse order.
 When an object of a derived class is destroyed, the derived class
destructor is called. After it finishes its tasks, it invokes its base class
destructor.
 This is called destructor chaining.
24
Example
Calling base class constructor implicitly
25
26
27
28
Example
Calling base class constructor explicitly
29
30
31
32

More Related Content

Similar to C++ programming public, private inheritance (20)

PPT
inheritance
Amir_Mukhtar
 
PPTX
inheritance
krishna partiwala
 
PPTX
Inheritance
Munsif Ullah
 
PPTX
C++ Inheritance.pptx
XanGwaps
 
PPT
Lab3
karan saini
 
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
PPT
Inheritance
Aadhi Aadhithya
 
PDF
L9
lksoo
 
PPTX
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Abu Saleh
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
PPTX
Inheritance
pooja_doshi
 
PDF
Inheritance
Pranali Chaudhari
 
PPT
11 Inheritance.ppt
LadallaRajKumar
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PDF
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
PDF
Inheritance
Prof. Dr. K. Adisesha
 
PPT
Inheritance in C++
RAJ KUMAR
 
PPTX
Inheritance slides
awaisch888
 
PPTX
Introduction to Inheritance
Keshav Vaswani
 
inheritance
Amir_Mukhtar
 
inheritance
krishna partiwala
 
Inheritance
Munsif Ullah
 
C++ Inheritance.pptx
XanGwaps
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
Inheritance
Aadhi Aadhithya
 
L9
lksoo
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Abu Saleh
 
Inheritance in c++
Vineeta Garg
 
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
Inheritance
pooja_doshi
 
Inheritance
Pranali Chaudhari
 
11 Inheritance.ppt
LadallaRajKumar
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
lecture-2021inheritance-160705095417.pdf
AneesAbbasi14
 
Inheritance in C++
RAJ KUMAR
 
Inheritance slides
awaisch888
 
Introduction to Inheritance
Keshav Vaswani
 

Recently uploaded (20)

PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Ad

C++ programming public, private inheritance

  • 1. Object Oriented Programming (CS1143) Department of Computer Science Capital University of Science and Technology (CUST) Week 7
  • 2. Outline  Single Public, Private, Protected Inheritance  Constructor and Destructor Chaining 2
  • 3. Inheritance  Inheritance enables you to define a general class (i.e., a base class) and later extend it to more specialized classes (i.e., derived classes).  For example we can give the definition of an animal; we can then add to the definition to create the definition of a horse.  There is an is-a relation from the more specific to the more general.  All horses are animals. 3
  • 4. 4
  • 5. Base Class and Derived Class  In C++, the most general class is called the base class (or super class) and a more specific class is called the derived class (or subclass).  The derived class inherits all of the data members and member functions of the base class (with the exception of constructors and destructors), and it can create new data members and member functions. 5
  • 6. Private, Public and Protected Inheritance  To create a derived class from a base class, we have three choices in C++: private inheritance, protected inheritance, and public inheritance.  To show the type of the inheritance we want to use, we insert a colon after the class, followed by one of the keywords (private protected, or public)  The most common is the public inheritance.  In the figure below, B is the base class and D is the derived class. 6
  • 7. Public Inheritance  The most common type of inheritance is public inheritance. The other two types of inheritance are rarely used.  Some other object-oriented languages, like Java, have only public inheritance  We will discuss it using an example 7
  • 9. Example  We design two classes, Person and Student  The class Student inherits from the class Person.  Person class uses only one data member: identity.  Student class needs two data members: identity and gpa.  However, since the identity data member is already defined in the class Person, it does not need to be defined in the class Student because of inheritance.  Similarly the Student class does not need to define its own set and get functions for identity. 9
  • 14. Description  On line 54, an object of the student class calls the member function of its base class (Person) as if it were its own function 14
  • 15. Private Members in Public Inheritance  A private data member in the base class is inherited in the derived class, but it becomes inaccessible in the derived class; it must be accessed only through its own class member functions. 15
  • 16. Public and Protected Members in Public Inheritance  A public member in the base class becomes a public member in the derived class.  A protected member in the base class becomes a protected member in the derived class.  The functions defined in the derived class can easily access a protected member without having to call a function inherited from the base class 16
  • 17. 17
  • 19. Three Types of Inheritance  Although public inheritance is by far the most common type of derivation, C++ allows us to use two other types of derivation: private and protected. 19
  • 20. Outline  Single Public, Private, Protected Inheritance  Constructor and Destructor Chaining 20
  • 21. Constructors and Destructors  Unlike data fields and functions, the constructors and the destructor of a base class are not inherited in the derived class.  The constructors and the destructor are not inherited because an object of a derived class naturally has more data members than a corresponding base class.  The constructor of a derived class must construct more; the destructor of a derived class must destruct more. 21
  • 22. Calling Base Class’s Constructors  You can invoke the base class’s constructor from the constructor initializer list of the derived class.  If a base constructor is not invoked explicitly, the base class’s no- argument constructor (default constructor) is invoked by default. 22
  • 23. Constructor Chaining  When constructing an object of a derived class, the derived class constructor first invokes its base class constructor before performing its own tasks.  This is called constructor chaining. 23
  • 24. Destructor Chaining  The destructors are automatically invoked in reverse order.  When an object of a derived class is destroyed, the derived class destructor is called. After it finishes its tasks, it invokes its base class destructor.  This is called destructor chaining. 24
  • 25. Example Calling base class constructor implicitly 25
  • 26. 26
  • 27. 27
  • 28. 28
  • 29. Example Calling base class constructor explicitly 29
  • 30. 30
  • 31. 31
  • 32. 32