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 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 4 using std::cout; 5 using std::endl; 6 using std::fixed; 7 8 #include 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.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography