Sams Teach Yourself C++ in 21 Days
To keep the program reasonably simple and manageable, only six methods have been put in the Mammalclass—four accessor methods,Sp ...
Implementing Inheritance 377 12 Mammal, assuming that these other classes all use public inheritance. Private inheritance is dis ...
43: void WagTail() const { cout << “Tail wagging...\n”; } 44: void BegForFood() const { cout << “Begging for food... ...
Implementing Inheritance 379 12 When Fidois destroyed, first the Dogdestructor is called and then the destructor for the Mammalp ...
43: 44: private: 45: BREED itsBreed; 46: }; 47: 48: Mammal::Mammal(): 49: itsAge(3), 50: itsWeight(5) 51: { 52: std::cout <&l ...
Implementing Inheritance 381 12 When Fidogoes out of scope,Dog’s destructor is called, followed by a call to Mammal’s destructor ...
32: { 33: public: 34: 35: // Constructors 36: Dog(); 37: Dog(int age); 38: Dog(int age, int weight); 39: Dog(int age, BREED bree ...
Implementing Inheritance 383 12 81: Dog::Dog(int age): 82: Mammal(age), 83: itsBreed(GOLDEN) 84: { 85: cout << “Dog(int) c ...
1: Mammal constructor... 2: Dog constructor... 3: Mammal(int) constructor... 4: Dog(int) constructor... 5: Mammal(int) construct ...
Implementing Inheritance 385 12 The implementation for the Dogconstructor, which takes an integer, is on lines 81–86. In its ini ...
Listing 12.5 illustrates what happens if the Dogclass overrides the Speak()method in Mammal. To save room, the accessor function ...
Implementing Inheritance 387 12 Mammal constructor... Mammal constructor... Dog constructor... Mammal sound! Woof! Dog destructo ...
LISTING12.6 Hiding Methods 1: //Listing 12.6 Hiding methods 2: #include <iostream> 3: using std::cout; 4: 5: class Mammal ...
Implementing Inheritance 389 12 name. So, although the Dogclass could have called the Move(int)method if it had not overridden t ...
7: public: 8: void Move() const { cout << “Mammal move one step\n”; } 9: void Move(int distance) const 10: { 11: cout < ...
Implementing Inheritance 391 12 Virtual Methods ................................................................................ ...
functions enable you to do that. To create a virtual function, you add the keyword vir- tualin front of the function declaration ...
Implementing Inheritance 393 12 On line 29, a pointer to Mammalis created (pDog), but it is assigned the address of a new Dogobj ...
33: }; 34: 35: class Pig : public Mammal 36: { 37: public: 38: void Speak()const { cout << “Oink!\n”; } 39: }; 40: 41: int ...
Implementing Inheritance 395 12 This stripped-down program, which provides only the barest functionality to each class, illustra ...
«
16
17
18
19
20
21
22
23
24
25
»
Free download pdf