C/C++ Arena

Step 6 of 8

Calling the base version, and protected

An override often wants to extend the base behavior, not replace it. Call the base version by naming it:

std::string describe() const override {
    return Unit::describe() + " [elite]";
}

Access levels, one more time:

Keyword Who can use it
public everyone
protected the class and classes derived from it
private only the class itself

Use protected sparingly. Every protected member is part of the contract with all future subclasses, so it's as hard to change later as a public one.

Your turn: finish Medic. Its describe() returns the base description plus " +heal", and heal(Unit& u) restores u by the protected power_ amount (use u.restore(...)).

Previous: Virtual destructors Next: Composition over inheritance