C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.

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.
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: Polymorphism.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
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
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Object Oriented Programming using Java - Polymorphism
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming Polymorphism Session 07 Mata kuliah: M0874 – Programming II Tahun: 2010.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
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.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
 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.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Object-Oriented Programming: Polymorphism Chapter 10.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
 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.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Polymorphism
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Learning Objectives Inheritance Virtual Function.
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism 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
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Abstract Classes and Interfaces
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:

C++ Polymorphism Systems Programming

Systems Programming: Polymorphism 2   Polymorphism Examples   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   Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers  switch  Type Fields and switch Statements  virtual  Abstract Classes and Pure virtual Functions   Polymorphism Case Study C++ Polymorphism

Systems Programming: Polymorphism 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.

Systems Programming: Polymorphism 4 Polymorphism Examples Example Animal hierarchy  move.  Animal base class – every derived class has a function move.  vectorAnimal  Different animal objects are maintained as a vector of Animal pointers.   Program issues same message ( move ) to each animal generically.   Proper function gets called – Fish –A Fish will move by swimming. – Frog –A Frog will move by jumping. – Bird –A Bird will move by flying.

Systems Programming: Polymorphism Polymorphism Examples virtual 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. SpaceObject s Example: SpaceObject s – SpaceObjectdraw. –Video game manipulates objects of types that inherit from SpaceObject, which contains member function draw. – draw –Function draw implemented appropriately for the different derived classes. – SpaceObject –A screen-manager program maintains a container of SpaceObject pointers. – drawSpaceObject –Call draw on each object using SpaceObject pointers drawThe proper draw function is called based on object’s type. – SpaceObject –A new class derived from SpaceObject can be added without affecting the screen manager.

Systems Programming: Polymorphism Relationships among Objects in an Inheritance Hierarchy   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 the type of the handle used to invoke the function, not on the type of the object to which the handle points –Invoked functionality depends on the type of the handle used to invoke the function, not on the type of the object to which the handle points.  virtual  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 –Crucial to implementing polymorphic behavior.

Systems Programming: Polymorphism 7 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 8 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 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 9 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 10 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 11 Calculate earnings based on commission rate and gross sales Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 12 Display name, social security number, gross sales and commission rate Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 13 Redefine functions earnings and print Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 14 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 15 Redefined earnings function incorporates base salary Redefined print function displays additional BasePlusCommissionEmployee details Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 16 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 17 Aiming base-class pointer at base-class object and invoking base-class functionality Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 18 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 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 19 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism 20 Invoking Base-Class Functions from Derived-Class Objects

Systems Programming: Polymorphism Aiming Derived-Class Pointers at Base-Class Objects   Aim a derived-class pointer at a base- class object. – –C++ compiler generates error. CommissionEmployee BasePlusCommissionEmployeeCommissionEmployee (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.

Systems Programming: Polymorphism 22 Cannot assign base-class object to derived-class pointer because is-a relationship does not apply Aiming Derived-Class Pointers at Base-Class Objects

Systems Programming: Polymorphism 23

Systems Programming: Polymorphism 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, this can be accomplished using downcasting (Section 13.8).

Systems Programming: Polymorphism 25 Cannot invoke derived-class-only members from base-class pointer Aiming base-class pointer at derived-class object

Systems Programming: Polymorphism 26

Systems Programming: Polymorphism Virtual Functions  handle  Normally the handle determines which class’s functionality to invoke.  virtual  With virtual functions –object virtual –The type of the object being pointed to, not the type of the handle, determines which version of a virtual function to invoke. – –This allows a program to dynamically (at runtime rather than compile time) determine which function to use. dynamic bindinglate binding.Referred to as dynamic binding or late binding.

Systems Programming: Polymorphism Virtual Functions  virtual  Declared by preceding the function’s prototype with the keyword virtual in the base class. Example virtual void draw () const; Shape would appear in the base class Shape.  shapePtr->draw() ), the program will choose the correct derived-class draw function dynamically based on the object type.  If the program invokes a virtual function through a base-class pointer to a derived-class object (e.g., shapePtr->draw() ), the program will choose the correct derived-class draw function dynamically based on the object type.  override virtual functions polymorphic behavior  Derived classes override virtual functions to enable polymorphic behavior.

Systems Programming: Polymorphism Virtual Functions  virtual virtual  Once declared virtual, a function remains virtual all the way down the hierarchy.  virtual squareObject.draw() static binding and this is Not polymorphic behavior!  When a virtual function is called by referencing a specific object by name using the dot member-selection operator(e.g., squareObject.draw() ), the function invocation is resolved at compile time.{This is static binding and this is Not polymorphic behavior!}  virtual  Dynamic binding with virtual functions only occurs off pointer and reference handles.

Systems Programming: Polymorphism 30 Virtual Functions

Systems Programming: Polymorphism 31 Declaring earnings and print as virtual allows them to be overridden, not redefined Virtual Functions

Systems Programming: Polymorphism 32 Functions earnings and print are already virtual – good practice to declare virtual even when overriding function Virtual Functions

Systems Programming: Polymorphism 33 Virtual Functions

Systems Programming: Polymorphism 34 Aiming base-class pointer at base-class object and invoking base-class functionality Virtual Functions

Systems Programming: Polymorphism 35 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 Virtual Functions

Systems Programming: Polymorphism 36 Virtual Functions

Systems Programming: Polymorphism 37 Virtual Functions

Systems Programming: Polymorphism 38 Summarizing Allowed Assignments Between Base- Class and Derived-Class Objects and Pointers   Four ways to aim base-class and derived-class pointers at base-class and derived-class objects – –Aiming a base-class pointer at a base-class object Is straightforward. – –Aiming a derived-class pointer at a derived-class object Is straightforward. – –Aiming a base-class pointer at a derived-class object Is safe, but can be used to invoke only member functions that base-class declares (unless downcasting is used). virtualCan achieve polymorphism with virtual functions – –Aiming a derived-class pointer at a base-class object Generates a compilation error.

Systems Programming: Polymorphism Type Fields and switch Statements  switch  A switch statement can be used to determine the type of an object at runtime. – –Include a type field as a data member in the base class. – –This enables the programmer to invoke appropriate action for a particular object. – –Causes problems A type test may be forgotten. May forget to add new types.

Systems Programming: Polymorphism 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. –abstract base classes. –Normally used as base classes and called abstract base classes. Provides an appropriate base class from which other classes can inherit.  concrete classes.  Classes used to instantiate objects are called concrete classes. – –Must provide implementation for every member function they define.

Systems Programming: Polymorphism 41 Abstract Classes and Pure virtual Functions  “pure” by  Pure virtual function:: A class is made abstract by declaring one or more of its virtual functions to be “pure” by placing “ = 0 ” in its declaration. Example virtual void draw() const = 0; –pure specifier –“ = 0 ” is known as a pure specifier. – –Do not provide implementations.

Systems Programming: Polymorphism 42 Abstract Classes and Pure virtual Functions  concrete derived class virtual  Every concrete derived class must override all base-class pure virtual functions with concrete implementations.   If not overridden, the 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.

Systems Programming: Polymorphism 43 Software Engineering Observation 24.8  abstract class  An abstract class defines a common public interface for the various classes in a class hierarchy.  abstract class virtual concrete derived classes  An abstract class contains one or more pure virtual functions that concrete derived classes must override.

Systems Programming: Polymorphism 44 Abstract Classes and Pure virtual Functions  abstract base class  The abstract base class can be used to declare pointers and references that 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 is particularly effective for implementing layered software systems. Examples: 1. Reading or writing data from and to devices. 2. An iterator class that can traverse all the objects in a container.