Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

More on Classes Inheritance and Polymorphism
Object Oriented Programming
Overview of Data Structures and Algorithms
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 20 Object Oriented Theory II.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
ITEC200 – Week03 Inheritance and Class Hierarchies.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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,
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
An Object-Oriented Approach to Programming Logic and Design
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
More on Drawable Objects, Hierarchical Objects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, January.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
OOP with Objective-C Categories, Protocols and Declared Properties.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Object-Oriented Programming: Polymorphism Chapter 10.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
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 and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Object-Oriented Programming
Abstract Classes and Inheritence Operator over-loading
CS250 Introduction to Computer Science II
Polymorphism and access control
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Chapter 14 Abstract Classes and Interfaces
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism virtual methods

Module 11: Polymorhism #2 2000/01Scientific Computing in OOCourse code 3C59 Aims of this module A key feature of object oriented programming is the ability to use objects "polymorphically" This is one of the most powerful featrues, and you cannot be said to be truly writing OO code unless you understand and can use polymorphism. Polymorphism allows code to be written which operates upon a base class type, but which will also immediately (without modification) work on any other concrete classes which inherit from this base class, and which may be invented at any time in the future. We explain this feature and describe a context in which it might apply.

Module 11: Polymorhism #3 2000/01Scientific Computing in OOCourse code 3C Polymorphism....and the words get bigger This is all to do with being able to write applications which operate upon a base class type, but which will also immediately (without modification) work on any other concrete classes which inherit from this base class, and which may be invented at any time in the future. This is one of the most powerful OO features, and you cannot be said to be truly writing OO unless you understand and can use polymorphism. A normal example is to consider graphics objects - so we will but first consider in the abstract Payroll program: Employee-> ProjectManager / Contractor / Programmer BPhysicsAnalysis: TrackJet / ClusterJet

Module 11: Polymorhism #4 2000/01Scientific Computing in OOCourse code 3C59 Suppose you were going to write a DISPLAY program to display graphics shape objects on the screen. The problem is that at the point of writing you do not know which specific shape objects will be available. Therefore you write a generic Shape class which has all of the methods you want for your program to work: class Shape { public: void draw( float x, float y ) ; //draw object at x,y void setSize( float size ) ; //set size of shape private: GraphicsService graphics ; };

Module 11: Polymorhism #5 2000/01Scientific Computing in OOCourse code 3C59..and here is some generic code to implement the methods.. // void Shape::draw( float x, float y ) { //For generic shape just make a dot // call graphics library function to draw a point graphics.point( x, y ) ; } // void Shape::setSize( float size ) { // A dot has no size so do nothing }

Module 11: Polymorhism #6 2000/01Scientific Computing in OOCourse code 3C59 Now lets imagine a bit of the DISPLAY program which draws a border at the top of the screen, using a Shape main() { Shape x ; drawTopBorder( x ) ; } void drawTopBorder( Shape& s ) { // screen runs 0->1 in both directions // lets draw 10 shapes along top // Set size so that 10 fit in a line s.setSize( 0.1 ) ; // Top means y = 1. float y = 1. ; // Now draw 10 shapes along x for( float x=0; x < 1 ; x+=0.1 ) { s.draw( x, y ) ; }

Module 11: Polymorhism #7 2000/01Scientific Computing in OOCourse code 3C59 this code should draw something like this....

Module 11: Polymorhism #8 2000/01Scientific Computing in OOCourse code 3C59 Dots are boring - no-one is buying our code !!! We want to add the possibility of having lots of different shapes to draw.  Lets make lots of shapes inherited from the Shape class ! Shape SquareCircleUCLLogoAmphibious LandingCraft

Module 11: Polymorhism #9 2000/01Scientific Computing in OOCourse code 3C59 Here is the square class... class Square: public Shape { public: void draw( float x, float y ) ; void setSize( float size ) ; private: float length ; }; Note that we override the methods we add a variable to characterise a square (lenght of side)

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59... and here are the overriding methods... // void Square::draw( float x, float y ) { // Use graphis library to draw 4 lines graphics.line( x, x + lengh, y, y ) ; graphics.line( x + length, x + length, y, y+length ) ; graphics.line( x, x, y, y + length ) ; graphics.line( x, x+length, y+length, y + length ) ; } // void Square::setSize( float size ) { length = size ; }

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 Now look at a modified main display program: main() { Square x ; drawTopBorder( x ) ; } Square + its overriding methods Shape + its methods creates this void drawTopBorder( Shape& s ) { // lets draw 10 shapes along top // Set size so that 10 fit in a line s.setSize( 0.1 ) ; // Top means y = 1. float y = 1. ; // Now draw 10 shapes along x for( float x=0; x < 1 ; x+=0.1 ) { s.draw( x, y ) ; } invokes this.. which only sees the shape part of s

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59... so this code will still draw this as it only sees the default Shape methods....

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 This problem is solved by using: virtual methods These are a way of telling the system that: an object may have its methods overridden therefore it should look to see if the object that has been passed is really a sub class if so then invoke the appropriate methods A method is made virtual by placing the virtual keyword before its signature

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 To do this you modify the Shape class like this... class Shape { public: virtual void draw( float x, float y ) ; virtual void setSize( float size ) ; };.... and similarly the methods... // virtual void Shape::draw( float x, float y ) {.... same as before... } // virtual void Shape::setSize( float size ) {.... same as before... }

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59...now when you do this (this code is unchanged)... main() { Square x ; drawTopBorder( x ) ; } Square + its overriding methods Shape + its methods creates this void drawTopBorder( Shape& s ) { // lets draw 10 shapes along top // Set size so that 10 fit in a line s.setSize( 0.1 ) ; // Top means y = 1. float y = 1. ; // Now draw 10 shapes along x for( float x=0; x < 1 ; x+=0.1 ) { s.draw( x, y ) ; } invokes this.. which finds out that s is in fact a Square

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59...and so finally and everyone buys our graphics program....

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 Application: drawTopBorder( ) Shape SquareCircleUCLLogoAmphibious LandingCraft operates only on Shape All virtual method calls forwarded to sub-class This is called "Polymorphism" You are said to be using subclasses of Shape polymorphically

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 main() { Square x ; drawTopBorder( x ) ; } Square + its overriding methods Shape + its methods void drawTopBorder( Shape& s ) { // lets draw 10 shapes along top // Set size so that 10 fit in a line s.setSize( 0.1 ) ; // Top means y = 1. float y = 1. ; // Now draw 10 shapes along x for( float x=0; x < 1 ; x+=0.1 ) { s.draw( x, y ) ; } Technical point: You MUST have a "pointer" or a "reference" here !!! Otherwise it just makes a local copy of the Shape part of Square

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 main() { Square x ; drawTopBorder( x ) ; } Square + its overriding methods Shape + its methods void drawTopBorder( Shape s ) { // lets draw 10 shapes along top // Set size so that 10 fit in a line s.setSize( 0.1 ) ; // Top means y = 1. float y = 1. ; // Now draw 10 shapes along x for( float x=0; x < 1 ; x+=0.1 ) { s.draw( x, y ) ; } Technical point: You MUST have a "pointer" or a "reference" here !!! Otherwise it just makes a local copy of the Shape part of Square Copy of Shape

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 Student exercise (easy option) Write the classes to represent the inheritance structure shown below (for this example we use entities which are fairly trivially different) Write and run a function which receives a ThreeVector reference and uses it to invoke a virtual method which prints the state of the actual sub-class entity passed in its argument I.e. something like: void printEntityState( ThreeVector& e ) {....} You will have to make sure that you supply a virtual void print( ) method in each of the base class and sub-classes ThreeVector px, py, pz Track px,py,pz radius Particle E, px,py,pz,m

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 Student exercise (difficult option) Modify the SuperHero code you wrote earlier so that methods are virtual Modify the fight method such that its argument is always SuperHero& Write a battle function void battle( SuperHero& a, SuperHero& b ) Which takes two SuperHero ’s polymorphically. and makes them fight. It then tells the winner to print its name (you will need to add a method) Write a main program to create some SuperHero ’s and then call the battle function, passing them as arguments in turn. SuperHero InvisibleSuperHero LifeSucking.. Flying...

Module 11: Polymorhism # /01Scientific Computing in OOCourse code 3C59 Summary of Module 11: Polymorphism & virtual methods In this module we have covered the following topics. Polymorphism Use of sub-classes polymorphically via references or pointers to a base class Use of virtual methods and the virtual keyword