Polymorphism and Virtualization


Multipath Inheritance Ambiguity

  • Let's suppose the case of the class 'Child' that inherits directly from two different classes 'Parent1' and 'Parent2'. And 'Parent'1 and 'Parent2' in turn inherit from the same class 'Grandparent'. It is said that 'Grandparent' class is indirect base class of 'Child'. This pose an ambiguity problem, because all the private and public members of 'Grandparent' are inherited to 'Child' twice!.
  • Ambiguity is avoided by declaring 'Grandparent' (the common 'ancestor' class) as a virtual class.

    Run Time vs. Compile Time Polymorphism

  • Compile Time Polymorphism is achieved by overloading functions and operators.
  • Run Time Polymorphism is achieved by using inheritance and virtual functions.
  • A virtual function is a member function of a class defined with the keyword virtual and its functionality can be over-rided in that class derived classes.
  • Virtual functions are used to give different meanings to a function, and this is part of the concept of polymorphism (different meanings = many (poly) forms (morfism) )
  • If a function is designated virtual in the base class then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

    Dynamic Binding vs. Static Binding

  • Virtual Functions are resolved during run-time or dynamic binding. The main difference between non-virtual and virtual is in the way they are resolved:
    • Non-virtual are resolved during compile time = static binding
    • Virtual are resolved during run-time = dynamic binding = late binding.

    Why do we need Virtual Functions?

  • It's because sometimes derived classes would do something not exactly in the same way their parents did.
  • For example, if we have the virtual function make in the base class Vehicle, derived classes FourWheeler and Sedan would make a car in different ways: FourWheeler will use all-terrain wheels and special 4x4 transmission and Sedan will use automatic transmission and normal tires.
  • If we create a dynamic list of generic Vehicles we may add to this list both FourWheeler and Sedan vehicles to that list, and when we want to make all the items on that dynamic list, we should only need to walk it calling the member make for each object in the list, and "automatically" each Vehicle in the list will execute its own make version, so Fourwheelers will have 4x4 transmissions and Sedans will have automatic transmissions.

    Code Example

    using namespace std;
    // Base class for C++ virtual function example
    class Window
        virtual void Create() // virtual function for C++ virtual function example
            cout << "Base class Window" << endl;
    class CommandButton : public Window
        void Create()
            cout << "Derived class Command Button - Overridden C++ virtual function" << endl;
    void main()
        Window *x, *y;
        x = new Window();
        y = new CommandButton();
  • The output will be:

    Base class Window
    Derived class Command Button - Overridden C++ virtual function