-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionPointer.cpp
executable file
·79 lines (64 loc) · 1.35 KB
/
FunctionPointer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
using namespace std;
// Case 1:
void add(int a, int b)
{
cout << "Addition is: " << a+b << endl;
}
void subtract(int a, int b)
{
cout << "Subtraction is: " << a-b << endl;
}
void multiply(int a, int b)
{
cout << "Multiplication is: " << a*b << endl;
}
// Case 2:
void fillRandomValues(vector<int>& randVec, int (*fnPtrRand)(void))
{
for (uint32_t i = 0; i < randVec.size(); i++)
{
randVec[i] = fnPtrRand();
}
}
int generateRandom()
{
return rand() % 10;
}
// Case 3:
void doSomeOp(string str, void (*fnPtrOp)(string))
{
fnPtrOp(str);
}
void printString(string s)
{
cout << "String: " << s << endl;
}
int main()
{
{
// fun_ptr_arr is an array of function pointers
void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};
unsigned int ch, a = 15, b = 10;
cout << "Enter Choice: 0 for add, 1 for subtract and 2 for multiply: ";
// cin >> ch;
ch = 1;
if (ch > 2) return 0;
(*fun_ptr_arr[ch])(a, b);
}
{
vector<int> randVec(10);
fillRandomValues(randVec, generateRandom);
for (int i : randVec)
{
cout << i << ", ";
}
cout << endl;
}
{
string str = "Hello";
doSomeOp(str, printString);
}
return 0;
}