Tuesday, 21 April 2015

Pure Virtual functions

Virtual functions defined inside the base class normally serve as a framework for future design of the class hierarchy, these functions can be overridden by the methods in the derived classes.

In most of the cases, the pure virtual functions are defined with the null-body, it has no definition. Such functions in the base class are similar to do-nothing or dummy functions and in C++, they are called pure virutal functions. The syntax for defining the pure virtual function is

Virtual return_type function_name(arguments) = 0;

? A pure virtual function declared in a base class has no implementation as far as the
  base class is concerned.
? The classes derived from a base class having a pure virtual function have to define
   such a function or redeclare it as a pure virtual function.
? A class containing pure virtual functions cannot be used to define any objects of its
   own and hence such classes are called pure abstract classes or abstract classes.
? Where all other classes without pure virtual functions and which are instantiated are
   called as concrete classes.

Properties of Pure virtual function

1. A pure virtual function has no implementation in the base class hence, a class with
    pure virtual function cannot be instantiated.
2. It acts as an empty buc ket that the derived class is supposed to fill.
3. A pure virtual member function can be invoked by its derived class.

No comments:

Post a Comment