+ 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
1 Respuesta
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;
}
}
}