Main Page

Previous Page
Next Page

[Page 1294]

G.4. Class Keypad

Class Keypad (Figs. G.5G.6) represents the keypad of the ATM and is responsible for receiving all user input. Recall that we are simulating this hardware, so we use the computer's keyboard to approximate the keypad. A computer keyboard contains many keys not found on the ATM's keypad. However, we assume that the user presses only the keys on the computer keyboard that also appear on the keypadthe keys numbered 09 and the Enter key. Line 9 of Fig. G.5 contains the function prototype for class Keypad's one member function getInput. This member function is declared const because it does not change the object.

Figure G.5. Keypad class definition.

 1  // Keypad.h
 2  // Keypad class definition. Represents the keypad of the ATM.
 3  #ifndef KEYPAD_H
 4  #define KEYPAD_H
 6  class Keypad
 7  {
 8  public:
 9     int getInput() const; // return an integer value entered by user
10  }; // end class Keypad
12  #endif // KEYPAD_H

Figure G.6. Keypad class member-function definition.
(This item is displayed on page 1295 in the print version)

 1  // Keypad.cpp
 2  // Member-function definition for class Keypad (the ATM's keypad).
 3  #include <iostream>
 4  using std::cin;
 6  #include "Keypad.h" // Keypad class definition
 8  // return an integer value entered by user
 9  int Keypad::getInput() const
10  {
11     int input; // variable to store the input
12     cin >> input; // we assume that user enters an integer
13     return input; // return the value entered by user
14  } // end function getInput

[Page 1295]

Keypad Class Member-Function Definition

In the Keypad implementation file (Fig. G.6), member function getInput (defined in lines 914) uses the standard input stream cin and the stream extraction operator (>>) to obtain input from the user. Line 11 declares a local variable to store the user's input. Line 12 reads input into local variable input, then line 13 returns this value. Recall that getInput obtains all the input used by the ATM. Keypad's getInput member function simply returns the integer input by the user. If a client of class Keypad requires input that satisfies some particular criteria (i.e., a number corresponding to a valid menu option), the client must perform the appropriate error checking. [Note: Using the standard input stream cin and the stream extraction operator (>>) allows noninteger input to be read from the user. Because the real ATM's keypad permits only integer input, however, we assume that the user enters an integer and do not attempt to fix problems caused by noninteger input.]

Previous Page
Next Page