-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ex6_54_55_56.cpp
50 lines (44 loc) · 1.35 KB
/
ex6_54_55_56.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
// @Yue Wang
//
// Exercise 6.54:
// Write a declaration for a function that takes two int
// parameters and returns an int, and declare a vector whose
// elements have this function pointer type.
//
// Exercise 6.55:
// Write four functions that add, subtract, multiply, and divide
// two int values. Store pointers to these functions in your
// vector from the previous exercise.
//
// Exercise 6.56:
// Call each element in the vector and print their result.
//
#include <iostream>
#include <string>
#include <vector>
using std::vector; using std::cout;
//
// @brief Exercise 6.54
// @note define the function type fp
//
inline int f(const int, const int);
typedef decltype(f) fp;//fp is just a function type not a function pointer
//
// @brief Exercise 6.55
// @note Store pointers to these functions in the vector
//
inline int NumAdd(const int n1, const int n2) { return n1 + n2; }
inline int NumSub(const int n1, const int n2) { return n1 - n2; }
inline int NumMul(const int n1, const int n2) { return n1 * n2; }
inline int NumDiv(const int n1, const int n2) { return n1 / n2; }
vector<fp*> v{ NumAdd, NumSub, NumMul, NumDiv };
int main()
{
//
// @brief Exercise 6.56
// @note Call each element in the vector and print their result.
//
for (auto it = v.cbegin(); it != v.cend(); ++it)
cout << (*it)(2, 2) << std::endl;
return 0;
}