Pure Virtual Function In CPP

A pure virtual function is a virtual function which has no definition in the base class.  The definition for a pure virtual function must be there in the derived class because of compile-time error.

A pure virtual function is initialized by zero. The class in which pure virtual function is there is known as abstract class.

An abstract class is a class which contains at least one pure virtual function.

#include<iostream.h>
#include<conio.h>

class A
{
public:
virtual void show()=0;
void fun()
{
cout << "fun called" << endl;
}
virtual void disp()
{
cout << "A disp called" << endl;
}
};
class B:public A
{
public:
void show()
{
cout << "show called" << endl;
}
void disp()
{
A::disp();
cout << "B disp called ";
}
};
void main()
{
A *ptr;
clrscr();
B b1;
ptr=&b1;
ptr->show();
ptr->fun();
ptr->disp();
getch();
}

Output:
show called
fun called
A disp called
B disp called

 

Leave a Reply