Pure Virtual Function in C++ with Example Program

Pure Virtual Function in C++:

It declares a function virtual inside the base class and redefines it in the derived classes. The function inside the base class is seldom used for performing any task.

Example:

virtual void display()=0;

such functions are called Pure Virtual Functions. It only serves as a placeholder. It is also called the “do-nothing” function.

A pure virtual function is a function declared in a base class that has no definition relative to the base class.

Program:

#include<iostream.h>
#include<conio.h>
using namespace std;
class simple
{
public:
virtual void example()=0;
};
class test: public simple
{
public:
void example()
{
cout<<"C is a Procedural Programming Languages";
}
};
class temp: public simple
{
public:
void example()
{
cout<<"C++ is an Object-Oriented Programming language"; 
} 
}; 
void main() 
{ 
Exforsys* arra[2]; test t1; temp t2; arr[0]=&t1; arr[1]=&t2; arr[0]->example();
arr[1]->example();
getch();
}

 

Output:
C is a Procedural Programming Languages
C++ is an Object-Oriented Programming language