Download presentation
Presentation is loading. Please wait.
Published byJesse Carpenter Modified over 9 years ago
1
Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected Member Data Base Class Access Specifications The Base Class Constructor Calls Overriding Member Functions
2
Inheritance (continue) Types of Inheritance More Examples of Simple Inheritance Examples of Multi-Level Inheritance Examples of Multiple Inheritance Homework and Lab Assignment
3
Concept of Inheritance What is inheritance? - to inherit properties/capabilities (member data and member functions) from an existing class or existing classes. - the existing class is called as a base class - the inherited class is called as a derived class - The two classes in the inheritance must have relationship in terms of code-reusable
4
Concept of Inheritance (continue) Why Inheritance? - code reusability (save time and money) - Increasing program reliability - Better problem-solving and program design - Supporting polymorphism
5
Concept of Inheritance (continue) Conceptual examples - example 1: circle base class: circle area = 3.1415*r*r derived class: sphere sphere area = 4 *circle::area volume = 4/3*circle::area*r Sphere is kind of circular shape r r
6
Concept of Inheritance (continue) - example 2: base class: employee members: ID,name, salary employee() show() derived class: manager members: manager is kind of employee office, bonus manager() computeBonus() show()
7
Simple Examples of Inheritance Example of inheriting a circle class to a sphere class - \CS116\YgaoHandouts\Ch9\circlein.cpp Example of inheritance - (p. 925-927) Textbook example
8
Base Classes and Derived Classes Characteristics of a base class - has all features of the objects in the category - is a general description of all objects in the category in terms of member data and member functions Characteristics of a derived class - has detailed and specific features of the objects in the class - usually has more capabilities than the base class in terms of member data and member functions Examples: - circlesphereemployeemanager
9
Private Member Data vs. Protected Member Data Define the member data as protected in a base class if the derived class can directly access the data as it own member data Define the member data as private in a base class if the derived class can only access the data via the member functions defined in the base class. Example: CS116/Ygao/Handouts /Ch9/circlein.cpp
10
Base Class Access Specifications
11
Base Class Access Specifications (continues)
12
The Base Class Constructor Calls Explicit base constructor calls i.e., shpere(double radius) : circle(radius) { } The base class’s constructor is called before the derived class’s constructor. The destructors are called in reverse order. i.e., (p.931-932) – textbook example
13
Overriding Member Functions The functions in a base class can be overridden in the derived classes Use the scope resolution operator :: to indicate the member functions called are from the base class. i.e., direct member function call area = 2*circle::Area() + Area(); the base member function call Must be used internally in the member functions of the derived class (p. 939-941) – textbook examples
14
Types of Inheritance Basically, there are three types of inheritance - simple inheritance: a base class derives on or many derived classes - multiple-level inheritance: a derived class can be served as a base class for next level of inheritance - multiple inheritance: there are more than one base class to derive one or many derived classess.
15
Types of Inheritance (continue) Example of simple inheritance
16
Types of Inheritance (continue) Example of multiple-level inheritance
17
Types of Inheritance (continue) Example of multiple inheritance
18
More Examples of Simple Inheritance /CS116/YGao/Handouts/Ch9/inherivd.cpp
19
Example of Multiple-level Inheritance /CS116/YGao/Handouts/Ch9/multilevel.cpp (p. 955-958 ) – textbook example
20
Example of Multiple Inheritance (p. 959 - 962) textbook example
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.