Then, why do we need virtual function? The key to understand 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. E.g.:
class Animal{
public void eat(){...}
}
class FlyingAnimal:Animal{
public void eat(){...}
}
Animal a = new FlyingAnimal();
The Animal
class has a function eat()
that generally describes how an animal should eat (e.g. put the object in mouth and swallow).
However, the FlyingAnimal
class should define a new eat()
method, because flying animals have a particular way of eating.
So the question that comes to mind here is: after I declared the variable a
of type Animal
and assigned it a new object of type FlyingAnimal
, what will a.eat()
do? Which of the two methods is called?
The answer here is: because a
is of type Animal
, it will call Animal
's method. The compiler is dumb and doesn't know that you are going to assign an object of another class to the a
variable.
Here is where the virtual
keyword comes in action: if you declare the method as virtual void eat() {...}
, 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 a.eat()
to either of the two methods, but instead it tells the system to do it at runtime!
So only when the code executes, the system will look at a
's content type not at its declared type and executes FlyingAnimal
's method.
You may wonder: why the hell would I want to do that? Why not say right from the start ? like:
FlyingAnimal a = new FlyingAnimal()
The reason for that is that, for example, you may have many derived classes from
Animal
: FlyingAnimal
, SwimmingAnimal
, BigAnimal
, WhiteDog
etc. And at one point you want to define a world containing many Animal
s, so you say:
Animal[] happy_friends = new Animal[100];
We have a world with 100 happy animals. You initialize them at some point:
...
happy_friends[2] = new AngryFish();
happy_friends[10]= new LoudSnake();
And at the end of the day, you want everybody to eat before they go to sleep. So you want to say:
for(int i=0; i<100; i++){
happy_friend[i].eat();
}
So as you can see, each animal has its own method of eating. Only by using virtual functions can you achieve this functionality. Otherwise, everyone would be forced to "eat" in the exact same way: as described in the most general eat
function inside the Animal
class.