Pure Virtual Function in C++:
Pure virtual function is the virtual function in the class without body of the function or definition of the function
Like : void virtual display()=0; in your class
There is no difference between virtual function or pure virtual function. You try to observe the output of the pure virtual function. The output of virtual and pure virtual function is same.
The class which contains even one pure virtual function that class can not be instatiated or we can not create object of that class.
The class which contains pure virtual function is know as Abstract class
Program of Pure Virtual Function in C++
//pure virtual function C++
#include<iostream>
using namespace std;
class Base
{
public:
void fun1()
{
cout<<endl<<"I am fun1 of Base class";
}
void fun2()
{
cout<<endl<<"I am fun2 of Base class";
}
//following is the code to create pure virtual function
virtual void display()=0;
};//end of Base class
class Derived : public Base
{
public:
void fun3()
{
cout<<endl<<"I am fun3 of derived class";
}
void fun4()
{
cout<<endl<<"I am fun4 of derived class";
}
void display()
{
cout<<endl<<"I am display of Derived class";
}
};//end of Derived class
int main()
{
Base *bptr;//object and pointer to Base class
Derived d;//object of derived class
bptr=&d;//pointing to derived class
bptr->fun1();// is the function of Base class
bptr->fun2();// is the function of Base class
bptr->display();
}//end of main
Output:
I am fun1 of Base classI am fun2 of Base class
I am display of Derived class