SOURCE FILE: ex0503.cpp
//
// Example 5.3. Program to test polymorphic function
// QualityPoints, which returns course quality points
//
#include <iostream.h>
#include <iomanip.h>
int main(void)
{
float QualityPoints(int, int); // prototypes
float QualityPoints(int, int, int);
cout << "Quality points for a 4-hour course taken\n";
cout << setprecision(3)
<< setiosflags(ios::showpoint | ios::fixed);
cout << "once, grade--B: " << setw(8)
<< QualityPoints(4, 3) << endl;
cout << "twice, grades-F and C:" << setw(8)
<< QualityPoints(4, 0, 2) << endl;
return 0;
}
//
// Polymorphic function to return the number of quality
// points a student earns for a course
// Pre: hours is the number of semester hours for a course.
// QPperHr is the quality points per semester hour:
// A - 4, B - 3, C - 2, D - 1, F - 0
// Post: The course quality points (hours * QPperHr) was
// returned as a float.
//
float QualityPoints(int hours, int QPperHr)
{
return float(hours * QPperHr);
}
//
// Polymorphic function to return the number of quality
// points for a course taken twice (higher grade given)
// Pre: hours is the number of semester hours for a course.
// QPperHr1 is the quality points per semester hour
// for the first attempt: A-4, B-3, C-2, D-1, F-0
// QPperHr2 is quality points/hour for the second attempt.
// Post: The course quality points (maximum of hours * QPperHr1
// and hours * QPperHr2) was returned as a float.
//
float QualityPoints(int hours, int QPperHr1, int QPperHr2)
{
int MaxQPperHr; // maximum of QPperHr1 and QPperHr2
if (QPperHr1 > QPperHr2)
MaxQPperHr = QPperHr1;
else
MaxQPperHr = QPperHr2;
return float(hours * MaxQPperHr);
}