PolymorphismCS-2303, C-Term 20101 Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,

Slides:



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

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
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.
Object-Oriented Programming: Polymorphism
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
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.
Object Oriented Programming
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CMSC 202 Polymorphism.
Class A { public : Int x; A()
Polymorphism.
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
“Under the Hood” of Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
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: Inheritance and Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Abstract Classes and Interfaces
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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:

PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

PolymorphismCS-2303, C-Term Review — Accessing Members of Base and Derived Classes class B { public: void m(); void n();... }// class B class D: public B { public void m(); void p();... }// class D The following are legal:– B_obj.m() //B’s m() B_obj.n() D_obj.m() //D’s m() D_obj.n() //B’s n() D_obj.p() B_ptr->m() //B’s m() B_ptr->n() D_ptr->m() //D’s m() D_ptr->n() //B’s n() D_ptr->p()

PolymorphismCS-2303, C-Term Review — Accessing Members of Base and Derived Classes (continued) class B { public: void m(); void n();... }// class B class D: public B { public void m(); void p();... }// class D The following are legal:– B_ptr = D_ptr; The following are not legal:– D_ptr = B_ptr; B_ptr->p(); Even if B_ptr is known to point to an object of class D Class D redefines method m()

PolymorphismCS-2303, C-Term Review — Accessing Members of Base and Derived Classes (continued) Access to members of a class object is determined by the type of the handle. Definition: Handle The thing by which the members of an object are accessed May be –An object name (i.e., variable, etc.) –A reference to an object –A pointer to an object Deitel & Deitel, §20.3 Note: this is not exactly the same thing as handle classes mentioned by Stroustrup

PolymorphismCS-2303, C-Term Review — Accessing Members of Base and Derived Classes (continued) This is referred to as static binding I.e., the binding between handles and members is determined at compile time I.e., statically

PolymorphismCS-2303, C-Term What if we need Dynamic Binding? I.e., what if we need a class in which access to methods is determined at run time by the type of the object, not the type of the handle class Shape { public: void Rotate(); void Draw();... } class Rectangle: public Shape { public: void Rotate(); void Draw();... } class Ellipse: public Shape { public: void Rotate(); void Draw();... } Need to access the method to draw the right kind of object Regardless of handle!

PolymorphismCS-2303, C-Term Solution – Virtual Functions Define a method as virtual, and the subclass method overrides the base class method E.g., class Shape { public: virtual void Rotate(); virtual void Draw();... } This tells the compiler to add internal pointers to every object of class Shape and its derived classes, so that pointers to correct methods can be stored with each object.

PolymorphismCS-2303, C-Term What if we need Dynamic Binding? class Shape { public: virtual void Rotate(); virtual void Draw();... } I.e., subclass methods override the base class methods –(if specified) C++ dynamically chooses the correct method for the class from which the object was instantiated. class Rectangle: public Shape { public: void Rotate(); void Draw();... } class Ellipse: public Shape { public: void Rotate(); void Draw();... }

PolymorphismCS-2303, C-Term Notes on virtual If a method is declared virtual in a class, –… it is automatically virtual in all derived classes It is a really, really good idea to make destructors virtual! virtual ~Shape(); –Reason: to invoke the correct destructor, no matter how object is accessed

PolymorphismCS-2303, C-Term Notes on virtual (continued) A derived class may optionally override a virtual function If not, base class method is used class Shape { public: virtual void Rotate(); virtual void Draw();... } class Line: public Shape { public: void Rotate(); //Use base class Draw method... }

PolymorphismCS-2303, C-Term Example Deitel & Deitel, §24.3 CommissionEmployee and BasePlusCommissionEmployee

PolymorphismCS-2303, C-Term Polymorphism The ability to declare functions/methods as virtual is one of the central elements of polymorphism in C++ Polymorphism:– from the Greek “having multiple forms” In programming languages, the ability to assign a different meaning or usage to something in different contexts

PolymorphismCS-2303, C-Term A Note from D&D on virtual When a virtual function is called by name of a specific object –using the dot member-selection operator (e.g., squareObject.draw() ), … the function invocation is resolved at compile time –This is static binding, not polymorphic behavior! Dynamic binding with virtual functions only occurs from pointer and reference handles Have not been able to verify from any other source!

PolymorphismCS-2303, C-Term Summary – Based and Derived Class Pointers Base-class pointer pointing to base-class object –Straightforward Derived-class pointer pointing to derived-class object –Straightforward Base-class pointer pointing to derived-class object –Safe –Can access non-virtual methods of only base-class –Can access virtual methods of derived class Derived-class pointer pointing to base-class object –Compilation error

PolymorphismCS-2303, C-Term Questions?

PolymorphismCS-2303, C-Term Abstract and Concrete Classes Abstract Classes –Classes from which it is never intended to instantiate any objects Incomplete—derived classes must define the “missing pieces”. Too generic to define real objects. –Normally used as base classes and called abstract base classes Provide appropriate base class frameworks from which other classes can inherit. Concrete Classes –Classes used to instantiate objects –Must provide implementation for every member function they define Definitions

PolymorphismCS-2303, C-Term Pure virtual Functions A class is made abstract by declaring one or more of its virtual functions to be “pure” –I.e., by placing "= 0" in its declaration Example virtual void draw() const = 0; –"= 0" is known as a pure specifier. –Tells compiler that there is no implementation.

PolymorphismCS-2303, C-Term Pure virtual Functions (continued) Every concrete derived class must override all base-class pure virtual functions –with concrete implementations If even one pure virtual function is not overridden, the derived-class will also be abstract –Compiler will refuse to create any objects of the class –Cannot call a constructor

PolymorphismCS-2303, C-Term Purpose When it does not make sense for base class to have an implementation of a function Software design requires all concrete derived classes to implement the function Themselves

PolymorphismCS-2303, C-Term Why Do we Want to do This? To define a common public interface for the various classes in a class hierarchy –Create framework for abstractions defined in our software system The heart of object-oriented programming Simplifies a lot of big software systems Enables code re-use in a major way Readable, maintainable, adaptable code

PolymorphismCS-2303, C-Term Abstract Classes and Pure virtual Functions Abstract base class can be used to declare pointers and references referring to objects of any derived concrete class Pointers and references used to manipulate derived-class objects polymorphically Polymorphism is particularly effective for implementing layered software systems – e.g., 1.Reading or writing data from and to devices. 2.Iterator classes to traverse all the objects in a container.

PolymorphismCS-2303, C-Term Example – Graphical User Interfaces All objects on the screen are represented by derived classes from an abstract base class Common windowing functions Redraw or refresh Highlight Respond to mouse entry, mouse clicks, user actions, etc. Every object has its own implementation of these functions Invoked polymorphically by windowing system

PolymorphismCS-2303, C-Term Questions?