Pure Virtual Function in C++ With Example Program
What is a Virtual Function?
A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual function is created in a class, all its derived classes must include the same virtual function. In a virtual function, we don’t need to write any definition. Instead, we only have to declare it. Moreover, we need to assign 0 during the declaration part. If a class has a virtual function, then the class is called the abstract class.
Syntax
The syntax for virtual function:
Virtual function name;
Example for Virtual Function:
Example:1
//The code is written to demonstrate the use of the virtual class
//We are using the input and output operators in the code, so we have to include the iostream function.
#include<iostream>
using namespace std;
//Creating a class using the class keyword
class Base
{
int x;
public:
//Creating a virtual function using the virtual keyword.
virtual void fun() = 0;
int getX() { return x; }
};
// Creating a derived class. This class inherits from Base and implements fun()
class Derived: public Base
{
int y;
public:
//Declaring the virtual function inside the derived class.
void fun() { cout << "fun() called"; }
};
int main(void)
{
//Creating an object of the derived class.
Derived d;
d.fun();
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the use of the virtual function. We have created a base class in which we have created a virtual function. Then we created a derived class that will inherit the properties of the base class. We have declared the virtual function inside the derived class and included it in the cout statement. In the main function, we created an object for the derived class and then called the virtual function.
Example-2
// This code is used to demonstrate the use of the virtual function inside a class
//We have included the iostream header file for using the input and output operators.
#include <iostream>
using namespace std;
// Creating a class using the class keyword
class Shape {
protected:
float dimension;
public:
//Creating a function member.
void getDimension() {
cin >> dimension;
}
// Creating a virtual method
virtual float calculateArea() = 0;
};
// Inheriting the properties of the shape class inside the square class.
class Square: public Shape {
public:
//using the virtual function created in the parent class.
float calculateArea() {
return dimension * dimension;
}
};
// Inheriting the properties of the shape class inside the square class.
class Circle: public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};
//Creating the main function.
int main() {
//Creating an object for the class square
Square square;
//Creating an object for the class circle.
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the use of the virtual function inside a class. We have created a class shape in which we have created a protected variable dimension. In the public part of the code, we have created a function member get dimension and a virtual function using the virtual keyword inside the same class. As we know that the virtual function will not have any definition, we have given the value 0 to the virtual function. We created a class square and then inherited the properties of the shape class. In the square class, we have declared the virtual function created in the main class, which will return the area of the square. Then we also created another class circle in which we inherited the main class's properties. According to the virtual class properties, we have declared the virtual function in the circle class, which will return the area of the circle. In the main function, we have created the object for the square and circle classes. Then using the object name, we called the virtual function, which we have declared in the derived classes. Using the cout statement, printed the values of the square and the circle area.