What is function overridding | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is function overridding

when subclass have a method with same name same argument which is already present in super class is known as function overriding by using function overriding we achieve runtime polymorphism

11th Feb 2018, 1:24 PM
Aayush Gupta
Aayush Gupta - avatar
1 Answer
0
Consider the following code. class Enemy { public: virtual void Attack() = 0; bool IsDead() {/*implement function here*/} } class Ninja : public Enemy { public: void Attack() { /*implement attack here*/} } class Zombie : public Enemy { public: void Attack() {/*implement different attack here*/} } main() { Enemy *current_enemy = new Ninja; int enemy_counter = 0; while (enemy_counter < 20) { current_enemy.Attack() if(current_enemy.IsDead()) { ++enemy_counter; delete current_enemy; if (enemy_counter % 2 == 0) current_enemy = new Ninja; else current_enemy = new Zombie; } } }
11th Feb 2018, 1:38 PM
Vlad Serbu
Vlad Serbu - avatar