### Virtual Functions

In C++, dynamic binding is supported by the mechanism of virtual functions. Virtual functions must be members of a class. Use virtual functions when you expect a class to be used as a base class in a derivation and when the implementation of the function may be overridden in the derived class. You can declare a member function with the keyword virtual in its class declaration. For example:

    class B
    {
          int a,b,c;
    public:
          virtual int f();
    };

You can reimplement a virtual member function, like any member function, in any derived class. The implementation that is executed when you make a call to a virtual function depends on the type of the object for which it is called. If a virtual member function is called for a derived class object and the function is redefined in the derived class, the definition in the derived class is executed. In this case, the redefined derived class function is said to override the base class function. This occurs even if the access to the function is through a pointer or reference to the base class.

If you call a virtual function with a pointer that has base class type but points to a derived class object , the member function of the derived class is called. However, if you call a nonvirtual function with a pointer that has base class type, the member function of the base class is called regardless of whether the pointer points to a derived class object.

If the argument types or the number of arguments of the two functions are different, the functions are considered different, and the function in the derived class does not override the function in the base class. The function in the derived class hides the function in the base class.

The return type of an overriding virtual function can differ from the return type of the overridden virtual function provided that:

- The overridden function returns a pointer or a reference to a class T, and
- The overriding virtual function returns a pointer or a reference to a class derived from T.

An error does result when a virtual function that returns D* overrides a virtual function that returns B* where B is an ambiguous base class of D. The reason is that two or more instances of class B will exist within class D, and the compiler will not know which base B to return. For more information, see Function Return Values.

A virtual function cannot be global or static because, by definition , a virtual function is a member function of a base class and relies on a specific object to determine which implementation of the function is called. You can declare a virtual function to be a friend of another class.

If a function is declared virtual in its base class , it can still be accessed directly using the :: ( scope resolution) operator. In this case, the virtual function call mechanism is suppressed and the function implementation defined in the base class is used. In addition, if you do not redefine a virtual member function in a derived class, a call to that function uses the function implementation defined in the base class.

A virtual function must be one of the following:

- Defined
- Declared pure
- Defined and declared pure

A base class containing one or more pure virtual member functions is called an abstract class.

Inheritance Overview

Example of Overriding Virtual Functions

Virtual Base Classes
Ambiguous Virtual Function Calls
Virtual Function Access
Abstract Classes
Function Return Values
Friends
Scope Resolution Operator
Derivation