inheritance concept of inheritance (what, why, and how) simple example of inheritance base classes...

20
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

Upload: jesse-carpenter

Post on 18-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 2: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Inheritance (continue)

• Types of Inheritance

• More Examples of Simple Inheritance

• Examples of Multi-Level Inheritance

• Examples of Multiple Inheritance

• Homework and Lab Assignment

Page 3: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 4: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Concept of Inheritance (continue)

• Why Inheritance?

- code reusability (save time and money)

- Increasing program reliability

- Better problem-solving and program design

- Supporting polymorphism

Page 5: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 6: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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()

Page 7: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 8: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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:

- circle sphere employee manager

Page 9: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 10: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Base Class Access Specifications

Base ClassAccessSpecification

How Members of the Base ClassAppear in the Derived Class

p riv a te •Private members of the base class areinaccessible to the derived class.

•Protected members of the base classbecome private members of thederived class.

•Public members of the base classbecome private members the derivedclass.

Page 11: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Base Class Access Specifications(continues)

Base ClassAccessSpecification

How Members of the Base ClassAppear in the Derived Class

p ro tec ted •Private members of the base class areinaccessible to the derived class.

•Protected members of the base classbecome protected members of thederived class.

•Public members of the base classbecome protected members of thederived class.

Page 12: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 13: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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

Page 14: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

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.

Page 15: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Types of Inheritance (continue)

• Example of simple inheritance

Page 16: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Types of Inheritance (continue)

• Example of multiple-level inheritance

Page 17: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Types of Inheritance (continue)

• Example of multiple inheritance

Page 18: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

More Examples of Simple Inheritance

• /CS116/YGao/Handouts/Ch9/inherivd.cpp

Page 19: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Example of Multiple-level Inheritance

• /CS116/YGao/Handouts/Ch9/multilevel.cpp

• (p. 955-958 ) – textbook example

Page 20: Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected

Example of Multiple Inheritance

• (p. 959 - 962) textbook example