< previous page page_930 next page >

Page 930
works correctly for actual parameters either of type Time or of type ExtTime. The correct Write function (Time::Write or ExtTime::Write) is invoked because the actual parameter carries the information necessary at run time to choose the appropriate function. Deriving a new and unanticipated class from Time presents no complications. If this new class overrides the Write function, then our Print function still works correctly. Dynamic binding ensures that each object knows how to print itself, and the appropriate version will be invoked. In OOP terminology, Write is a polymorphic operationan operation that has multiple meanings depending on the type of the object that responds to it at run time.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Polymorphic Operation An operation that has multiple meanings depending on the type of the object to which it is bound at run time.
Here are some things to know about using virtual functions in C++:
1. To obtain dynamic binding, you must use pass-by-reference when passing a class object to a function. If you use pass-by-value, the compiler does not use the virtual mechanism; instead, member slicing and static binding occur.
2. In the declaration of a virtual function, the word virtual appears only in the base class, not in any derived class.
3. If a base class declares a virtual function, it must implement that function, even if the body is empty.
4. A derived class is not required to provide its own reimplementation of a virtual function. In this case, the base class's version is used by default.
5. A derived class cannot redefine the function return type of a virtual function.
Object-Oriented Design
We have looked at language features that let us implement an object-oriented design. Now let's turn to the phase that precedes implementationOOD itself.
A computer program usually models some real-life activity or concept. A banking program models the real-life activities associated with a bank. A spreadsheet program models a real spreadsheet, a large paper form used by accountants and financial planners. A robotics program models human perception and human motion.
Nearly always, the aspect of the world that we are modeling (the application domain or problem domain) consists of objectschecking accounts,

 
< previous page page_930 next page >