Current post is on what does 'Pure Virtual Function' mean?.
What is pure virtual function ?
A pure virtual function is a virtual function which will force derived classes to override. If a class has any pure virtual funtion(s), the class is called an "abstract class" and you can't create objects of that class.
Declaration Syntax:
return_value classname:: Func_Name( parameters ) = 0;
Example:
class AbstractBaseClass
{
void ThisIsAPureVirtualFunction(void) = 0;
};
Why it is required?
- To specify an interface that must be provided by all classes deriving from it
- To make a class an abstract base class so that it cannot be instantiated
Can they have definition (method body)?
- Yes, they may have it.
1) If a class needs to be abstract & has no suitable candidate for virtual function, then my making destructor pure virtual & providing the definition, class is made virtual and derived class will have no issue in calling delete function
2) If a class need to provide a default behaviour but not let derived classes just inherit it "silently".
Appreciate your feedback via comments. Thanks.
No comments:
Post a Comment