Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The key to understanding the practical usage of virtual functions is to keep in mind that an object of a certain class can be assigned another object of a class derived from the first object's class.</p> <p>E.g.: </p> <pre><code>class Animal { public void eat() {...} } class FlyingAnimal : Animal { public void eat() {...} } Animal a = new FlyingAnimal(); </code></pre> <p>The <code>Animal</code> class has a function <code>eat()</code> that generally describes how an animal should eat (e.g. put the object in mouth and swallow). </p> <p>However, the <code>FlyingAnimal</code> class should define a new <code>eat()</code> method, because flying animals have a particular way of eating.</p> <p>So the question that comes to mind here is: after I declared the variable <code>a</code> of type <code>Animal</code> and asigned it a new object of type <code>FlyingAnimal</code>, what will <code>a.eat()</code> do? Which of the two methods is called?</p> <p>The answer here is: because <code>a</code> is of type <code>Animal</code>, it will call <code>Animal</code>'s method. The compiler is dumb and doesn't know that you are going to assign an object of another class to the <code>a</code> variable.</p> <p>Here is where the <code>virtual</code> keyword comes in action: if you declare the method as <code>virtual void eat() {...}</code>, you are basically telling the compiler "be careful that I am doing some clever stuff here that you cannot handle because you're not as smart". So the compiler will not attempt to link the call <code>a.eat()</code> to either of the two methods, but instead it tells the system to do it <strong>at runtime</strong>!</p> <p>So only when the code executes, the system will look at <code>a</code>'s <strong>content type</strong> not at its declared type and executes <code>FlyingAnimal</code>'s method.</p> <p>You may wonder: why the hell would I want to do that? Why not say right from the start <code>FlyingAnimal a = new FlyingAnimal()</code> ?</p> <p>The reason for that is that, for example, you may have many derived classes from <code>Animal</code>: <code>FlyingAnimal</code>, <code>SwimmingAnimal</code>, <code>BigAnimal</code>, <code>WhiteDog</code> etc. And at one point you want to define a world containing many <code>Animal</code>s, so you say:</p> <pre><code>Animal[] happy_friends = new Animal[100]; </code></pre> <p>We have a world with 100 happy animals. You initialize them at some point:</p> <pre><code>... happy_friends[2] = new AngryFish(); ... happy_friends[10] = new LoudSnake(); ... </code></pre> <p>And at the end of the day, you want everybody to eat before they go to sleep. So you want to say:</p> <pre><code>for (int i=0; i&lt;100; i++) { happy_friends[i].eat(); } </code></pre> <p>So as you can see, each animal has its own method of eating. Only by using <strong>virtual</strong> functions can you achieve this functionality. Otherwise, everyone would be forced to "eat" in the exact same way: as described in the most general <code>eat</code> function inside the <code>Animal</code> class.</p> <p>EDIT: This behavior is actually <strong>default</strong> in common high-level languages like Java.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload