page_title: Virtual function
section_title: Example
hierarchical_section_title: Virtual function / Example
caption_attribution_description: English: The is a class diagram of C++ programming. It was created by my programming tool (Visual Studio 2008) in July 30, 2009.
context_page_description: In object-oriented programming, in languages such as C++, and Object Pascal, 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 polymorphism portion of object-oriented programming. In short, a virtual function defines a target function to be executed, but the target might not be known at compile time.
  Most programming languages, such as Java, PHP and Python, treat all methods as virtual by default and do not provide a modifier to change this behavior. However, some of them provide modifiers to avoid methods to be overridden by derived classes.
context_section_description: 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.
  class Animal {
   public:
    // Intentionally not virtual:
    void Move(void) {
      std::cout << "This animal moves in some way" << std::endl;
    }
    virtual void Eat(void) = 0;};// The class "Animal" may possess a definition for Eat if desired.class Llama : public Animal {
   public:
    // The non virtual function Move is inherited but not overridden.
    void Eat(void) override {
      std::cout << "Llamas eat grass!" << std::endl;
    }};
  This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling Eat), without needing to know what kind of animal may be in the list, how each animal eats, or what the complete set of possible animal types might be.
  We can better see how virtual functions work by implementing the above example in C
  #include <stdio.h>/* an object points to its class... */struct Animal {
      const struct AnimalClass * class;};/* which contains the virtual function Animal.Eat */struct AnimalClass {
      void (*Eat)(struct Animal *); // 'virtual' function };/* Since Animal.Move is not a virtual function   it is not in the structure above. */void Move(struct Animal * self){
      printf("<Animal at %p> moved in some way\n", (void *) self);}/* unlike Move, which executes Animal.Move directly,   Eat cannot know which function (if any) to call at compile time.   Animal.Eat can only be resolved at run time when Eat is called. */void Eat(struct Animal * self){
      const struct AnimalClass * class = *(const void **) self;
      if (class->Eat) 
          class->Eat(self); // execute Animal.Eat
      else
          fprintf(stderr, "Eat not implemented\n");}/* implementation of Llama.Eat this is the target function    to be called by 'void Eat(struct Animal *).' */static void _Llama_eat(struct Animal * self){
      printf("<Llama at %p> Llama's eat grass!\n", (void *) self);    
  }/* initialize class */const struct AnimalClass Animal = {(void *) 0}; // base class does not implement Animal.Eatconst struct AnimalClass Llama = {_Llama_eat};  // but the derived class doesint main(void){
     /* init objects as instance of its class */
     struct Animal animal = {& Animal};
     struct Animal llama = {& Llama};
     Move(& animal); // Animal.Move
     Move(& llama);  // Llama.Move
     Eat(& animal);  // cannot resolve Animal.Eat so print "Not Implemented" to stderr
     Eat(& llama);   // resolves Llama.Eat and executes}
caption: Class Diagram of Animal
exif: {}