1 / 18

Pointer to Functions Lesson xx

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.

kert
Download Presentation

Pointer to Functions Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointer to FunctionsLesson xx

  2. Objectives • Pointer review • Pointer to a function • Declaration • Illustration • Examples • Program using a pointer to a function

  3. Pointer Review int * pi; char * pc; date * pd; void * pv;

  4. Pointer to a Function Declaration int (*pf) (float, char);

  5. Pointer to Function Illustration main() 3fc 4ab 4ab fun1() pf fun2() 4ff

  6. Mechanics main() 3fc void (*pf) (void); void prtInfo (void); pf = &prtInfo; 4ab 4ab prtInfo ( ) pf fun2() 4ff

  7. 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

  8. 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

  9. 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

  10. 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};

  11. 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; }

  12. 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); }

  13. 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};

  14. 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]

  15. 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; }

  16. 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]

  17. 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); }

  18. Summary • Pointer review • Pointer to a function • Declaration • Illustration • Examples • Program using a pointer to a function

More Related