chapter 15 – inheritance, virtual functions, and polymorphism

21
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Upload: noah-griffith

Post on 26-Dec-2015

235 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Page 2: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Inheritance

Name of another way in which classes and objects relate– Base class– Derived class

When object of derived class declared– Object of base class is automatically created

and contained within derived class object

Lesson 15.1

Page 3: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Image of base class object creation when derived class object instantiated

Lesson 15.1

Derived class object

Base class object

Page 4: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Defining a Base Class

Lesson 15.1

class Base { private: type base_priv_dat; protected: type base_prot_dat; public: type base_function_name ( ); };

Page 5: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Defining a Derived Class

Lesson 15.1

class Derived: public Base { private: int derived_priv_dat; public: void derived_function_name ( ); };

Page 6: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Interaction Between Base and Derived Class Objects

Private base class members are private member of base class only

Protected base class members are protected members of both derived and base classes

Public base class members are public members of derived and base classes

Derived class members convey no special access privileges to base class functions

Lesson 15.1

Page 7: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Relationship Between Base and Derived Objects

Derived object Base object

private dataprivate data

protected dataprotected data

public functionspublic functions

private dataprivate data

public functionspublic functions

derived_object.derived_function( )

derived_object.base_function( )

Page 8: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

No Base Class Object Name

Use only derived object name to access both derived and base class functions

Derived class takes precedence– over-riding base class function

Lesson 15.1

Page 9: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Comparison of Types of Class and Object Interactions

Friend classobject

Granting class object

Client classobject

Server classobject

Componentclass object

Composite ClassObject

Derived class object Base

class objectLesson 15.1

On all examples, upper boxes representprivate data members and lower boxesrepresent public member functions.

Page 10: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Private and Protected Inheritance

class Derived : private Base– public and protected members of Base become

private members of Derived– private members of Base remain private to Base

class Derived : protected Base– public and protected members of Base become

public and protected member of Derived– private members of Base remain private to Base

Lesson 15.1

Page 11: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Order of Constructor and Destructor Function Calls

Both base and derived constructors called when derived class object instantiated

Bass class constructor called first, derived constructor second

Derived class destructor called first, base class destructor second

Order in which objects declared determines order of constructor calls

Lesson 15.2

Page 12: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Other Uses of Inheritance

Well suited to extending or customizing existing code

Can make full use of base class features and add specific features

Can modify base classes without requiring derived classes to be modified– Allows base classes to remain up to date

Lesson 15.1

Page 13: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Explicit Call to Base Class Constructor

Lesson 15.2

Derived object (value1, value2);

Derived :: Derived (type derived_var, type base_var)

:derived_member (derived_var), Base (base_var)

Call

Constructor

Page 14: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Effect of Inheritance Levels Object for lowest level class contains sub-

objects of all other classes Order of calling constructors is from top

down in inheritance graph Pass data one step or level at a time Functions can be overridden in any class

down inheritance graph Can assign derived class objects to base

class objects, but not reverse

Lesson 15.3

Page 15: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Effect of Inheritance Levels

Object for lowest level class contains sub-objects of all other classes

Lesson 15.3 Earth object

Planet object(no name)

Celestial_bodyobject

(no name)

Page 16: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Multiple Inheritance

Class can inherit from more than one class directly– When one class has "is a" relationship with

more than one class

Lesson 15.4

Page 17: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Virtual Classes

Class which we are not allowed to create independent objects

Used as base class for other classes for which we do want independent objects

C++ automatically makes class virtual when member of class is pure virtual function

Lesson 15.5

virtual type function (type, type) = 0;

Page 18: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Pointers and Inheritance

Can declare base class type pointer variable and assign address of derived class object

Any address of object down inheritance graph

Lesson 15.5

Page 19: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Binding

Association of function call with a function Early binding

– Occurs during compilation– Also called static or compile-time binding

Late binding– Occurs during execution– Also called dynamic or run-time binding

Lesson 15.5

Page 20: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Polymorphism

Achieved by creating and using virtual functions

Code making function call does not explicitly state which function it is calling– C++ decides during execution which is correct

function

Page 21: Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Summary

Create an inheritance hierarchy Use inheritance as a form of class

interaction Make a virtual class Use polymorphism in an engineering

program

Learned how to: