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

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 1 Embedded Computing.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
EU market situation for eggs and poultry Management Committee 20 October 2011.
2 |SharePoint Saturday New York City
VOORBLAD.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
1 Chapter 13 Nuclear Magnetic Resonance Spectroscopy.
Energy Generation in Mitochondria and Chlorplasts
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
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,
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Cpt S 122 – Data Structures Polymorphism
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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 Polymorphism, Interfaces & Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
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.
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Object-Oriented Programming: Polymorphism
Presentation transcript:

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

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 – 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. 4 Software Engineering Observation 13.6 Polymorphic programming can eliminate the need for unnecessary switch logic. By using the C++ polymorphism mechanism to perform the equivalent logic, programmers can avoid the kinds of errors typically associated with switch logic.

2006 Pearson Education, Inc. All rights reserved Abstract Classes and Pure virtual Functions Abstract classes – Classes from which the programmer never intends to instantiate any objects Incomplete derived classes must define the missing pieces Too generic to define real objects – Normally used as base classes, called abstract base classes Provides an appropriate base class from which other classes can inherit Classes used to instantiate objects are called concrete classes – Must provide implementation for every member function they define

2006 Pearson Education, Inc. All rights reserved Abstract Classes and Pure virtual Functions (Cont.) Pure virtual function – A class is made abstract by declaring one or more of its virtual functions to be pure Placing = 0 in its declaration – Example virtual void draw() const = 0; – = 0 is known as a pure specifier – Do not provide implementations Every concrete derived class must override all base-class pure virtual functions with concrete implementations – If not overridden, derived-class will also be abstract – Used when it does not make sense for base class to have an implementation of a function, but the programmer wants all concrete derived classes to implement the function

2006 Pearson Education, Inc. All rights reserved. 7 Software Engineering Observation 13.8 An abstract class defines a common public interface for the various classes in a class hierarchy. An abstract class contains one or more pure virtual functions that concrete derived classes must override.

2006 Pearson Education, Inc. All rights reserved Abstract Classes and Pure virtual Functions (Cont.) We can use the abstract base class to declare pointers and references – Can refer to objects of any concrete class derived from the abstract class – Programs typically use such pointers and references to manipulate derived-class objects polymorphically Polymorphism particularly effective for implementing layered software systems – Reading or writing data from and to devices Iterator class – Can traverse all the objects in a container

2006 Pearson Education, Inc. All rights reserved. 9 Fig | Employee hierarchy UML class diagram.

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

2006 Pearson Education, Inc. All rights reserved. 11 Outline Employee.h (2 of 2) Function earnings is pure virtual, not enough data to provide a default, concrete implementation Function print is virtual, default implementation provided but derived-classes may override

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

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

2006 Pearson Education, Inc. All rights reserved Creating Concrete Derived Class SalariedEmployee SalariedEmployee inherits from Employee – Includes a weekly salary Overridden earnings function incorporates weekly salary Overridden print function incorporates weekly salary – Is a concrete class (implements all pure virtual functions in abstract base class)

2006 Pearson Education, Inc. All rights reserved. 15 Outline Salaried Employee.h (1 of 1) SalariedEmployee inherits from Employee, must override earnings to be concrete Functions will be overridden (or defined for the first time)

2006 Pearson Education, Inc. All rights reserved. 16 Outline Salaried Employee.cpp (1 of 2) Maintain new data member weeklySalary

2006 Pearson Education, Inc. All rights reserved. 17 Outline Salaried Employee.cpp (1 of 2) Overridden earnings and print functions incorporate weekly salary

2006 Pearson Education, Inc. All rights reserved Creating Concrete Derived Class HourlyEmployee HourlyEmployee inherits from Employee – Includes a wage and hours worked Overridden earnings function incorporates the employee s wages multiplied by hours (taking time-and-a-half pay into account) Overridden print function incorporates wage and hours worked – Is a concrete class (implements all pure virtual functions in abstract base class)

2006 Pearson Education, Inc. All rights reserved. 19 Outline Hourly Employee.h (1 of 1) HourlyEmployee inherits from Employee, must override earnings to be concrete Functions will be overridden (or defined for first time)

2006 Pearson Education, Inc. All rights reserved. 20 Outline Hourly Employee.cpp (2 of 2) Maintain new data member, hoursWorked Overridden earnings and print functions incorporate wage and hours

2006 Pearson Education, Inc. All rights reserved Creating Concrete Derived Class CommissionEmployee CommissionEmployee inherits from Employee – Includes gross sales and commission rate Overridden earnings function incorporates gross sales and commission rate Overridden print function incorporates gross sales and commission rate – Concrete class (implements all pure virtual functions in abstract base class)

2006 Pearson Education, Inc. All rights reserved. 22 Outline Commission Employee.h (1 of 1) CommissionEmployee inherits from Employee, must override earnings to be concrete Functions will be overridden (or defined for first time)

2006 Pearson Education, Inc. All rights reserved. 23 Outline Commission Employee.cpp (1 of 2) Maintain new data member, commissionRate

2006 Pearson Education, Inc. All rights reserved. 24 Outline Commission Employee.cpp (2 of 2) Maintain new data member, grossSales Overridden earnings and print functions incorporate commission rate and gross sales

2006 Pearson Education, Inc. All rights reserved Creating Indirect Concrete Derived Class BasePlusCommissionEmployee BasePlusCommissionEmployee inherits from CommissionEmployee – Includes base salary Overridden earnings function that incorporates base salary Overridden print function that incorporates base salary – Concrete class, because derived class is concrete Not necessary to override earnings to make it concrete, can inherit implementation from CommissionEmployee – Although we do override earnings to incorporate base salary

2006 Pearson Education, Inc. All rights reserved. 26 Outline BasePlus Commission Employee.h (1 of 1) BasePlusCommissionEmployee inherits from CommissionEmployee, already concrete Functions will be overridden

2006 Pearson Education, Inc. All rights reserved. 27 Outline BasePlus Commission Employee.cpp (1 of 2) Maintain new data member, baseSalary

2006 Pearson Education, Inc. All rights reserved. 28 Outline BasePlus Commission Employee.cpp (1 of 2) Overridden earnings and print functions incorporate base salary

2006 Pearson Education, Inc. All rights reserved Demonstrating Polymorphic Processing Create objects of types SalariedEmployee, HourlyEmployee, CommissionEmployee and BasePlusCommissionEmployee – Demonstrate manipulating objects with static binding Using name handles rather than pointers or references Compiler can identify each object s type to determine which print and earnings functions to call – Demonstrate manipulating objects polymorphically Uses a vector of Employee pointers Invoke virtual functions using pointers and references

2006 Pearson Education, Inc. All rights reserved. 30 Outline fig13_23.cpp (1 of 7)

2006 Pearson Education, Inc. All rights reserved. 31 Outline fig13_23.cpp (2 of 7) Using objects (rather than pointers or references) to demonstrate static binding

2006 Pearson Education, Inc. All rights reserved. 32 Outline fig13_23.cpp (3 of 7) vector of Employee pointers, will be used to demonstrate dynamic binding Demonstrate dynamic binding using first pointers, then references

2006 Pearson Education, Inc. All rights reserved. 33 Outline fig13_23.cpp (4 of 7) Using references and pointers cause virtual functions to be invoked polymorphically

2006 Pearson Education, Inc. All rights reserved. 34 Outline fig13_23.cpp (5 of 7)

2006 Pearson Education, Inc. All rights reserved. 35 Outline fig13_23.cpp (6 of 7)

2006 Pearson Education, Inc. All rights reserved. 36 Outline fig13_23.cpp (7 of 7)