Answer:
Here is the complete program:
#include <iostream>
using namespace std;
class RunnerInfo {
public:
void SetTime(int timeRunSecs); // Time run in seconds
void SetDist(double distRunMiles); // Distance run in miles
double GetSpeedMph(); // Speed in miles/hour
private:
int timeRun;
double distRun; };
void RunnerInfo::SetTime(int timeRunSecs) {
timeRun = timeRunSecs; // timeRun refers to data member
}
void RunnerInfo::SetDist(double distRunMiles) {
distRun = distRunMiles; }
double RunnerInfo::GetSpeedMph() {
return distRun / (timeRun / 3600.0); // miles / (secs / (hrs / 3600 secs)) }
int main() {
RunnerInfo runner1; // User-created object of class type RunnerInfo
RunnerInfo runner2; // A second object
runner1.SetTime(360);
runner1.SetDist(1.2);
runner2.SetTime(200);
runner2.SetDist(0.5);
cout << "Runner1's speed in MPH: " << runner1.GetSpeedMph() << endl;
cout << "Runner2's speed in MPH: " << runner2.GetSpeedMph() << endl;
return 0; }
Step-by-step explanation:
(A):
private:
The variables timeRun and distRun are the data members of class RunnerInfo so they should be set to be private in order to prevent access from outside the class. So they are set to private which is an access specifier.
(B):
RunnerInfo
Here the name of the class RunnerInfo is used with double colon operator (::) which is used to qualify a C++ member function. This is used to define a function outside a class. So here the function name is SetTime() and it is defined outside class RunnerInfo. So we use resolution operator to define this function. The basic syntax is:
class_name :: function_name (args)
(C):
RunnerInfo::
This is missing in the above code. This works the same as (B).
:: double colons are called resolution operator :: and it is used to define a function outside a class.
(D):
GetSpeedMph()
runner1 is the object of class RunnerInfo which represents Runner 1. So in order to display the Runner 1's speed in Mph, the method GetSpeedMph() is called using the object runner1. The object is basically used to access the method of class RunnerInfo. This method GetSpeedMph() computes the speed using formula distRun / (timeRun / 3600.0); and returns the computed speed in MPH.
(E):
runner2.GetSpeedMph()
If we see the print statement:
Runner2's speed in MPH:
This shows that the speed of 2nd Runner is required. So for this purpose, the object runner2 is used. So using this object we can call the method that computes the speed of the 2nd runner. This method is GetSpeedMph(). So using runner2 for 2nd runner we access the method GetSpeedMph() to compute the Runner2's speed in MPH.
The entire program after correction gives the following result:
Runner1's speed in MPH: 12 Runner2's speed in MPH: 9