I don't understand the difference between a base class with virtual function and without it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I don't understand the difference between a base class with virtual function and without it.

Seems that the examples (in the lesson "polim" and "virt. func"are the same.

31st Mar 2016, 6:19 AM
Alessandro Noferi
Alessandro Noferi - avatar
2 Answers
+ 4
There is a tiny difference though. Lets call base virtual method "x". When you reload the "virtual" function in derivative class it leads to call of reloaded function in code like this: Base *a = new Derivative(); a->x(); //calls reloaded version from Derivative in case when "virtual" keyword is ommited, the same code: Base *a = new Derivative(); a->x(); //calls original version from Base, despite from the fact we "reloaded" "x" in Derivative class as well Please note in both examples we used pointer of type Base.
1st Apr 2016, 9:40 PM
Valery Chikalov
Valery Chikalov - avatar
0
Definetely agree with Valery's explanation. However while trying to implement both proposed solutions within the "Learn c++" application emulator I noticed that the method reloaded version is always called regardless whether it's been declared virtual or not. It's seems to me, the only way to grab and verify the theory of "virtual methods" in action is through a real IDE such as "eclipse" or "Code Block"... Please confirm my assumption :-)
24th Sep 2016, 5:05 AM
The Artist
The Artist - avatar