0% found this document useful (0 votes)
29 views

Lecture 7-Week7 - Single Public and Private Inheritance-Constructor Chaining

The document discusses object-oriented programming concepts including inheritance, base and derived classes, and the three types of inheritance in C++. It provides examples of defining classes Person and Student to demonstrate public inheritance and how constructors and destructors are handled during object creation and destruction.

Uploaded by

Nnoor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lecture 7-Week7 - Single Public and Private Inheritance-Constructor Chaining

The document discusses object-oriented programming concepts including inheritance, base and derived classes, and the three types of inheritance in C++. It provides examples of defining classes Person and Student to demonstrate public inheritance and how constructors and destructors are handled during object creation and destruction.

Uploaded by

Nnoor
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Object Oriented Programming

(CS1143)
Week 7

Department of Computer Science


Capital University of Science and Technology (CUST)
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
Person Class

10
Student Class

11
main

12
Output

13
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’s 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
Week3. Slide 6.

18
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

You might also like