180 likes | 314 Views
Pointer to Functions Lesson xx. Objectives. Pointer review Pointer to a function Declaration Illustration Examples Program using a pointer to a function. Pointer Review. int * pi; char * pc; date * pd; void * pv ;. Pointer to a Function Declaration.
E N D
Objectives • Pointer review • Pointer to a function • Declaration • Illustration • Examples • Program using a pointer to a function
Pointer Review int * pi; char * pc; date * pd; void * pv;
Pointer to a Function Declaration int (*pf) (float, char);
Pointer to Function Illustration main() 3fc 4ab 4ab fun1() pf fun2() 4ff
Mechanics main() 3fc void (*pf) (void); void prtInfo (void); pf = &prtInfo; 4ab 4ab prtInfo ( ) pf fun2() 4ff
Direct & Indirect Function Calls void (*pf) (void); void prtInfo (void); pf = &prtInfo; prtInfo(); //direct call (*pf) ( ); //indirect call main() 3fc 4ab 4ab prtInfo ( ) pf fun2() 4ff
Another Example int (*pf) (int n); int square (int n); pf = □ int x = 5; int s = square(x); s = (*pf) (x); main() 3fc 4ab 4ab square ( ) pf fun2() 4ff
Program Description • A. Write a program that produces the following simple menu: • Menu • 0 Addition • 1 Subtraction • 2 Multiplication • 3 Division • 4 Quit • Enter menu choice 0‑4 • B. After the user has chosen an operation, read in 2 operands • C. Use a pointer to a function to call the appropriate function to do the computation
Program Listing Part 1 #include <iostream> using std::cout; using std::endl; using std::cin; void add (intna, intnb); /*function declarations*/ void sub (intna, intnb); void mul (intna, intnb); void divi (intna, intnb); int main() { int op; int a , b ; /*declare array of pointers to functions*/ void (*pf[4]) (int,int) ={add, sub, mul, divi};
Program Listing Part 2 while (1) { cout << "\n\nmenu\n0 = addition \n1 = subtraction"; cout << "\n2 =multiplication \n3 = division \n4 = quit\n"; cout << "enter menu choice 0‑4 "; cin >> op; if (op == 4) break; cout << "enter 2 numbers "; cin >> a >> b; (*pf[op]) (a,b); } return 0; }
Program Listing Part 3 void add (intna, intnb) {cout << "you are now doing addition"; cout << " the answer is " << (na+nb); } void sub (intna, intnb) {cout << "you are now doing subtraction"; cout << " the answer is " << (na-nb); } void mul (intna, intnb) {cout << "you are now doing multiplication"; cout << " the answer is " << (na*nb); } void divi (intna, intnb) {cout << "you are now doing division"; cout << " the answer is " << (na/nb); }
Code Explanation Part 1 #include <iostream> using std::cout; using std::endl; using std::cin; void add (intna, intnb); /*function declarations*/ void sub (intna, intnb); void mul (intna, intnb); void divi (intna, intnb); int main() { int op; int a , b ; /*declare array of pointers to functions*/ void (*pf[4]) (int,int) ={add, sub, mul, divi};
Array of Pointers to Functions void (*pf[4]) (int,int) ={add, sub, mul, divi}; pf 3fc add() 3fc 4ab 4ff sub ( ) 4ab 5fa pf [0] mul() 4ff pf [1] pf [2] divi() 5fa pf [3]
Program Listing Part 2 while (1) { cout << "\n\nmenu\n0 = addition \n1 = subtraction"; cout << "\n2 =multiplication \n3 = division \n4 = quit\n"; cout << "enter menu choice 0‑4 "; cin >> op; if (op == 4) break; cout << "enter 2 numbers "; cin >> a >> b; (*pf[op]) (a,b); } return 0; }
Indirect Function Call op a b (*pf[op]) (a,b); pf 3fc 2 6 7 add() 3fc 4ab 4ff sub ( ) 4ab 5fa pf [0] mul() 4ff pf [1] pf [2] divi() 5fa pf [3]
Program Listing Part 3 void add (intna, intnb) {cout << "you are now doing addition"; cout << " the answer is " << (na+nb); } void sub (intna, intnb) {cout << "you are now doing subtraction"; cout << " the answer is " << (na-nb); } void mul (intna, intnb) {cout << "you are now doing multiplication"; cout << " the answer is " << (na*nb); } void divi (intna, intnb) {cout << "you are now doing division"; cout << " the answer is " << (na/nb); }
Summary • Pointer review • Pointer to a function • Declaration • Illustration • Examples • Program using a pointer to a function