Polymorphism Polymorphism occurs with objects instantiated from a set of classes related by inheritance to the same ancestor class. –Each class provides.

Slides:



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

Copyright © 2003 Pearson Education, Inc. Slide 1.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10: Introduction to Inheritance
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
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.
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,
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,
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
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
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.
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.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© 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.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
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.
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,
Web Design & Development Lecture 9
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
CS212: Object Oriented Analysis and Design
Types of Programming Languages
CS250 Introduction to Computer Science II
Class vs Abstract vs Interface
Polymorphism Lec
Learning Objectives Inheritance Virtual Function.
Polymorphism CT1513.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Inheritance: Polymorphism and Virtual Functions
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
VIRTUAL FUNCTIONS RITIKA SHARMA.
C++ Polymorphism Reference and pointer implicit type casting
Extending Classes Through Inheritance
Lecture 6: 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:

Polymorphism Polymorphism occurs with objects instantiated from a set of classes related by inheritance to the same ancestor class. –Each class provides its own implementation of the (virtual) functions. –Therefore, objects respond differently to the same set of messages originally declared in the ancestor.

Polymorphism Polymorphism is most useful when there are a series of related classes that require the “same” behaviors. –The individual classes typically define their own specific behavior implementation.

Polymorphism Example #1: –A company where there is an abstract class Employee. Every employee must earn money but the way earnings are calculated may be different based upon the type of employee (manager, commission worker, hourly worker).

Polymorphism Example #2: –Most 2-dimensional shapes have a position, area, and perimeter. The specific shape subclasses then define the particular implementations of these functions.

Polymorphism Example #3: –All customers at a store are allowed to pay with cash. –Some customers have a charge account at the store, and therefore have an alternate way to pay. –When information on customers is printed, the ones with charge accounts should show their balance, etc.

Virtual Functions A C++ virtual function is a function that can be made polymorphic by redefinition. A non-virtual function –can still be redefined –causes the function that is called on an object to depend solely on the object’s compile-time type –is therefore not polymorphic.

Declaring Virtual Functions class Parent { virtual void poly(); void strict(); }; class Child: public Parent { virtual void poly(); // redefinition void strict(); // redefinition };

Calling Virtual Functions Parent *p( new Parent() ); Parent *c( new Child() ); p->poly(); // calls Parent::poly() p->strict(); // calls Parent::strict() c->poly(); // calls Child::poly() c->strict(); // calls Parent::strict() !

Constructors / Destructors By definition a constructor function cannot be defined as a virtual function. –See Factory design patterns for ways around this. A class that contains virtual functions should also contain a virtual destructor.

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, they are not well-defined enough for instantiation. –Abstract classes provide a generic base class that can be used by concrete classes to provide an interface and/or implementation.

Abstract Classes An abstract class in C++ is defined as any class that contains at least one pure virtual function.

Pure Virtual Functions A pure virtual function associates an “initializer” with the virtual function declaration. virtual returnType functionName () const = 0; Derived classes may define an implementation for the pure virtual function. –If the derived class does not include an implementation for the pure virtual function, the derived class is also abstract.

Pure Virtual Functions Defining pure virtual functions in the abstract base class: –Abstract.h: class declaration virtual ret-type functionName(..,..,..) const = 0; –Abstract.C: class definition There is no implementation in the *.C file. –(although no one will stop you!)

Pure Virtual Functions Implementing virtual functions in a concrete derived class: –Concrete.h virtual ret-type functionName(..,..,..) const; –Concrete.C ret-type Concrete::functionName(..,..,..) const { // … }

Pure Virtual Functions Animal Dog

Pure Virtual Functions Animal.h virtual void speak() = 0; Animal.C –No implementation Dog.h virtual void speak(); Dog.C void speak() { cout << “Arf!”; } Here we define a group of classes known collectively as animals. All animals can speak, but each one speaks differently. If we ask an animal to speak, it will make the appropriate response.

For The Curious: What is happening? Any time the C++ compiler encounters a class definition containing a virtual function, the compiler creates a virtual function table (vtable) for the class. –The vtable is used in the program to determine which function implementation is to be executed when the function is called. –It also helps realize RTTI (not used here)