Why use virtual member function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why use virtual member function

9th Mar 2017, 9:21 AM
Amar Dahake
2 Answers
+ 1
Supose I have a class called Hero with an attack function and i want that all my derived classes from hero implement this function different for example the attack of a ranged hero must be different from the attack of a melee hero , thats why we use the keyword "virtual" couse it allow subclasses to make different implementation of the same function (function attack on the example) deppending on what we need.
9th Mar 2017, 9:55 AM
Hector Sulbaran
Hector Sulbaran - avatar
+ 5
thanks
12th Mar 2017, 7:29 AM
Amar Dahake