OOPortal OOPortal


Inheritance/Polymorphism  «Prev 

Virtual function selection

A virtual function and its derived instances that have the same signature must have the same return type. The virtual function redefinition is called overriding. Notice that nonvirtual member functions with the same signature can have different return types in derived classes. The return type rule is undergoing reconsideration by the ANSI standards committee. Namely, many compilers require that virtual functions are overridden using the same return type. This restriction is relaxed where a derived class replaces the base class as the return type.

C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.
The whole function body can be replaced with a new implementation in the derived class.
C++ Virtual Function - Properties:
C++ virtual function is,
  1. A member function of a class
  2. Declared with virtual keyword
  3. Usually has a different functionality in the derived class
  4. A function call is resolved at run-time

The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. However, the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.

Virtual Functions

When you specify a function as virtual in a base class, you indicate to the compiler that you want dynamic binding for function calls in any class that is derived from this base class.
A virtual function is declared in a base class by using the keyword virtual, as shown in Figure 4-11
Figure 4-11: Calling a virtual function

A function that you specify as virtual in a base class will be virtual in all classes that are directly or indirectly derived from the base. This is the case whether or not you specify the function as virtual in a derived class. To obtain polymorphic behavior, each derived class may implement its own version of the virtual function.
You make virtual function calls using a variable whose type is a pointer or a reference to a base class object.
Figure 4-11 illustrates how a call to a virtual function through a pointer is resolved dynamically. The pointer to the base class type is used to store the address of an object with a type corresponding to one of the derived classes. It could point to an object of any of the three derived classes shown or, of course, to a base class object. The type of the object to which the pointer points when the call executes determines which volume() function is called. Describing a class as polymorphic means that it is a derived class that contains at least one virtual function. Note that a call to a virtual function using an object is always resolved statically. You only get dynamic resolution of calls to virtual functions through a pointer or a reference. Storing an object of a derived class type in a variable of a base type will result in the derived class object being sliced, so it has not derived class characteristics.
With that said, let us give virtual functions a whirl. To make the previous example work as it should, a very small change to the Box class is required. I just need to add the virtual keyword to the definition of the volume() function

class Box
{
// Rest of the class as before...
public:
// Function to calculate the volume of a Box object
virtual double volume() const { return length*width*height; }
};