< previous page page_339 next page >

Page 339
LISTING 20.8 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
24:          switch (choice)
25:          {
26:             case 1:pFunc = GetVals; break;
27:             case 2:pFunc = Square; break;
28:             case 3:pFunc = Cube; break;
29:             case 4:pFunc = Swap; break;
30:             default:fQuit = true; break;
31:          }
32:          if (fQuit == true)
33:             break;
34:          PrintVals ( pFunc, valOne, valTwo);
35:       }
36:       return 0;
37:    }
38:
39:    void PrintVals ( VPF pFunc,int& x, int& y)
40:    {
41:       cout << x:  << x <<  y:  << y << end1;
42:       pFunc(x,y);
43:       cout << x:  << x <<  y:  << y << end1;
44:    }

Output:
(0)Quit (1) Change Values (2) Square (3)Cube (4)Swap: 1
x: 1 y:2
New value for ValOne: 2
New value for ValTwo: 3
x: 2 y:3
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3
x: 2 y:3
x: 8 y: 27
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2
x: 8 y: 27
x:64 y:729
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4
x:64 y:729
x:729 y:64
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0
Analysis: On line 9,
typedef is used to declare VPF to be of the type function that returns void and takes two parameters, both integer references.
On line 10, the function PrintVals() is declared to take three parameters, a VPF and two integer references. On line 18, pFunc is now declared to be of type VPF.
Once the type VPF is defined, all subsequent uses, to declare pFunc and PrintVals(), are much cleaner.

 
< previous page page_339 next page >

If you like this book, buy it!