Implementing Inheritance 389
12
name. So, although the Dogclass could have called the Move(int)method if it had not
overridden the version of Move()without parameters, now that it has done so, it must
override both if it wants to use both. Otherwise, it hidesthe method that it doesn’t over-
ride. This is reminiscent of the rule that if you supply any constructor, the compiler no
longer supplies a default constructor.
The rule is this: After you override any overloaded method, all the other overrides of that
method are hidden. If you want them not to be hidden, you must override them all.
It is a common mistake to hide a base class method when you intend to override it, by
forgetting to include the keyword const. constis part of the signature, and leaving it off
changes the signature, and thus hides the method rather than overrides it.
Overriding Versus Hiding
In the next section, virtual methods are described. Overriding a virtual method supports
polymorphism—hiding it undermines polymorphism. You’ll see more on this very soon.
Calling the Base Method ................................................................................
If you have overridden the base method, it is still possible to call it by fully qualifying
the name of the method. You do this by writing the base name, followed by two colons
and then the method name:
baseClass::Method()
You can call the Move()method of the Mammalclass as follows:
Mammal::Move().
You can use these qualified names just as you would any other method name. It would
have been possible to rewrite line 33 in Listing 12.6 so that it would compile, by writing
Fido.Mammal::Move(10);
This calls the Mammalmethod explicitly. Listing 12.7 fully illustrates this idea.
LISTING12.7 Calling a Base Method from a Overridden Method
1: //Listing 12.7 Calling a base method from a overridden method.
2: #include <iostream>
3: using namespace std;
4:
5: class Mammal
6: {