9 Classes: A Deeper Look, Part 1 379 9.1 Introduction 380 9.2 Time Class Case Study 381 9.3 Class Scope and Accessing Class Members 388 9.4 Separating Interface from Implementation 389 9.5 Access Functions and Utility Functions 390 9.6 Time Class Case Study: Constructors with Default Arguments 393 9.7 Destructors 398 9.8 When Constructors and Destructors Are Called 399 9.9 Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member 402 9.10 Default Memberwise Assignment 405 9.11 Wrap-Up 407 10 Classes: A Deeper Look, Part 2 414 10.1 Introduction 415Contents xi 10.2 const (Constant) Objects and const Member Functions 415 10.3 Composition: Objects as Members of Classes 423 10.4 friend Functions and friend Classes 429 10.5 Using the this Pointer 431 10.6 static Class Members 436 10.7 Proxy Classes 441 10.8 Wrap-Up 445 11 Operator Overloading; Class string 451 11.1 Introduction 452 11.2 Using the Overloaded Operators of Standard Library Class string 453 11.3 Fundamentals of Operator Overloading 456 11.4 Overloading Binary Operators 457 11.5 Overloading the Binary Stream Insertion and Stream Extraction Operators 458 11.6 Overloading Unary Operators 462 11.7 Overloading the Unary Prefix and Postfix ++ and -- Operators 463 11.8 Case Study: A Date Class 464 11.9 Dynamic Memory Management 469 11.10 Case Study: Array Class 471 11.10.1 Using the Array Class 472 11.10.2 Array Class Definition 475 11.11 Operators as Member Functions vs. Non-Member Functions 483 11.12 Converting between Types 483 11.13 explicit Constructors 485 11.14 Building a String Class 487 11.15 Wrap-Up 488 12 Object-Oriented Programming: Inheritance 499 12.1 Introduction 500 12.2 Base Classes and Derived Classes 500 12.3 protected Members 503 12.4 Relationship between Base Classes and Derived Classes 503 12.4.1 Creating and Using a CommissionEmployee Class 504 12.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance 508 12.4.3 Creating a CommissionEmployee– BasePlusCommissionEmployee Inheritance Hierarchy 514 12.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data 519xii Contents 12.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data 522 12.5 Constructors and Destructors in Derived Classes 527 12.6 public, protected and private Inheritance 527 12.7 Software Engineering with Inheritance 528 12.8 Wrap-Up 529 13 Object-Oriented Programming: Polymorphism 534 13.1 Introduction 535 13.2 Introduction to Polymorphism: Polymorphic Video Game 536 13.3 Relationships Among Objects in an Inheritance Hierarchy 536 13.3.1 Invoking Base-Class Functions from Derived-Class Objects 537 13.3.2 Aiming Derived-Class Pointers at Base-Class Objects 540 13.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 541 13.3.4 Virtual Functions 543 13.4 Type Fields and switch Statements 549 13.5 Abstract Classes and Pure virtual Functions 549 13.6 Case Study: Payroll System Using Polymorphism 551 13.6.1 Creating Abstract Base Class Employee 552 13.6.2 Creating Concrete Derived Class SalariedEmployee 556 13.6.3 Creating Concrete Derived Class CommissionEmployee 558 13.6.4 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 560 13.6.5 Demonstrating Polymorphic Processing 562 13.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood” 566 13.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info 569 13.9 Virtual Destructors 573 13.10 Wrap-Up 573 14 Templates 579 14.1 Introduction 580 14.2 Function Templates 580 14.3 Overloading Function Templates 583 14.4 Class Templates 584Contents xiii 14.5 Nontype Parameters and Default Types for Class Templates 590 14.6 Wrap-Up 591 15 Stream Input/Output 595 15.1 Introduction 596 15.2 Streams 597 15.2.1 Classic Streams vs. Standard Streams 597 15.2.2 iostream Library Headers 598 15.2.3 Stream Input/Output Classes and Objects 598 15.3 Stream Output 601 15.3.1 Output of char * Variables 601 15.3.2 Character Output Using Member Function put 601 15.4 Stream Input 602 15.4.1 get and getline Member Functions 602 15.4.2 istream Member Functions peek, putback and ignore 605 15.4.3 Type-Safe I/O 605 15.5 Unformatted I/O Using read, write and gcount 605 15.6 Introduction to Stream Manipulators 606 15.6.1 Integral Stream Base: dec, oct, hex and setbase 607 15.6.2 Floating-Point Precision (precision, setprecision) 607 15.6.3 Field Width (width, setw) 609 15.6.4 User-Defined Output Stream Manipulators 610 15.7 Stream Format States and Stream Manipulators 612 15.7.1 Trailing Zeros and Decimal Points (showpoint) 612 15.7.2 Justification (left, right and internal) 613 15.7.3 Padding (fill, setfill) 615 15.7.4 Integral Stream Base (dec, oct, hex, showbase) 616 15.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) 617 15.7.6 Uppercase/Lowercase Control (uppercase) 618 15.7.7 Specifying Boolean Format (boolalpha) 618 15.7.8 Setting and Resetting the Format State via Member Function flags 619 15.8 Stream Error States 620 15.9 Tying an Output Stream to an Input Stream 622 15.10 Wrap-Up 623 16 Exception Handling: A Deeper Look 632 16.1 Introduction 633xiv Contents 16.2 Example: Handling an Attempt to Divide by Zero 633 16.3 When to Use Exception Handling 639 16.4 Rethrowing an Exception 640 16.5 Exception Specifications 641 16.6 Processing Unexpected Exceptions 642 16.7 Stack Unwinding 642 16.8 Constructors, Destructors and Exception Handling 644 16.9 Exceptions and Inheritance 645 16.10 Processing new Failures 645 16.11 Class unique_ptr and Dynamic Memory Allocation 648 16.12 Standard Library Exception Hierarchy 650 16.13 Wrap-Up 652 17 File Processing 658 17.1 Introduction 659 17.2 Files and Streams 659 17.3 Creating a Sequential File 660 17.4 Reading Data from a Sequential File 664 17.5 Updating Sequential Files 669 17.6 Random-Access Files 670 17.7 Creating a Random-Access File 671 17.8 Writing Data Randomly to a Random-Access File 675 17.9 Reading from a Random-Access File Sequentially 677 17.10 Case Study: A Transaction-Processing Program 679 17.11 Object Serialization 686 17.12 Wrap-Up 686 18 Class string and String Stream Processing 696 18.1 Introduction 697 18.2 string Assignment and Concatenation 698 18.3 Comparing strings 700 18.4 Substrings 703 18.5 Swapping strings 703 18.6 string Characteristics 704 18.7 Finding Substrings and Characters in a string 706 18.8 Replacing Characters in a string 708 18.9 Inserting Characters into a string 710 18.10 Conversion to C-Style Pointer-Based char * Strings 711 18.11 Iterators 713Contents xv 18.12 String Stream Processing 714 18.13 Wrap-Up 717 19 Searching and Sorting 724 19.1 Introduction 725 19.2 Searching Algorithms 725 19.2.1 Efficiency of Linear Search 726 19.2.2 Binary Search 727 19.3 Sorting Algorithms 732 19.3.1 Efficiency of Selection Sort 733 19.3.2 Efficiency of Insertion Sort 733 19.3.3 Merge Sort (A Recursive Implementation) 733 19.4 Wrap-Up 740 20 Custom Templatized Data Structures 746 20.1 Introduction 747 20.2 Self-Referential Classes 748 20.3 Dynamic Memory Allocation and Data Structures 749 20.4 Linked Lists 749 20.5 Stacks 764 20.6 Queues 768 20.7 Trees 772 20.8 Wrap-Up 780 21 Bits, Characters, C Strings and structs 791 21.1 Introduction 792 21.2 Structure Definitions 792 21.3 typedef 794 21.4 Example: Card Shuffling and Dealing Simulation 794 21.5 Bitwise Operators 797 21.6 Bit Fields 806 21.7 Character-Handling Library 810 21.8 Pointer-Based String Manipulation Functions 815 21.9 Pointer-Based String-Conversion Functions 822 21.10 Search Functions of the Pointer-Based String-Handling Library 827 21.11 Memory Functions of the Pointer-Based String-Handling Library 831 21.12 Wrap-Up 835xvi Contents 22 Standard Template Library (STL) 850 22.1 Introduction to the Standard Template Library (STL) 851 22.2 Introduction to Containers 853 22.3 Introduction to Iterators 856 22.4 Introduction to Algorithms 861 22.5 Sequence Containers 863 22.5.1 vector Sequence Container 864 22.5.2 list Sequence Container 871 22.5.3 deque Sequence Container 875 22.6 Associative Containers 877 22.6.1 multiset Associative Container 877 22.6.2 set Associative Container 880 22.6.3 multimap Associative Container 881 22.6.4 map Associative Container 883 22.7 Container Adapters 885 22.7.1 stack Adapter 885 22.7.2 queue Adapter 887 22.7.3 priority_queue Adapter 888 22.8 Algorithms 890 22.8.1 fill, fill_n, generate and generate_n 890 22.8.2 equal, mismatch and lexicographical_compare 892 22.8.3 remove, remove_if, remove_copy and remove_copy_if 895 22.8.4 replace, replace_if, replace_copy and replace_copy_if 897 22.8.5 Mathematical Algorithms 900 22.8.6 Basic Searching and Sorting Algorithms 903 22.8.7 swap, iter_swap and swap_ranges 905 22.8.8 copy_backward, merge, unique and reverse 906 22.8.9 inplace_merge, unique_copy and reverse_copy 909 22.8.10 Set Operations 910 22.8.11 lower_bound, upper_bound and equal_range 913 22.8.12 Heapsort 915 22.8.13 min and max 918 22.8.14 STL Algorithms Not Covered in This Chapter 919 22.9 Class bitset 920 22.10 Function Objects 924 22.11 Wrap-Up 927 23 Boost Libraries, Technical Report 1 and C++0x 936 23.1 Introduction 937Contents xvii 23.2 Deitel Online C++ and Related Resource Centers 937 23.3 Boost Libraries 937 23.4 Boost Libraries Overview 938 23.5 Regular Expressions with the regex Library 941 23.5.1 Regular Expression Example 942 23.5.2 Validating User Input with Regular Expressions 944 23.5.3 Replacing and Splitting Strings 947 23.6 Smart Pointers 950 23.6.1 Reference Counted shared_ptr 950 23.6.2 weak_ptr: shared_ptr Observer 954 23.7 Technical Report 1 960 23.8 C++0x 961 23.9 Core Language Changes 962 23.10 Wrap-Up 967 24 Other Topics 974 24.1 Introduction 975 24.2 const_cast Operator 975 24.3 mutable Class Members 977 24.4 namespaces 979 24.5 Operator Keywords 982 24.6 Pointers to Class Members (.* and ->*) 984 24.7 Multiple Inheritance 986 24.8 Multiple Inheritance and virtual Base Classes 991 24.9 Wrap-Up 996 Chapters on the Web 1001 A Operator Precedence and Associativity 1002 B ASCII Character Set 1004 C Fundamental Types 1005 D Number Systems 1007 D.1 Introduction 1008xviii Contents D.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers 1011 D.3 Converting Octal and Hexadecimal Numbers to Binary Numbers 1012 D.4 Converting from Binary, Octal or Hexadecimal to Decimal 1012 D.5 Converting from Decimal to Binary, Octal or Hexadecimal 1013 D.6 Negative Binary Numbers: Two’s Complement Notation 1015 E Preprocessor 1020 E.1 Introduction 1021 E.2 #include Preprocessor Directive 1021 E.3 #define Preprocessor Directive: Symbolic Constants 1022 E.4 #define Preprocessor Directive: Macros 1022 E.5 Conditional Compilation 1024 E.6 #error and #pragma Preprocessor Directives 1025 E.7 Operators # and ## 1026 E.8 Predefined Symbolic Constants 1026 E.9 Assertions 1027 E.10 Wrap-Up 1027 Appendices on the Web 1033 Index 1035 Chapters 25–26 and Appendices F–I are PDF documents posted online at the book’s Companion Website, which is accessible from www.pearsonhighered.com/deitel. 25 ATM Case Study, Part 1: Object-Oriented Design with the UML 25-1 25.1 Introduction 25-2 25.2 Introduction to Object-Oriented Analysis and Design 25-2 25.3 Examining the ATM Requirements Document 25-3 25.4 Identifying the Classes in the ATM Requirements Document 25-10 25.5 Identifying Class Attributes 25-17 25.6 Identifying Objects’ States and Activities 25-21 25.7 Identifying Class Operations 25-25 25.8 Indicating Collaboration Among Objects 25-32 25.9 Wrap-Up 25-39Contents xix 26 ATM Case Study, Part 2: Implementing an Object-Oriented Design 26-1 26.1 Introduction 26-2 26.2 Starting to Program the Classes of the ATM System 26-2 26.3 Incorporating Inheritance into the ATM System 26-8 26.4 ATM Case Study Implementation 26-15 26.4.1 Class ATM 26-16 26.4.2 Class Screen 26-23 26.4.3 Class Keypad 26-25 26.4.4 Class CashDispenser 26-26 26.4.5 Class DepositSlot 26-28 26.4.6 Class Account 26-29 26.4.7 Class BankDatabase 26-31 26.4.8 Class Transaction 26-35 26.4.9 Class BalanceInquiry 26-37 26.4.10 Class Withdrawal 26-39 26.4.11 Class Deposit 26-44 26.4.12 Test Program ATMCaseStudy.cpp 26-47 26.5 Wrap-Up 26-47 F C Legacy Code Topics F-1 F.1 Introduction F-2 F.2 Redirecting Input/Output on UNIX/Linux/Mac OS X and Windows Systems F-2 F.3 Variable-Length Argument Lists F-3 F.4 Using Command-Line Arguments F-5 F.5 Notes on Compiling Multiple-Source-File Programs F-7 F.6 Program Termination with exit and atexit F-9 F.7 Type Qualifier volatile F-10 F.8 Suffixes for Integer and Floating-Point Constants F-10 F.9 Signal Handling F-11 F.10 Dynamic Memory Allocation with calloc and realloc F-13 F.11 Unconditional Branch: goto F-14 F.12 Unions F-15 F.13 Linkage Specifications F-18 F.14 Wrap-Up F-19 G UML 2: Additional Diagram Types G-1 G.1 Introduction G-1 G.2 Additional Diagram Types G-2xx Contents H Using the Visual Studio Debugger H-1 H.1 Introduction H-2 H.2 Breakpoints and the Continue Command H-2 H.3 Locals and Watch Windows H-8 H.4 Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands H-11 H.5 Autos Window H-13 H.6 Wrap-Up H-14 I Using the GNU C++ Debugger I-1 I.1 Introduction I-2 I.2 Breakpoints and the run, stop, continue and print Commands I-2 I.3 print and set Commands I-8 I.4 Controlling Execution Using the step, finis