< previous page page_274 next page >

Page 274
LISTING 17.3 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
14:
15:    class Dog : public Mammal
16:    {
17:    public:
18:       void Speak()const { cout << Woof!\n; }
19:    };
20:
21:    class Cat : public Mammal
22:    {
23:    public:
24:       void Speak()const { cout << Meow!\n; }
25:    };
26:
27:    void ValueFunction (Mammal);
28:    void PtrFunction   (Mammal*);
29:    void RefFunction (Mammal&);
30:    int main()
31:    {
32:       Mammal* ptr=0;
33:       int choice;
34:       while (1)
35:       {
36:          bool fQuit = false;
37:          cout << (1)dog (2)cat (0)Quit: ;
38:          cin >> choice;
39:          switch (choice)
40:          {
41:             case 0: fQuit = true;
42:             break;
43:             case 1: ptr = new Dog;
44:             break;
45:             case 2: ptr = new Cat;
46:             break;
47:             default: ptr = new Mammal;
48:             break;
49:          }
50:          if (fQuit)
51:             break;
52:          PtrFunction(ptr);
53:          RefFunction(*ptr);
54:          ValueFunction(*ptr);
55:       }
56:       return 0;
57:    }
58:
59:    void ValueFunction (Mammal MammalValue)
60:    {
61:       MammalValue.Speak();
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_274 next page >

If you like this book, buy it!