I'm confused by a question at the end of chapter 8 of "Accelerated C++." The question is:
Note that the various analysis functions we wrote in 6.2/110 share the same behavior; they differ only in terms of the functions they call to calculate the final grade. Write a template function, parameterized by the type of the grading function, and use that function to evaluate the grading schemes.
Here are the analysis functions in question:
Code:
double median_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(),
back_inserter(grades), grade_aux);
return median(grades);
}
double average_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(),
back_inserter(grades), average_grade);
return median(grades);
}
double optimistic_median_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(),
back_inserter(grades), optimistic_median);
return median(grades);
}
I don't really know what the question means. The functions differ in the function they pass to "transform," but what does it mean to write a template function for these functions? When it mentions the "type" of the grading functions, does that mean the type they return? Each of the functions in question return a double. As do each of the above functions. So why would you write a template function? Surely if the object of the exercise is to consolidate the behavior of the three functions above into one, then wouldn't that just involve passing the function used by "transform" as an argument?