< Previous | Contents | Next >

Overriding Virtual Base Class Member

Functions

The next step in overriding is to give the member function a new definition in the derived class. Thats what I do for the Boss class with:

void Boss::Taunt() const //override base class member function

Calling and Overriding Base Class Member Functions 345



{

cout << "The boss says he will end your pitiful existence.\n";

}

This new definition is executed when I call the member function through any Boss object. It replaces the definition of Taunt() inherited from Enemy for all Boss objects. When I call the member function in main() with the following line, the message The boss says he will end your pitiful existence. is displayed.

aBoss.Taunt();

Overriding member functions is useful when you want to change or extend the behavior of base class member functions in derived classes.


Tra p

image

Don’t confuse override with overload. When you override a member function, you provide a new definition of it in a derived class. When you overload a function, you create multiple versions of it with different signatures.

image


Tra p

image

When you override an overloaded base class member function, you hide all of the other overloaded versions of the base class member function—meaning that the only way to access the other versions of the member function is to explicitly call the base class member function. So if you override an overloaded member function, it’s a good idea to override every version of the overloaded function.

image