< previous page page_259 next page >

Page 259
LISTING 16.4 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
119:      Dog yorkie (3,YORKIE);
120:      Dog dobbie (4,20,DOBERMAN);
121:      fido.Speak();
122:      rover.WagTail();
123:      cout << Yorkie is  << yorkie.GetAge() <<  years old\n;
124:      cout << Dobbie weighs  << dobbie.GetWeight() <<  pounds\n;
125:      return 0;
126:   }

The output has been numbered so that each line can be referred to in the analysis. These numbers do not print in the actual output.

Output:
1:  Mammal constructor
2:  Dog constructor
3:  Mammal(int) constructor
4:  Dog(int) constructor
5:  Mammal(int) constructor
6:  Dog(int, int) constructor
7:  Mammal(int) constructor
8:  Dog(int, BREED) constructor.
9:  Mammal(int) constructor
10: Dog(int, int, BREED) constructor
11: Mammal sound!
12: Tail wagging
13: Yorkie is 3 years old.
14: Dobie weighs 20 pounds.
15: Dog destructor
16: Mammal destructor
17: Dog destructor
18: Mammal destructor
19: Dog destructor
20: Mammal destructor
21: Dog destructor
22: Mammal destructor
23: Dog destructor
24: Mammal destructor
Analysis: In Listing 16.4, Mammal's constructor has been overloaded on line 11 to take an integer, the Mammal's age. The implementation on lines 6166 initializes itsAge with the value passed into the constructor, and itsWeight with the value 5.
Dog has overloaded five constructors on lines 3539. The first is the default constructor. The second takes the age, which is the same parameter that the Mammal constructor takes. The third constructor takes both the age and the weight; the fourth takes the age and breed; and the fifth takes the age, weight, and breed.

 
< previous page page_259 next page >

If you like this book, buy it!