As an example, we’ll create a class of objects that might be useful as a general–purpose programming element. A counter is a variable that counts things. Maybe it counts file accesses, or the number of times the user presses the [Enter] key, or the number of customers entering a bank. Each time such an event takes place, the counter is incremented (1 is added to it). The counter can also be accessed to find the current count.
Let’s assume that this counter is important in the program and must be accessed by many different functions. In procedural languages such as C, a counter would probably be implemented as an external variable. However, as we noted in Chapter 1, external variables complicate the program’s design and may be modified accidentally. This example, COUNTER, provides a counter variable that can be modified only through its member functions.
Friday, October 24, 2008
A Counter Example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment