Sams Teach Yourself C++ in 21 Days

(singke) #1
43:
44: private:
45: BREED itsBreed;
46: };
47:
48: Mammal::Mammal():
49: itsAge(3),
50: itsWeight(5)
51: {
52: std::cout << “Mammal constructor... “ << endl;
53: }
54:
55: Mammal::~Mammal()
56: {
57: std::cout << “Mammal destructor... “ << endl;
58: }
59:
60: Dog::Dog():
61: itsBreed(GOLDEN)
62: {
63: std::cout << “Dog constructor... “ << endl;
64: }
65:
66: Dog::~Dog()
67: {
68: std::cout << “Dog destructor... “ << endl;
69: }
70: int main()
71: {
72: Dog Fido;
73: Fido.Speak();
74: Fido.WagTail();
75: std::cout << “Fido is “ << Fido.GetAge() << “ years old” << endl;
76: return 0;
77: }

Mammal constructor...
Dog constructor...
Mammal sound!
Tail wagging...
Fido is 3 years old
Dog destructor...
Mammal destructor...
Listing 12.3 is like Listing 12.2, except that on lines 48 to 69 the constructors
and destructors now print to the screen when called. Mammal’s constructor is
called, then Dog’s. At that point, the Dogfully exists, and its methods can be called.

OUTPUT


380 Day 12


LISTING12.3 continued

ANALYSIS
Free download pdf