oop: inheritance in c++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf ·...

35
OOP: Inheritance in C++

Upload: others

Post on 27-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

OOP: Inheritance in C++

Page 2: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Contents

• Concept of Inheritance

• Relationship Between Base Class and Derived Class

• Constructor and Destructor in Derived Classes

• Public, Protected and Private Inheritance

Page 3: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Inheritance

• Inheritance is a form of software reuse in which we create a class that absorbs an existing class’s data and behaviors and enhances them with new capabilities.

• This existing class is called the base class, and the new class is referred to as the derived class.

• A direct base class is the base class from which a derived class explicitly inherits.

• An indirect base class is inherited from two or more levels up in the class hierarchy.

• Single inheritance: derived from one base class.

• Multiple inheritance: derived from multiple base classes.

Page 4: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Inheritance

• With public inheritance, every object of a derived class is also an object of that derived class’s base class. However, base-class objects are not objects of their derived classes.

• Example: Let us assume vehicle as a base class and car as a derived class, then all cars are vehicles, but not all vehicles are cars.

is-a relationship vs. has-a relationship

CompositionInheritance

Page 5: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Inheritance Examples

Page 6: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Inheritance hierarchy for university CommunityMembers.

Page 7: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Inheritance hierarchy for Shapes

• Public members of the base class become public members of the derived class.

• friend functions are not inherited

Page 8: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

A simple example of inheritance

Page 9: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Protected Members

• A base class’s public members are accessible within its body and anywhere that the program has a handle to an object of that class or one of its derived classes.

• A base class’s private members are accessible only within its body and to the friends of that base class.

• A base class’s protected members can be accessed within the body of that baseclass, by members and friends of that base class, and by members and friends of any classes derived from that base class.

Page 10: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Relationship Between

Base Class and Derived Class

Page 11: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Commission Employee Class

Page 12: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 13: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 14: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 15: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 16: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

For Source Code: Practice1

Page 17: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

Bas

ePlu

sCo

mm

issi

on

Emp

loye

eC

lass

Page 18: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 19: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 20: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 21: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 22: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 23: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 24: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

For Source Code: Practice2

CPM: Copied, Paste, and Modify

Page 25: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

BasePlusCommissionEmployee using Inheritance

Page 26: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 27: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 28: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

BasePlusCommissionEmployeeusing Inheritance with Protected Data

Page 29: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 30: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 31: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 32: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 33: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 34: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that
Page 35: OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf · Inheritance • Inheritance is a form of software reuse in which we create a class that

For Source code Practice3