< previous page page_336 next page >

Page 336
LISTING 20.7 PASSING POINTERS TO FUNCTIONS AS FUNCTION ARGUMENTS

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

 
< previous page page_336 next page >

If you like this book, buy it!