-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This program is used to leran the function pointer
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |