what is a virtual function? why do we need virtual functions?

Virtual function: it is used to achieve dynamic (run time) polymorphism. When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual reseeding it's normal declaration. 

When a function is made virtual, c++ determines which function to use at runtime based on the type of object pointed to by the base pointer, rather than the type of the pointer. Thus by making the base pointer to point to different object, we can exclude different versions of the virtual functions.