Saturday, 2 October 2010

PURE VIRTUAL FUNCTION



PURE VIRTUAL FUNCTION
A pure virtual function is a virtual function declared in the base class, but there is no definition for the virtual function, so that it can be overridden by the derived classes. When a function is declared as virtual function in a class, then that class cannot be used to declare any object because it becomes an abstract class.
SYNTAX:
virtual return-type function-name (args) =0;
Example:

class Base
{
public:
virtual void greet()=0;   // pure virtual function
};

class derived: public Base
{
public:
void greet()
{
cout<<”I am derived class”;
}
};

void main()
{
derived Obj;

Obj.greet();
}
Output:
Hi! I am derived class



No comments:

Post a Comment