04e_polymorph

04e_polymorph - Data Structures - CSCI 102 CS102 C+...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 C++ Polymorphism Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C Structs (Ch 10) Topics to cover 2 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Object-Oriented Programming C++ Classes (Ch 11) Constructors Destructors Member functions Exception Handling (Ch 15) Namespaces (Ch 8) Operator Overloading (Ch 14) Polymorphism (Ch 13) Virtual functions Abstract classes Interfaces
Background image of page 2
The ability of an object of one type to appear and be used like an object of a different type What is polymorphism ? 3 Data Structures - CSCI 102 Copyright © William C. Cheng Polymorphism Based on inheritance, Triange Is-A Shape so. .. Remember our Shape and Triangle classes? We can actually pass Triangles around as Shapes! Triangle t( 5 , 5 , 15 , 20 ); Shape tval = t; Shape &tref = t; Shape *tptr = &t; Shape *tptr2 = new Triangle( 1 , 2 , 55 , 62 ); Write algorithms & frameworks that manipulate base class You can write very advanced code with polymorphism Can even write code for subclasses that hasn’t been realized or designed!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
So what does this code do? 4 Data Structures - CSCI 102 Copyright © William C. Cheng Polymorphism Triangle t( 5 , 5 , 15 , 20 ); t.print(); Shape tval = t; tval.print(); Shape &tref = t; tref.print(); Shape *tptr = &t; tptr->print(); It should call the Triangle object’s overloaded print method, right? Does it?
Background image of page 4
Even though we can pass around a Triangle as a Shape, it doesn’t help if we can’t make it call Triangle’s overloaded methods 5 Data Structures - CSCI 102 Copyright © William C. Cheng Early/Static/Compile-Time Binding When your code is compiled, the compiler directly associates a function call with a memory location to make function calls a fast lookup Why does this happen? Isn’t there some way to tell the compiler to wait until run-time to figure out what version of the " print() " function it should call? When you compiler sees " s.print() ", it immediately associates it with the Shape class
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The keyword virtual tells C++ to find the "most-derived" version of a method (a.k.a. the one farthest down the inheritance tree that’s still relevant) Tells the C++ to wait until the code runs to figure out what version of a method it should actually call 6 Data Structures - CSCI 102 Copyright © William C. Cheng Virtual Functions Just add the keyword virtual to the beginning of any function prototype in your class How is it used? It’s more expensive than normal lookup (time &
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

04e_polymorph - Data Structures - CSCI 102 CS102 C+...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online