OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf ·...
Transcript of OOP: Inheritance in C++lecture.riazulislam.com/uploads/3/9/8/5/3985970/cpp_slides_13.pdf ·...
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/12.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/13.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/14.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/15.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/18.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/19.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/20.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/21.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/22.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/23.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/26.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/27.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/29.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/30.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/31.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/32.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/33.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/34.jpg)
![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](https://reader034.fdocuments.net/reader034/viewer/2022043000/5f77b0df11cb343d1d53052a/html5/thumbnails/35.jpg)
For Source code Practice3