What Then Is Function Overriding?

 < Day Day Up > 



Good question. The difference between simply hiding base class functions and overriding base class functions is the invocation of the virtual calling mechanism via a base class pointer when using virtual functions. As you saw in figure 13-9, when the getI() function is called via the Foo base class pointer, the base class version of the method is invoked even though the base class pointer contains the address of a derived class object. Therefore, DerivedFoo getI() did not override Foo getI(). An overriding function will be called via a base class pointer whereas a function that simply hides the name of a base class function will not. That is the difference! Understanding this difference is the key to understanding the heart and soul of object-oriented programming.

When you override a virtual base class function in a derived class and call the function via a base class pointer you achieve polymorphic behavior. Simple function name hiding does not achieve polymorphic behavior.

Now, let me show you how to achieve polymorphic behavior using virtual functions and function overriding.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net