 2006 Pearson Education, Inc. All rights reserved. 1 13 Object-Oriented Programming: Polymorphism.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 2006 Pearson Education, Inc. All rights reserved Templates.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Cpt S 122 – Data Structures Polymorphism
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Object-Oriented Programming Polymorphism Session 07 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Polymorphism Chapter 10.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism

 2006 Pearson Education, Inc. All rights reserved. Web Sites

 2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  What polymorphism is, how it makes programming more convenient, and how it makes systems more extensible and maintainable.  To declare and use virtual functions to effect polymorphism.  The distinction between abstract and concrete classes.  To declare pure virtual functions to create abstract classes.

 2006 Pearson Education, Inc. All rights reserved Introduction 13.2 Polymorphism Examples 13.3 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived- Class Objects Aiming Derived-Class Pointers at Base-Class Objects Derived-Class Member-Function Calls via Base- Class Pointers Virtual Functions

 2006 Pearson Education, Inc. All rights reserved Introduction Polymorphism with inheritance hierarchies – “Program in the general” vs. “program in the specific” – Process objects of classes that are part of the same hierarchy as if they are all objects of the base class – Each object performs the correct tasks for that object’s type Different actions occur depending on the type of object – New classes can be added with little or not modification to existing code

 2006 Pearson Education, Inc. All rights reserved Introduction (Cont.) Example: Animal hierarchy – Animal base class – every derived class has function move – Different animal objects maintained as a vector of Animal pointers – Program issues same message ( move ) to each animal generically – Proper function gets called A Fish will move by swimming A Frog will move by jumping A Bird will move by flying

 2006 Pearson Education, Inc. All rights reserved Polymorphism Examples Polymorphism occurs when a program invokes a virtual function through a base-class pointer or reference – C++ dynamically chooses the correct function for the class from which the object was instantiated Example: SpaceObject s – Video game manipulates objects of types that inherit from SpaceObject, which contains member function draw – Function draw implemented differently for the different classes – Screen-manager program maintains a container of SpaceObject pointers – Call draw on each object using SpaceObject pointers Proper draw function is called based on object’s type – A new class derived from SpaceObject can be added without affecting the screen manager

 2006 Pearson Education, Inc. All rights reserved Relationships Among Objects in an Inheritance Hierarchy Demonstration – Invoking base-class functions from derived-class objects – Aiming derived-class pointers at base-class objects – Derived-class member-function calls via base-class pointers – Demonstrating polymorphism using virtual functions Base-class pointers aimed at derived-class objects Key concept – An object of a derived class can be treated as an object of its base class

 2006 Pearson Education, Inc. All rights reserved Invoking Base-Class Functions from Derived-Class Objects Aim base-class pointer at base-class object – Invoke base-class functionality Aim derived-class pointer at derived-class object – Invoke derived-class functionality Aim base-class pointer at derived-class object – Because derived-class object is an object of base class – Invoke base-class functionality Invoked functionality depends on type of the handle used to invoke the function, not type of the object to which the handle points – virtual functions Make it possible to invoke the object type’s functionality, rather than invoke the handle type’s functionality Crucial to implementing polymorphic behavior

 2006 Pearson Education, Inc. All rights reserved. 10 Outline Commission Employee.h (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 11 Outline Commission Employee.h (2 of 2) Function print will be redefined in derived class to print the employee’s information Function earnings will be redefined in derived classes to calculate the employee’s earnings

 2006 Pearson Education, Inc. All rights reserved. 12 Outline Commission Employee.cpp (1 of 4)

 2006 Pearson Education, Inc. All rights reserved. 13 Outline Commission Employee.cpp (2 of 4)

 2006 Pearson Education, Inc. All rights reserved. 14 Outline Commission Employee.cpp (3 of 4) Calculate earnings based on commission rate and gross sales

 2006 Pearson Education, Inc. All rights reserved. 15 Outline Commission Employee.cpp (4 of 4) Display name, social security number, gross sales and commission rate

 2006 Pearson Education, Inc. All rights reserved. 16 Outline BasePlus Commission Employee.h (1 of 1) Redefine functions earnings and print

 2006 Pearson Education, Inc. All rights reserved. 17 Outline BasePlus Commission Employee.cpp (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 18 Outline BasePlus Commission Employee.cpp (2 of 2) Redefined earnings function incorporates base salary Redefined print function displays additional BasePlusCommissionEmployee details

 2006 Pearson Education, Inc. All rights reserved. 19 Outline fig13_05.cpp (1 of 5)

 2006 Pearson Education, Inc. All rights reserved. 20 Outline fig13_05.cpp (2 of 5) Aiming base-class pointer at base-class object and invoking base-class functionality

 2006 Pearson Education, Inc. All rights reserved. 21 Outline fig13_05.cpp (3 of 5) Aiming derived-class pointer at derived-class object and invoking derived-class functionality Aiming base-class pointer at derived-class object and invoking base-class functionality

 2006 Pearson Education, Inc. All rights reserved. 22 Outline fig13_05.cpp (4 of 5)

 2006 Pearson Education, Inc. All rights reserved. 23 Outline fig13_05.cpp (5 of 5)

 2006 Pearson Education, Inc. All rights reserved Aiming Derived-Class Pointers at Base-Class Objects Aim a derived-class pointer at a base-class object – C++ compiler generates error CommissionEmployee (base-class object) is not a BasePlusCommissionEmployee (derived-class object) – If this were to be allowed, programmer could then attempt to access derived-class members which do not exist Could modify memory being used for other data

 2006 Pearson Education, Inc. All rights reserved. 25 Outline fig13_06.cpp (1 of 2) Cannot assign base-class object to derived-class pointer because is-a relationship does not apply

 2006 Pearson Education, Inc. All rights reserved. 26 Outline fig13_06.cpp (2 of 2)

 2006 Pearson Education, Inc. All rights reserved Derived-Class Member-Function Calls via Base-Class Pointers Aiming base-class pointer at derived-class object – Calling functions that exist in base class causes base-class functionality to be invoked – Calling functions that do not exist in base class (may exist in derived class) will result in error Derived-class members cannot be accessed from base-class pointers However, they can be accomplished using downcasting (Section 13.8)

 2006 Pearson Education, Inc. All rights reserved. 28 Outline fig13_07.cpp (1 of 2) Cannot invoke derived-class-only members from base-class pointer

 2006 Pearson Education, Inc. All rights reserved. 29 Outline fig13_07.cpp (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 30 Software Engineering Observation 13.3 If the address of a derived-class object has been assigned to a pointer of one of its direct or indirect base classes, it is acceptable to cast that base-class pointer back to a pointer of the derived-class type. In fact, this must be done to send that derived- class object messages that do not appear in the base class.

 2006 Pearson Education, Inc. All rights reserved Virtual Functions Which class’s function to invoke – Normally Handle determines which class’s functionality to invoke – With virtual functions Type of the object being pointed to, not type of the handle, determines which version of a virtual function to invoke Allows program to dynamically (at runtime rather than compile time) determine which function to use – Called dynamic binding or late binding

 2006 Pearson Education, Inc. All rights reserved Virtual Functions (Cont.) virtual functions – Declared by preceding the function’s prototype with the keyword virtual in base class – Derived classes override function as appropriate – Once declared virtual, a function remains virtual all the way down the hierarchy – Static binding When calling a virtual function using specific object with dot operator, function invocation resolved at compile time – Dynamic binding Dynamic binding occurs only off pointer and reference handles

 2006 Pearson Education, Inc. All rights reserved. 33 Software Engineering Observation 13.4 Once a function is declared virtual, it remains virtual all the way down the inheritance hierarchy from that point, even if that function is not explicitly declared virtual when a class overrides it.

 2006 Pearson Education, Inc. All rights reserved. 34 Good Programming Practice 13.1 Even though certain functions are implicitly virtual because of a declaration made higher in the class hierarchy, explicitly declare these functions virtual at every level of the hierarchy to promote program clarity.

 2006 Pearson Education, Inc. All rights reserved. 35 Error-Prevention Tip 13.1 When a programmer browses a class hierarchy to locate a class to reuse, it is possible that a function in that class will exhibit virtual function behavior even though it is not explicitly declared virtual. This happens when the class inherits a virtual function from its base class, and it can lead to subtle logic errors. Such errors can be avoided by explicitly declaring all virtual functions virtual throughout the inheritance hierarchy.

 2006 Pearson Education, Inc. All rights reserved. 36 Software Engineering Observation 13.5 When a derived class chooses not to override a virtual function from its base class, the derived class simply inherits its base class’s virtual function implementation.

 2006 Pearson Education, Inc. All rights reserved. 37 Outline Commission Employee.h (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 38 Outline Commission Employee.h (2 of 2) Declaring earnings and print as virtual allows them to be overridden, not redefined

 2006 Pearson Education, Inc. All rights reserved. 39 Outline BasePlus Commission Employee.h (1 of 1) Functions earnings and print are already virtual – good practice to declare virtual even when overriding function

 2006 Pearson Education, Inc. All rights reserved. 40 Outline fig13_10.cpp (1 of 5)

 2006 Pearson Education, Inc. All rights reserved. 41 Outline fig13_10.cpp (2 of 5) Aiming base-class pointer at base-class object and invoking base-class functionality

 2006 Pearson Education, Inc. All rights reserved. 42 Outline fig13_10.cpp (3 of 5) Aiming derived-class pointer at derived-class object and invoking derived-class functionality Aiming base-class pointer at derived-class object and invoking derived-class functionality via polymorphism and virtual functions

 2006 Pearson Education, Inc. All rights reserved. 43 Outline fig13_10.cpp (4 of 5)

 2006 Pearson Education, Inc. All rights reserved. 44 Outline fig13_10.cpp (5 of 5)