• Register
57,980 points
11 5 5

Want to solve programming problems and get paid for it? If that sounds interesting to you then contact us.

The virtual function and the pure virtual function are run-time polymorphism concept. The main difference between 'virtual function' and 'virtual function are discussed below.

 

The virtual function and the pure virtual function are concepts of run-time polymorphism. The main difference between ' virtual function' and 'Pure virtual function' is that the 'virtual function' has its definition in the base class and is also redefined by inheriting derived classes. The pure virtual function has no definition in the base class, and all inherited derived classes have to redefine it.

However, the virtual function is also called as dynamic dispatch and run-time dispatch, because the called function is specified at run time according to the type of object used.

Polymorphism is compatible with the C ++ and Java languages. In Java, the term "override" is used instead of "virtual function", since the virtual function is the C ++ term.

 
Basis for comparisonVirtual functionPure virtual function
BASICThe virtual function has its definition in the base class.The pure virtual function has no definition in the base class.
Statementvirtual_function_name (parameter_list) {. . . . .};virtual_function_name (parameter_list) = 0;
Derived classAll derived classes may or may not override the virtual function of the base class.All derived classes must override the virtual function of the base class.
Effect
 
Virtual functions are hierarchical in nature; it does not affect compilation if some derived class does not override the virtual function of the base class.If all derived classes cannot override the virtual function of the base class, the compilation error will occur.
Abstract classNo conceptIf a class contains at least one pure virtual function, it is declared abstract.

 

Definition of virtual function 

The virtual function is the member function of the base class, and it is redefined by the derived classes that inherit the base class. Not all inherited derived classes need to redefine the virtual function, only derived classes that may require their operation. A virtual function is created by declaring the function in the base class preceded by the keyword 'virtual'.

 

Statement:

class base {public: virtual type funt_name (list of parameters) {. . . }};

Inherited derived classes can redefine the virtual function without any "virtual" keywords. Derived classes redefine the virtual function to accomplish their task. Since the virtual function is redefined in derived classes, we have multiple forms of the same function. Now which version of the function is called depends on the type of object that is invoked to invoke that function.

 

Multi-level inheritance 

In multilevel inheritance, where a derived class that has inherited the virtual function from its base class, when used as a base class for another derived class, the virtual function can still be overridden. So when a virtual function is received, its virtual reality is also inherited.

Virtual functions are more hierarchical in nature, that is, if an assumed class does not override/redefine the virtual purpose inherited from the base class and when the derived class object requests that virtual function, the virtual function defined by the class is invoked base.

 

Definition of pure virtual function 

As seen above, if the derived class does not override the virtual function, then the virtual function defined by the base class is used. But what if the base class itself doesn't define the virtual function? Many times the base class doesn't have a definition for the virtual function, or sometimes you want all derived classes to override the virtual function.

To handle these two previous situations, C ++ supports the concept of " Pure virtual function ". A "pure virtual function" is declared in the base class but does not have its definition in the base class. The pure virtual function is declared as follows.

virtual type function_name (parameter_list) = 0;

As long as a virtual function in the base class is made "pure", each derived class must override the pure virtual function of the base class. If the derived class cannot override the pure virtual function of the base class, it will result in a compilation error.

 

Abstract class 

The class that contains at least one pure function is called an "abstract class". You cannot create abstract class objects, but you can create references and pointers to abstract classes. Members of abstract classes can be accessed through the derived classes object that the abstract base class inherits.

A class you want to declare abstract, use the 'abstract' keyword in front of the 'class' keyword.

// for example, abstract class class-name {. . virtual type function_name (parameter_list) = 0; . . };

  1. Virtual functions are definitely defined in the base class and redefined (overridden) in the derived class. On the other hand, the pure virtual function of the base class is not particularly defined in the base class
  2. If necessary, the derived class redefines (overrides) the virtual function, whereas, in the case of the pure virtual function, the derived class must definitely redefine the pure virtual function.
  3. If the derived class cannot redefine (override) the virtual function, you can use the virtual function of the base class. Conversely, if a derived class cannot override the pure virtual function, a compilation error occurs.
  4. The base class that contains the virtual function can be instantiated, that is, its object can be created. In contrast, the base class that contains a pure virtual function, that is, an abstract class cannot be instantiated since an abstract class is not fully defined.

Note:
The prototype of the "virtual function" and the "pure virtual function" remains the same throughout the program.

 

Conclusion:

The 'virtual functions' and the 'pure virtual function' have their importance, as in the 'virtual functions' all derived classes do not need to redefine the virtual function and where we want all derived classes to redefine the virtual, pure virtual function The function is best applied there.

57,980 points
11 5 5