VIRTUAL CLASSES AND PURE VIRTUAL FUNCTIONS
Classes two types:
1.Abstract classes 2. Concrete classes
Abstract class.
A class whose object can not be instantiated is called abstract class.
e.g. shape class.
Concrete class.
A class whose object can be instantiated is called concrete class.
A class can be made abstract class by adding pure virtual function.
A pure virtual function is one with the expression =0 added to the declaration.
#include <iostream>
using namespace std;
class base
{
public:
virtual void show()=0;
};
class derive1:public base
{
public:
void show()
{
cout<<"derive1 ";
}
};
class derive2:public base
{
void show()
{
cout<<"derive2 ";
}
};
void main ()
{
base* ptr;
derive1 d1;
derive2 d2;
ptr = &d1;
ptr->show( );
ptr = &d2;
ptr->show( );
}
Sample run is derive1 derive2
Here the PURE VIRTUAL FUNCTION IS
virtual void show( )= 0;