Dog objects are Mammal objects. This is the essence of the is-a relationship. When Fido is created, his base constructor is called fir...
Dog objects are Mammal objects. This is the essence of the is-a relationship. When Fido is created, his base constructor is called first, creating a Mammal. Then the Dog constructor is called, completing the construction of the Dog object. Because we gave Fido no parameters, the default constructor was called in each case. Fido doesn't exist until he is completely constructed, which means that both his Mammal part and his Dog part must be constructed. Thus, both constructors must be called.
When Fido is destroyed, first the Dog destructor will be called and then the destructor for the Mammal part of Fido. Each destructor is given an opportunity to clean up after its own part of Fido. Remember to clean up after your Dog! Listing 12.3 demonstrates this.
1: //Listing 12.3 Constructors and destructors called.
2:
3: #include <iostream.h>
4: enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
5:
6: class Mammal
7: {
8: public:
9: // constructors
10: Mammal();
11: ~Mammal();
12:
13: //accessors
14: int GetAge() const { return itsAge; }
15: void SetAge(int age) { itsAge = age; }
16: int GetWeight() const { return itsWeight; }
17: void SetWeight(int weight) { itsWeight = weight; }
18:
19: //Other methods
20: void Speak() const { cout << "Mammal sound!\n"; }
21: void Sleep() const { cout << "shhh. I'm sleeping.\n"; }
22:
23:
24: protected:
25: int itsAge;
26: int itsWeight;
27: };
28:
29: class Dog : public Mammal
30: {
31: public:
32:
33: // Constructors
34: Dog();
35: ~Dog();
36:
37: // Accessors
38: BREED GetBreed() const { return itsBreed; }
39: void SetBreed(BREED breed) { itsBreed = breed; }
40:
41: // Other methods
42: void WagTail() { cout << "Tail wagging...\n"; }
43: void BegForFood() { cout << "Begging for food...\n"; }
44:
45: private:
46: BREED itsBreed;
47: };
48:
49: Mammal::Mammal():
50: itsAge(1),
51: itsWeight(5)
52: {
53: cout << "Mammal constructor...\n";
54: }
55:
56: Mammal::~Mammal()
57: {
58: cout << "Mammal destructor...\n";
59: }
60:
61: Dog::Dog():
62: itsBreed(YORKIE)
63: {
64: cout << "Dog constructor...\n";
65: }
66:
67: Dog::~Dog()
68: {
69: cout << "Dog destructor...\n";
70: }
71: int main()
72: {
73: Dog fido;
74: fido.Speak();
75: fido.WagTail();
76: cout << "Fido is " << fido.GetAge() << " years old\n";
77: return 0;
78: }
Output: Mammal constructor...
Dog constructor...
Mammal sound!
Tail wagging...
Fido is 1 years old
Dog destructor...
Mammal destructor...
Analysis: In Listing 12.3, that the constructors and destructors now print to the screen when called. Mammal's constructor is called, then Dog's. At that point the Dog fully exists, and its methods can be called. When fido goes out of scope, Dog's destructor is called, followed by a call to Mammal's destructor.