How can you modify the setHealth method to ensure health cannot be set to a negative value? Change one line in the code below.
How do you modify the attack method in the base class so the Warrior class can override it? Add one word.
Add virtual before void attack()
What keyword ensures the correct destructor is called for derived classes? Add one word.
class Character {
~Character() { cout << "Destroying Character" << endl; }
};
Add virtual before ~Character()
In the code below, how would you provide controlled access to health while keeping it private?
Add one line.
How do you modify health in the base class so it can be accessed directly by the Warrior class? Change one word.
Change private to protecte
What keyword ensures that the correct attack method is called dynamically for derived classes? Fill in the blank.
Character* c = new Warrior();
c->attack(); // Add keyword to enable dynamic dispatch
Ensure attack is virtual in the base class
In the code below, how would you make health inaccessible to other classes directly? Change one word.
class Character {
public: // Change this to demonstrate encapsulation int health;
};
Change public to private
How do you modify the Warrior class to inherit from Character? Add one word.
How do you allow the attack method to behave polymorphically in derived classes? Add one word.
class Character {
void attack() { // Modify this
cout << "Character attacks!" << endl;
}
};
Add virtual before void attack()