C++ Virtual Function
A virtual function is a function that is declared as virtual in a base class and is redefined in the derived class.
To make a function as virtual declared using the virtual keyword.
When you refer parent class object using the pointer or any reference to the subclass, you can call or invoke the virtual function for the object and execute the derived class of the function. It is used to tell the compiler to perform the late binding or dynamic linking on the function.

Syntax:
virtual return_type function_name() { // body of the function }
Example:
#include <iostream>
using namespace std;
class A // base class
{
public:
virtual void show() //virtual function
{
cout << "Base class Function" << endl;
}
};
class B: public A // derived class
{
public:
void show()
{
cout << "Derived class Function" << endl;
}
};
int main() {
B b;
A *a;
a=&b;
a->show();
return 0;
}
Pure Virtual Function
A Pure virtual function is virtual function with no body of the function. In this situation, the base class object not declared and derived class should declare the function. A Pure Virtual Function can be declared by equating it to zero.
virtual return_type function_name()=0;
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.