Sams Teach Yourself C++ in 21 Days

(singke) #1
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: Dobbie 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...
In Listing 12.4,Mammal’s constructor has been overloaded on line 12 to take an
integer, the Mammal’s age. The implementation on lines 62–67 initializes itsAge
with the value passed into the constructor and initializes itsWeightwith the value 5.
Doghas overloaded five constructors on lines 36–40. The first is the default constructor.
On line 37, the second constructor takes the age, which is the same parameter that the
Mammalconstructor takes. The third constructor takes both the age and the weight, the
fourth takes the age and the breed, and the fifth takes the age, the weight, and the breed.
On line 74 is the code for Dog’s default constructor. You can see that this has something
new. When this constructor is called, it in turn calls Mammal’s default constructor as you
can see on line 75. Although it is not strictly necessary to do this, it serves as documenta-
tion that you intended to call the base constructor, which takes no parameters. The base
constructor would be called in any case, but actually doing so makes your intentions
explicit.

OUTPUT


384 Day 12


The output has been numbered here so that each line can be referred to in
the analysis.

NOTE

ANALYSIS
Free download pdf