Virtual Method
C++ virtual Method also known as Virtual Function is a member function declared inside base class that we can redefine(override) in a derived class. In order to declare virtual method we need to use Virtual keyword before the method. Virtual function will only work when we are applying upcasting concept to it. When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.
Before entering coding part, we need to know a concept called Method overriding. If a child class has the same method as declared in the parent class, it is known as Method Overriding in c++. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
CODE
EG 1 . Creating 2 classes with a virtual function in the parent class
#include <iostream>
using namespace std;
class BaseClass {
  public:
    virtual void m1() {
        cout << "Base class method invoked" << endl;
    }
};
class DerivedClass : public BaseClass {
  public:
    void m1() {
        cout << "Derived class method invoked" << endl;
    }
};
int main() {
    //UPCASTING
    BaseClass* obj1 = new DerivedClass();
    obj1->m1();
}
(Click Here, For more exercises)