|
|
|
|
|
|
|
LISTING 20.10 ARRAY OF POINTERS TO MEMBER FUNCTIONS |
|
|
|
 |
|
|
|
|
1: //Listing 20.10 Array of pointers to member functions
2:
3: #include <iostream.h>
4:
5:
6: class Dog
7: {
8: public:
9: void Speak()const { cout << Woof!\n; }
10: void Move() const { cout << Walking to heel\n; }
11: void Eat() const { cout << Gobbling food\n; }
12: void Growl() const { cout << Grrrrr\n; }
13: void Whimper() const { cout << Whining noises\n; }
14: void RollOver() const { cout << Rolling over\n; }
15: void PlayDead() const
16: { cout << Is this the end of Little Caeser?\n; }
17: };
18:
19: typedef void (Dog::*PDF)()const ;
20: int main()
21: {
22: const int MaxFuncs = 7;
23: PDF DogFunctions[MaxFuncs] =
24: { Dog::Speak,
25: Dog::Move,
26: Dog::Eat,
27: Dog::Growl,
28: Dog::Whimper,
29: Dog::RollOver,
30: Dog::PlayDead };
31:
32: Dog* pDog =0;
33: int Method;
34: bool fQuit = false;
35:
36: while (!fQuit)
37: {
38: cout << (0)Quit (1)Speak (2)Move (3)Eat (4)Growl;
39: cout << (5)Whimper (6)Roll Over (7)Play Dead: ;
40: cin >> Method;
41: if (Method == 0)
42: {
43: fQuit = true;
44: break;
45: }
46: else
47: { |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|