Class which contains virtual functions are called abstract classes.Virtual fuctions have same name and are declared in, base class and also 'n' number of derived classes.
'Virtual' is a keyword preceded before any fuction to make it as a virtual function.
Virtual function equated to 0 is called ' pure virtual function' .
Class shape
{
public:
Virtual void area()=0;
};
Class shape : public rectangle , public triangle ;
Here class ' shape ' is base class.Where as rectangle and triangle are derived classes.
So, area is a virtual function that are redefined in derived classes.
class rectangle
{
Public:
Void area()
{
area=base * height ;
Cout<<area <<endl ;
}
};
Class triangle
{
Public:
Void area()
{
area= (base*height) /2;
Cout<<area<<endl;
}
};
In the above example both classes have the function named area.
Rectangle rect.area(); will refer to the function redefined in class rectangle.
Triangle tri.area(); will refer to the function redefined in class triangle.
Hence , this is only possible when you have declared area () in base class as 'virtual'.