< previous page page_264 next page >

Page 264
It is a common mistake to hide a base class method, when you intend to override it, by forgetting to include the keyword const. const is part of the signature, and leaving it off changes the signature and thus hides the method rather than overriding it.
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. For example:
Mammal::Move()
It would have been possible to rewrite line 29 in Listing 16.6 so that it would compile:
29:     fido.Mammal::Move(10);
This calls the Mammal method explicitly. Listing 16.7 fully illustrates this idea.
LISTING 16.7 CALLING THE BASE METHOD FROM THE OVERRIDDEN METHOD

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:     //Listing 16.7 Calling base method from overridden method.
2:
3:     #include <iostream.h>
4:
5:     class Mammal
6:     {
7:     public:
8:        void Move() const { cout << Mammal move one step\n; }
9:        void Move(int distance) const
10:          { cout << Mammal move  << distance <<  steps.\n; }
11:    protected:
12:       int itsAge;
13:       int itsWeight;
14:    };
15:
16:    class Dog : public Mammal
17:    {
18:    public:
19:       void Move()const;
20:    };
21:
22:    void Dog::Move() const
23:    {
24:       cout << In dog move\n;
25:       Mammal::Move(3);
26:    }
27:
28:    int main()
29:    {
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_264 next page >

If you like this book, buy it!