virtual function in C++

A virtual function is a member function that is declared in the base class using the keyword virtual and is overridden in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime. Virtual function is runtime polymorphism.
* A virtual function is always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override the virtual function, in that case, the base class version of the function is used.
* The prototype of virtual functions should be the same in the base as well as derived class.
* Virtual functions should be accessed using pointer or reference of base class type to achieve runtime polymorphism.
* A virtual function can be private and still can be overridden by the derived class. 
* A virtual function call uses the default arguments determined by the static type of the pointer or reference. An overriding function in a derived class does not inherent default arguments from the function it overrides.
* A class may have virtual destructor but it cannot have a virtual constructor.
* A virtual function can be a friend function of another class.
* A virtual function cannot be static.
```
#include <iostream>
using namespace std;

class Base {
public:
    virtual void func(int a=1) { cout << "Base::func():" << a << endl; }
    Base() { func(); }  // Base::func():1
    void call() { func(); }
};

class Sub : public Base {
public:
    void func(int a=2) { cout << "Sub::func():" << a << endl; }
    Sub() { func(); } // Sub::func():2
    void call() { func(); }
};

int main() {
    Base * b = new Sub();

    b->Base::call(); // Sub::func():1
    b->call();  //  Sub::func():1
    delete b;
}
/*
Base:func():1
Sub:func():2
Sub:func():1
Sub:func():1
*/
```