Under the Hood of Polymorphism CS-2303, C-Term 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)
Under the Hood of Polymorphism CS-2303, C-Term 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.
Under the Hood of Polymorphism CS-2303, C-Term 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 © by Pearson Education, Inc. All Rights Reserved. Compiling Virtual Functions
Under the Hood of Polymorphism CS-2303, C-Term © by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig Pointer to object (level 1)
Under the Hood of Polymorphism CS-2303, C-Term © by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig Pointer to vtable (stored in every object – level 2)
Under the Hood of Polymorphism CS-2303, C-Term © by Pearson Education, Inc. All Rights Reserved. Deitel & Deitel, Fig Index into vtable to access method (level 3)
Under the Hood of Polymorphism CS-2303, C-Term Performance Impact Nominal Must access two additional pointers to get to method Versus direct access for non- virtual methods
Under the Hood of Polymorphism CS-2303, C-Term 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
Under the Hood of Polymorphism CS-2303, C-Term 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). © by Pearson Education, Inc. All Rights Reserved.
Under the Hood of Polymorphism CS-2303, C-Term 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
Under the Hood of Polymorphism CS-2303, C-Term Questions?
Under the Hood of Polymorphism CS-2303, C-Term