Difference between revisions of "CPP/FunctionPointer"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==FunctionPointer.cpp== <source lang="cpp"> #include <iostream> using namespace std; void add(int a, int b); void DoThing(void (*pt2Func)(int,int)); int main(int argc, char …') |
FirstPerson (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | + | <source lang = "cpp"> | |
− | <source lang="cpp"> | + | |
#include <iostream> | #include <iostream> | ||
− | |||
using namespace std; | using namespace std; | ||
− | + | int add(int a, int b) { return a + b; } | |
− | + | int subtract(int a, int b) { return a - b; } | |
− | + | int multiply(int a, int b) { return a * b; } | |
− | int | + | int divide(int a, int b) { return a / b ; } |
− | { | + | |
− | + | ||
− | + | ||
− | return | + | |
− | } | + | |
− | + | //to make syntax easier, use typedef for a function pointer | |
− | { | + | //AritmaticFunctionPointer is a pointer function that takes a function with a int return value with 2 int parameter |
− | + | typedef int (*ArithmaticFunctionPointer)(int,int); | |
+ | |||
+ | int ApplyFunction(const ArithmaticFunctionPointer func, int a, int b){ | ||
+ | return (*func)(a,b); | ||
} | } | ||
+ | |||
+ | int main(){ | ||
+ | const int ADD = 1; | ||
+ | const int SUB = 2; | ||
+ | const int MULT = 3; | ||
+ | const int DIV = 4; | ||
+ | int a = 0; | ||
+ | int b = 0; | ||
− | + | cout <<"Enter 2 number : " ; | |
− | + | cin >> a >> b; | |
− | + | ||
+ | cout << "Choose :\n[1]ADD\n[2]SUBTRACT\n[3]MULTIPLY\n[4]DIVIDE\n<input> : "; | ||
+ | int opt = 0; | ||
+ | cin >> opt; | ||
+ | |||
+ | switch(opt){ | ||
+ | case ADD: cout << ApplyFunction(add,a,b) << endl; break; | ||
+ | case SUB: cout << ApplyFunction(subtract,a,b)<< endl;; break; | ||
+ | case MULT: cout << ApplyFunction(multiply,a,b)<< endl;; break; | ||
+ | case DIV: cout << ApplyFunction(divide,a,b)<< endl;; break; | ||
+ | default: cout << "Invalid Options...exiting...\n"; break; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
} | } | ||
</source> | </source> |
Latest revision as of 19:33, 25 June 2010
#include <iostream> using namespace std; int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } int divide(int a, int b) { return a / b ; } //to make syntax easier, use typedef for a function pointer //AritmaticFunctionPointer is a pointer function that takes a function with a int return value with 2 int parameter typedef int (*ArithmaticFunctionPointer)(int,int); int ApplyFunction(const ArithmaticFunctionPointer func, int a, int b){ return (*func)(a,b); } int main(){ const int ADD = 1; const int SUB = 2; const int MULT = 3; const int DIV = 4; int a = 0; int b = 0; cout <<"Enter 2 number : " ; cin >> a >> b; cout << "Choose :\n[1]ADD\n[2]SUBTRACT\n[3]MULTIPLY\n[4]DIVIDE\n<input> : "; int opt = 0; cin >> opt; switch(opt){ case ADD: cout << ApplyFunction(add,a,b) << endl; break; case SUB: cout << ApplyFunction(subtract,a,b)<< endl;; break; case MULT: cout << ApplyFunction(multiply,a,b)<< endl;; break; case DIV: cout << ApplyFunction(divide,a,b)<< endl;; break; default: cout << "Invalid Options...exiting...\n"; break; } return 0; }