< previous page page_360 next page >

Page 360
(text box continued from previous page)
One advantage of physical hiding is that it helps the programmer avoid the temptation to take advantage of any unusual features of a function's implementation. For example, suppose we were changing the Activity program to read temperatures and output activities repeatedly. Knowing that function GetTemp doesn't perform range checking on the input value, we might be tempted to use-1000 as a sentinel for the loop:
int main()
{
    int temperature;

    GetTemp(temperature);
    while (temperature != -1000)
    {
        PrintActivity(temperature);
        GetTemp(temperature);
    }
    return 0;
}
This code works fine for now, but later we might want to improve GetTemp so that it checks for a valid temperature range (as it should).
void GetTemp( /* out */ int& temp )

// This function prompts for a temperature to be entered, reads
// the input value, checks to be sure it is in a valid temperature
// range, and echo-prints it
// Postcondition:

//     User has been prompted for a temperature value (temp)
//  && Error messages and additional prompts have been printed
//      in response to invalid data
//  && IF no valid data was encountered before EOF
//         Value of temp is undefined
//     ELSE
//         -50 
 temp  130  &&  temp has been printed

{
    cout < Enter the outside temperature (-50 through 130): ;
    cin >> temp;

(text box continued on next page)

 
< previous page page_360 next page >