The word virtual is strange enough on its own. It is defined philosophically as "that which is not real" but contains all of the properties of the real object. However, ultimately something virtual is "fake." Virtual functions are similar in concept, so follow along with me through this article to discover how to use them and when to do so in the C++ language.
This is the second part of my series covering the polymorphic traits in C++ and object-oriented programming, so if you missed it, make sure to catch up with the first part. This article will pick up from where we took a break last time. Just look for Polymorphism in C++ on this web site; start a search or simply click on my name to see my profile and get the link from there.
Now then, if you managed to follow the first part of this article series, welcome back. We learned what the virtual function itself is, and what direct effect it will have on functions where you declare them with the magic keyword in front of them. To put it as simply as possible, from the point of view of working with the functions where no pointer is implicated, it has no effect whatsoever.
However, when you assign a derived object to the base pointer, whenever you call a virtual function of the base object, if that function was redefined (what in fact in this case is an overwrite) inside the derived class, the derived class function will be called. While we go through this article, you will learn about abstract classes, pure virtual functions, a method for implementing polymorphic behavior, and ultimately why it is so important for destructors to be virtual.
We have much to observe and learn, so here we go. Expanding on the knowledge you amassed last week, you will find that, when the function that you call from a base pointer is always going to be decided at run time, the programmers call that dynamic (late) binding. When you call the function from a reference or you call a non-virtual function, that is static binding, and you will not have any polymorphic behavior.