< previous page page_331 next page >

Page 331
LISTING 20.5 POINTERS TO FUNCTIONS

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:     // Listing 20.5 Using function pointers
2:
3:     #include <iostream.h>
4:
5:     void Square (int&, int&);
6:     void Cube (int&, int&);
7:     void Swap (int&, int &);
8:     void GetVals(int&, int&);
9:     void PrintVals(int, int);
10:
11:    int main()
12:    {
13:       void (* pFunc) (int &, int &);
14:       bool fQuit = false;
15:
16:       int valOne=1, valTwo=2;
17:       int choice;
18:       while (fQuit == false)
19:       {
20:          cout <<  (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: ;
21:          cin >> choice;
22:          switch (choice)
23:          {
24:             case 1: pFunc = GetVals; break;
25:             case 2: pFunc = Square; break;
26:             case 3: pFunc = Cube; break;
27:             case 4: pFunc = Swap; break;
28:             default : fQuit = true; break;
29:          }
30:
31:          if (fQuit)
32:             break;
33:
34:          PrintVals(valOne, valTwo);
35:          pFunc(valOne, valTwo);
36:          PrintVals(valOne, valTwo);
37:       }
38:       return 0;
39:    }
40:
41:    void PrintVals(int x, int y)
42:    {
43:       cout << x:  << x <<  y:  << y << endl;
44:    }
45:
46:    void Square (int & rX, int & rY)
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_331 next page >

If you like this book, buy it!