-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun_ptr.cpp
43 lines (36 loc) · 878 Bytes
/
fun_ptr.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
// in the calling function, pf = (*pf)
#include <iostream>
double betsy(int);
double pam(int);
// second argument is pointer to a type double function
// that take a type int argument
void estimate(int lines, double(*pf)(int));
// in this method, we give the function name with
// format: return type (*pf)(input type), and
// pass method name to perfrom compute.
int main()
{
using namespace std;
int code;
cout << "How many lines of code do you need? ";
cin >> code;
cout << "Here's Betsy's estimate: \n";
estimate(code, betsy);
cout << "Here's Pam's estimate: \n";
estimate(code, pam);
return 0;
}
double betsy(int lns)
{
return 0.05 * lns;
}
double pam(int lns)
{
return 0.03 * lns + 0.0004 * lns * lns;
}
void estimate(int lines, double (*pf)(int))
{
using namespace std;
cout << lines << " lines will take ";
cout << (*pf)(lines) << " hour(s)\n";
}