Programming and Graphics

(Kiana) #1

6.12 The class of runners 181


The following main function contained in the filerunner.cc reads the
properties of the runners from the keyboard, keeps a record of the fastest runner,
and prints the fastest runner in the end:


#include <iostream>
#include <string>
using namespace std;

int main()
{
runner fastest; // introduce the default runner
double fasttime = fastest.gettime(); // default time:
bool more = true;
string answer;

while(more) // repeat as long as more is true
{
runner member; // introduce the next runner
member.read(); // evaluate the next runner named ‘‘member’’
member.print(); // print the properties of ‘‘member’’

if(member.gettime() < fasttime)
{fastest = member;
fasttime = member.gettime();
}

cout << " More runners? (y/n)"; // inquire for more runners
getline (cin, answer);
if(answer != "y") more = false;
}

cout << endl << " Fastest runner:" << endl;
cout << " ---------------" << endl;
fastest.print(); // print the properties of the fastest runner

return 0;
}

If we want to keep a table of the runners, we can introduce the vector
“member[i]” whose entries are objects of the “runner” class. The following main
function contained in the filerunnerfast.ccreads the properties of the runners
from the keyboard, keeps track of the fastest runner, and prints the fastest
runner in the end:


#include <iostream>
#include <string>
#include "clrunner.h"

using namespace std;
Free download pdf