Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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.

3 Module 11: Polymorhism #3 2000/01Scientific Computing in OOCourse code 3C59 11.1 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

4 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 ; };

5 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 }

6 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 ) ; } 0 1 1 0

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

8 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

9 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)

10 Module 11: Polymorhism #10 2000/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 ; }

11 Module 11: Polymorhism #11 2000/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

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

13 Module 11: Polymorhism #13 2000/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

14 Module 11: Polymorhism #14 2000/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... }

15 Module 11: Polymorhism #15 2000/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

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

17 Module 11: Polymorhism #17 2000/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

18 Module 11: Polymorhism #18 2000/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

19 Module 11: Polymorhism #19 2000/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

20 Module 11: Polymorhism #20 2000/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

21 Module 11: Polymorhism #21 2000/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...

22 Module 11: Polymorhism #22 2000/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


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

Similar presentations


Ads by Google