Lecture overview -- Keyboard shortcut: 'u'  Previous page: Member access control: private, public and protected -- Keyboard shortcut: 'p'  Next page: Destructors and Inheritance - Virtual Destructors -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 4 - Page 5 : 24
Notes about C++
Abstraction Mechanisms, Part 2
Virtual Functions

A virtual member function in C++ works as expected:

The dynamic type of the receiver controls which function to call.

Destructors may also be virtual, §12.4.2.

  • The C++ Programming Language: Page 310
 

  • Conditions for getting polymophic behavior in C++:   obj.f()

    • A virtual member function f must be used on an object obj

    • The object obj must be accessed via a pointer or a reference

  • Avoiding polymorphic behavior with a virtual function f

    • Activate f with a scope operation: C::f(...)

To see this image you must download and install the SVG plugin from Adobe.In Firefox please consultthis page.

The class B is a specialization of class A

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/virtual-member-functions/virtual-1-simplified.ccVirtual versus non-virtual functions in an AB class hierarchy.


y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/virtual-member-functions/virtual-1-simplified-outputProgram output.