Skip to content

Commit

Permalink
function pointer
Browse files Browse the repository at this point in the history
This program is used to leran the function pointer
  • Loading branch information
feihun1 committed Apr 1, 2017
1 parent 1c48e1f commit 156ac46
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cpp four.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//The use of the function pointer
#include <iostream>
using namespace std;
double sam(int minutes);
double sufei(int minutes);
void sum(int count, double(*p)(int));
void sum(int count, double(*p)(int)) //The return value of a function and parameters need to declare
{
cout << count<<endl << "it will take:";
cout << (*p)(count) << endl;
}
double sam(int minutes)
{
return 0.5*minutes;
}
double sufei(int minutes)
{
return 0.4*minutes;
}
int main()
{
int code;
cout << "please input a number:" << endl;
cin >> code;
cout << "Here is sam's time:";
sum(code, sam); //The function name as address directly
cout << "Here is sufei's sum:";
sum(code, sufei);
cin.get();
cin.get();
return 0;
}

0 comments on commit 156ac46

Please sign in to comment.