PDA

View Full Version : What is virtual function on C++?



shreeharisg
02-18-2014, 09:57 PM
What is virtual function on C++? Give any example of it.

michral
06-01-2017, 10:09 AM
In languages such as C++, a virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated. This concept is an important part of the polymorphic portion of object-oriented programming.
virtual methods are declared by prepending the virtual keyword to the function's declaration in the base class. This modifier is inherited by all implementations of that method in derived classes, meaning that they can continue to over-ride each other and be late-bound. And even if methods owned by the base class call the virtual method, they will instead be calling the derived method.
For example, a base class Animal could have a virtual function eat. Subclass Llama would implement eat() differently than subclass Wolf, but one can invoke eat() on any class instance referred to as Animal, and get the eat() behavior of the specific subclass.

Server9host
06-02-2017, 11:52 AM
Virtual functions permit a program to decision strategies that do not essentially even exist at the instant the code is compiled.In object-oriented programming, in languages like C++, a virtual function is an Associate avoidable technique that dynamic dispatch is accelerated.

jackwilliam
07-26-2017, 04:06 PM
A virtual function makes its class a polymorphic base class. Derived classes ... What is the difference between a virtual function and pure virtual function in C++?.