CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Advertisements

Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes CSE335 Spring 2008 E. Kraemer adapted from K. Stirewalt.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CMSC 202 Lesson 18 Polymorphism 1. Warmup What errors are present in the following hierarchy? Assume GetCurrentTime() and RingBell() are defined elsewhere.
Chapter -6 Polymorphism
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2003 Prentice Hall, Inc. All rights reserved Virtual Functions Polymorphism –Same message, “print”, given to many objects All through a base.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
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.
Polymorphism Lecture - 9.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Lecture 11: Virtual Function and Pure Virtual Function
CMSC 202 Polymorphism.
Class A { public : Int x; A()
Polymorphism.
Polymorphism Lec
Polymorphism CT1513.
Chapter 20- Virtual Functions and Polymorphism
Polymorphism 1 CMSC 202.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Polymorphism - Greek for “many forms”
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Inheritance: Polymorphism and Virtual Functions
Capitolo 10- Virtual Functions and Polymorphism
Virtual Functions Example
Lecture 6: Polymorphism
Computer Science II for Majors
Presentation transcript:

CMSC 2021 Polymorphism

CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular object. Static (Early) Binding When the compiler can determine which method in the class hierarchy to use for a particular object. Dynamic (Late) Binding When the determination of which method in the class hierarchy to use for a particular object occurs during program execution.

CMSC 2023 Static Binding Compiler can determine For example, Time t1; ExtTime et2; t1.Set(); // static binding et1.Set(); // static binding t1.Write(); // static binding et1.Write(); // static binding

CMSC 2024 Dynamic Binding Compiler cannot determine binding of object to method Binding is determined dynamically at runtime To indicate that a method is to be bound dynamically, must use the reserved word virtual virtual void Write(); // dynamic binding will occur When a method is defined as virtual, all overriding methods from that point on down the hierarchy must be defined as virtual A pure virtual method is defined as: virtual void Write() const = 0; When a class contains a pure virtual method, the class is truly abstract; I.e., it cannot be instantiated

CMSC 2025 Static vs. Dynamic Binding Example Class Attributes Methods Shape - virtual computeArea - virtual computeVolume - pure virtual printShapeName - pure virtual print Point x - virtual printShapeName y - virtual print - constructor - setPoint - getX - getY Circle radius - virtual printShapeName - virtual print - virtual computeArea - setRadius - getRadius - constructor Cylinder height - virtual printShapeName - virtual print - virtual computeArea - virtual computeVolume - setHeight - getHeight - constructor

CMSC 2026 Static vs. Dynamic Binding Example (con’t) void virtualViaPointer(const Shape*); void virtualViaReference(const Shape &); void main() { Point point(7, 11); Circle circle(3.5, 22, 8); Cylinder cylinder(10, 3.3, 10, 10); point.printShapeName(); // static binding point.print(); cout << endl; // static binding circle.printShapeName(); // stataic binding circle.print(); cout << endl; // static binding cylinder.printShapeName(); // static binding cylinder.print(); cout << endl; // static binding

CMSC 2027 Static vs. Dynamic Binding Example (con’t) Shape *arrayOfShapes[3]; // array of base class pointers arrayOfShapes[0] = &point; arrayOfShapes[1] = &circle; arrayOfShapes[2] = &cylinder; for (int I = 0; I < 3; I++) virtualViaPointer(arrayShapes[I]); for (int j = 0; j < 3; j++) virtualViaReference(*arrayShapes[j]); }

CMSC 2028 Static vs. Dynamic Binding Example (con’t) void virtualViaPointer(const Shape *baseClassPtr) { // Dynamic binding takes place baseClassPtr->printShapeName(); baseClassPtr->print(); cout computeArea() << endl computeVolume() << endl; }

CMSC 2029 Static vs. Dynamic Binding Example (con’t) void virtualViaReference(const Shape &baseClassRef) { // Dynamic binding takes place baseClassRef.printShapeName(); baseClassRef.print(); cout << “Area = “ << baseClassRef.computeArea() << endl; << “Volume = “ << baseClassRef.computeVolume() << endl; }