< previous page page_926 next page >

Page 926
which Write function to call by checking the data type of the associated object.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Static Binding The compile-time determination of which function to call for a particular object.
In some situations, the compiler cannot determine the type of an object, and the binding of an operation to an object must occur at run time. One situation, which we look at now, involves parameter passage of class objects.
The basic C++ rule for passing class objects as parameters is that the actual parameter and its corresponding formal parameter must be of identical type. With inheritance, though, C++ relaxes the rule. You may pass an object of a child class C to an object of its parent class P, but not the other way aroundthat is, you cannot pass an object of type P to an object of type C. More generally, you can pass an object of a descendant class to an object of any of its ancestor classes. This rule has a tremendous benefitit allows us to write a single function that applies to any descendant class instead of writing a different function for each. For example, we could write a fancy Print function that takes as a parameter an object of type Time or any class descended from Time:
void Print( /* in */ Time someTime )
{
    cout << ************************* < endl;
    cout << ** The time is ;
    someTime.Write();
    cout << endl;
    cout << ************************* < endl;
}
Given the code fragment
Time    startTime(8, 30, 0);
ExtTime endTime(10, 45, 0, CST);

Print(startTime);
Print(endTime);
the compiler lets us pass either a Time object or an ExtTime object to the Print function. Unfortunately, the output is not what we would like. When endTime is printed, the time zone CST is missing from the output. Let's see why.

 
< previous page page_926 next page >