Tech Guru Said..
In C++ a virtual function in class that is initialized to zero (0) is referred as a pure virtual function. It has no body and hence also known as do-nothing or the dummy function.
Example: virtual void show()=0;
An important thing to note about pure virtual functions is that these functions must be overridden in all the derived classes otherwise it would result in a compilation error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.
A class containing one or more pure virtual functions is called an Abstract class, which means an instance of such class can't be created (but pointer to that class can be created).
Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.
1. Pure virtual function has no function body.
2. Overriding is must in pure virtual funciton. (Must)
3. It is define as: virtual int myfunction() = 0;
4. A class is "abstract class" when it has at least one pure virtual function.
5. You can’t create instance of "abstract class", rather you have to inherit the "abstract class" and override all pure virtual function.
Sample program:
class alpha
{
public:
virtual void show()=0; //pure virtual function
};
class beta:public alpha
{
public:
void show() //overriding
{
cout<<"OOP in C++";
}
};
void main()
{
alpha *p;
beta b;
p=&b;
p->show();
}
Output: OOP in C++