// Listing 14.7 demonstrates use of an array of pointers to functions #include using namespace std; void square(int &,int &); void cube(int &, int &); void swap(int &, int &); void getVals(int &, int &); void printVals(int, int); int main() { int valOne = 1; int valTwo = 2; int choice = 0; int i = 0; const int MAX_ARRAY = 5; void (*pFuncArray[MAX_ARRAY])(int &, int &); for (i = 0; i < MAX_ARRAY; i++) { cout << "(1)Change Values (2)square (3)cube (4)swap:"; cin >> choice; switch (choice) { case 1: pFuncArray[i] = getVals; break; case 2: pFuncArray[i] = square; break; case 3: pFuncArray[i] = cube; break; case 4: pFuncArray[i] = swap; break; default: pFuncArray[i] = 0; } } for (i = 0; i < MAX_ARRAY; i++) { pFuncArray[i](valOne, valTwo); printVals(valOne, valTwo); } return 0; } void printVals(int x, int y) { cout << "x: " << x << " y: " << y << endl; } void square(int & rX, int & rY) { rX *= rX; rY *= rY; } void cube(int & rX, int & rY) { int tmp; tmp = rX; rX *= rX; rX = rX * tmp; tmp = rY; rY *= rY; rY = rY * tmp; } void swap(int & rX, int & rY) { int temp; temp = rX; rX = rY; rY = temp; } void getVals(int & rValOne, int & rValTwo) { cout << "New value for ValOne: "; cin >> rValOne; cout << "New value for ValTwo: "; cin >> rValTwo; }