Tuesday, February 9, 2010

What do you mean by 'Pure Virtual Function' in C++ \ VC++?

Current post is on what does 'Pure Virtual Function' mean?.


Lets examine:

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

Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.