Encapsulation
Inheritance
Polymorphism
200

How can you modify the setHealth method to ensure health cannot be set to a negative value? Change one line in the code below.

class Character {
private:
    int health;

public:
    void setHealth(int h) {
        health = h; // Add validation here
    }
};


if (h >= 0) health = h;



200

How do you modify the attack method in the base class so the Warrior class can override it? Add one word.


class Character {
    void attack() { // Modify this to allow overriding
        cout << "Character attacks!" << endl;
    }
};


Add virtual before void attack()

200

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()

300

In the code below, how would you provide controlled access to health while keeping it private?

Add one line.

class Character {
private:
    int health;
};


int getHealth() { return health; }



300

How do you modify health in the base class so it can be accessed directly by the Warrior class? Change one word.

class Character {
private: // Modify this to allow inheritance access
    int health;
};


Change private to protecte

300

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

400

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

400

How do you modify the Warrior class to inherit from Character? Add one word.

class Character {
public:
    void attack() {
        cout << "Character attacks!" << endl;
    }
};

class Warrior {
    // Add inheritance here
};


class Warrior : public Character { };



400

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()

M
e
n
u