www.gibmonks.com

Main Page

Previous Page
Next Page

[Page 1293]

G.3. Class Screen

Class Screen (Figs. G.3G.4) represents the screen of the ATM and encapsulates all aspects of displaying output to the user. Class Screen approximates a real ATM's screen with a computer monitor and outputs text messages using cout and the stream insertion operator (<<). In this case study, we designed class Screen to have one operationdisplayMessage. For greater flexibility in displaying messages to the Screen, we now declare three Screen member functionsdisplayMessage, displayMessageLine and displayDollarAmount. The prototypes for these member functions appear in lines 1214 of Fig. G.3.

Figure G.3. Screen class definition.

 1  // Screen.h
 2  // Screen class definition. Represents the screen of the ATM.
 3  #ifndef SCREEN_H
 4  #define SCREEN_H
 5
 6  #include <string>
 7  using std::string;
 8
 9  class Screen
10  {
11  public:
12     void displayMessage( string ) const; // output a message
13     void displayMessageLine( string ) const; // output message with newline
14     void displayDollarAmount( double ) const; // output a dollar amount
15  }; // end class Screen
16
17  #endif // SCREEN_H

Figure G.4. Screen class member-function definitions.
(This item is displayed on pages 1293 - 1294 in the print version)

 1  // Screen.cpp
 2  // Member-function definitions for class Screen.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6  using std::fixed;
 7
 8  #include <iomanip>
 9  using std::setprecision;
10
11  #include "Screen.h" // Screen class definition
12
13  // output a message without a newline
14  void Screen::displayMessage( string message ) const
15  {
16     cout << message;
17  } // end function displayMessage
18
19  // output a message with a newline
20  void Screen::displayMessageLine( string message ) const
21  {
22     cout << message << endl;
23  } // end function displayMessageLine
24
25  // output a dollar amount
26  void Screen::displayDollarAmount( double amount ) const
27  {
28     cout << fixed << setprecision( 2 ) << "$" << amount;
29  } // end function displayDollarAmount

Screen Class Member-Function Definitions

Figure G.4 contains the member-function definitions for class Screen. Line 11 #includes the Screen class definition. Member function displayMessage (lines 1417) takes a string as an argument and prints it to the console using cout and the stream insertion operator (<<). The cursor stays on the same line, making this member function appropriate for displaying prompts to the user. Member function displayMessageLine (lines 2023) also prints a string, but outputs a newline to move the cursor to the next line. Finally, member function displayDollarAmount (lines 2629) outputs a properly formatted dollar amount (e.g., $123.45). Line 28 uses stream manipulators fixed and setprecision to output a value formatted with two decimal places. See Chapter 15, Stream Input/Output, for more information about formatting output.


Previous Page
Next Page