Why are we using virtual functions in C++? What is the usage of attack function in virtual function? Can anyone explain me? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Why are we using virtual functions in C++? What is the usage of attack function in virtual function? Can anyone explain me?

Because it is the new concept for me in c++..so for it didn't learn about it...

11th Feb 2017, 3:59 PM
Nithyapriya
1 ответ
+ 1
By defining attack function virtual you telling complier to call not the Enemy member function attack(), but attack() of the specific derived class (Monster or Ninja). Otherwise it'll call Enemy attack function both times because we have pointers only on the Enemy object.
13th Feb 2017, 2:09 PM
Dmytro