Presentation is loading. Please wait.

Presentation is loading. Please wait.

Under the Hood of Polymorphism CS-2303, C-Term 20101 Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from.

Similar presentations


Presentation on theme: "Under the Hood of Polymorphism CS-2303, C-Term 20101 Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from."— Presentation transcript:

1 Under the Hood of Polymorphism CS-2303, C-Term 20101 Under the Hood of Polymorphism CS-2303 System Programming Concepts (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)

2 Under the Hood of Polymorphism CS-2303, C-Term 20102 Review – 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();... } Compiler to adds internal pointers to every object of class Shape and its derived classes, so that pointers to correct methods can be accessed from any object via any pointer. Creates three levels of indirection to access a virtual method.

3 Under the Hood of Polymorphism CS-2303, C-Term 20103 Virtual Function Table (vtable) created for any class with virtual function(s) One for base class, one for each derived class Vtable entries point to methods for appropriate class Constructor sets a pointer in each object to point to appropriate vtable Executing program indexes into vtable to select appropriate function implementation for each function call ©1992-2010 by Pearson Education, Inc. All Rights Reserved. Compiling Virtual Functions

4 Under the Hood of Polymorphism CS-2303, C-Term 20104 ©1992-2010 by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig 24.24 Pointer to object (level 1)

5 Under the Hood of Polymorphism CS-2303, C-Term 20105 ©1992-2010 by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig 24.24 Pointer to vtable (stored in every object – level 2)

6 Under the Hood of Polymorphism CS-2303, C-Term 20106 ©1992-2010 by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig 24.24 Index into vtable to access method (level 3)

7 Under the Hood of Polymorphism CS-2303, C-Term 20107 Performance Impact Nominal Must access two additional pointers to get to method Versus direct access for non- virtual methods

8 Under the Hood of Polymorphism CS-2303, C-Term 20108 Trade-off Nominal performance impact vs. clarity of thought and ease of programming Most of the time, clarity of thought wins Especially with modern optimizing compilers In some (rare) cases, performance is essential Note:– Java has similar implementation under the hood for all methods

9 Under the Hood of Polymorphism CS-2303, C-Term 20109 Software Engineering Observation Dynamic binding enables independent software vendors (ISVs) to distribute software without revealing proprietary secrets. Software distributions can consist of only header files and object files –No source code needs to be revealed. Software developers use inheritance to derive new classes from those provided by the ISVs –Previous software that works with the ISVs classes still work with the derived classes –Will use the overridden virtual functions provided in these classes (via dynamic binding). ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

10 Under the Hood of Polymorphism CS-2303, C-Term 201010 Software Engineering Observation (example) Develop a plug-in for, say, Photoshop Can still use all of Photoshops tools on your images Powerful method for innovation, leveraging existing code to drive new ideas

11 Under the Hood of Polymorphism CS-2303, C-Term 201011 Questions?

12 Under the Hood of Polymorphism CS-2303, C-Term 201012


Download ppt "Under the Hood of Polymorphism CS-2303, C-Term 20101 Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from."

Similar presentations


Ads by Google