You are previewing C++11 for Programmers, Second Edition.
O'Reilly logo
C++11 for Programmers, Second Edition

Book Description

In C++11 for Programmers, the Deitels bring their proven Live Code approach to teaching today’s powerful new version of the C++ language. Like all Deitel Developer titles, they teach the best way possible: via hundreds of complete example C++ programs, with thousands of lines of downloadable C++ source code.

Unlike other C++11 books, this guide teaches robust, best-practice coding practices that fully support the CERT® Coordination Center’s authoritative secure coding standards. To help you write programs that are even more secure, the Deitels also introduce C++11’s new non-deterministic random-number generation capabilities. Using all these techniques, you can write industrial-strength C+11 code that stands up to attacks from viruses, worms, and other forms of malware.

Ideal for anyone who’s worked with at least one programming language before, C++11 for Programmers utilizes a proven “early objects” approach, emphasizing program clarity, software reuse, and component-oriented software construction. In addition to the core language, it will help you take advantage of the newest standard libraries and the newest language extensions. Coverage includes many new C++11 features, including smart pointers, regular expressions, shared_ptr and weak_ptr, and more.

This book contains 240 complete C++11 programs (more than 15,000 lines of downloadable code). All code has been thoroughly tested on three popular industrial-strength C++11 compilers: GNU C++ 4.7, Microsoft® Visual C++® 2012, and Apple® LLVM in Xcode® 4.5.

Table of Contents

  1. Title Page
  2. Copyright Page
  3. Dedication Page
  4. Contents
  5. Preface
    1. Features
      1. C++11 Standard
      2. Object-Oriented Programming
      3. Pedagogic Features
      4. Other Features
    2. Secure C++ Programming
    3. Training Approach
    4. Online Chapter and Appendices
    5. Obtaining the Software Used in C++11 for Programmers
    6. C++11 Fundamentals: Parts I, II, III and IV LiveLessons Video Training Product
    7. Acknowledgments
      1. Reviewers
    8. About the Authors
    9. Deitel® Dive-Into® Series Corporate Training
  6. 1. Introduction
    1. 1.1. Introduction
    2. 1.2. C++
      1. C++ Standard Library
    3. 1.3. Object Technology
      1. The Automobile as an Object
      2. Member Functions and Classes
      3. Instantiation
      4. Reuse
      5. Messages and Member Function Calls
      6. Attributes and Data Members
      7. Encapsulation
      8. Inheritance
      9. Object-Oriented Analysis and Design (OOAD)
      10. The UML (Unified Modeling Language)
    4. 1.4. Typical C++ Development Environment
      1. Phase 1: Editing a Program
      2. Phase 2: Preprocessing a C++ Program
      3. Phase 3: Compiling a C++ Program
      4. Phase 4: Linking
      5. Phase 5: Loading
      6. Phase 6: Execution
      7. Problems That May Occur at Execution Time
    5. 1.5. Test-Driving a C++ Application
      1. Running a C++ Application from the Windows Command Prompt
      2. Running a C++ Application Using GNU C++ with Linux
    6. 1.6. Operating Systems
      1. 1.6.1. Windows—A Proprietary Operating System
      2. 1.6.2. Linux—An Open-Source Operating System
      3. 1.6.3. Apple’s OS X; Apple’s iOS for iPhone®, iPad® and iPod Touch® Devices
      4. 1.6.4. Google’s Android
    7. 1.7. C++11 and the Open Source Boost Libraries
      1. Boost C++ Libraries
    8. 1.8. Web Resources
      1. Deitel & Associates Websites
  7. 2. Introduction to C++ Programming, Input/Output and Operators
    1. 2.1. Introduction
      1. Compiling and Running Programs
    2. 2.2. First Program in C++: Printing a Line of Text
      1. Comments
      2. #include Preprocessing Directive
      3. Blank Lines and White Space
      4. The main Function
      5. An Output Statement
      6. The std Namespace
      7. The Stream Insertion Operator and Escape Sequences
      8. The return Statement
    3. 2.3. Modifying Our First C++ Program
      1. Printing a Single Line of Text with Multiple Statements
      2. Printing Multiple Lines of Text with a Single Statement
    4. 2.4. Another C++ Program: Adding Integers
      1. Variable Declarations
      2. Fundamental Types
      3. Identifiers
      4. Placement of Variable Declarations
      5. Obtaining the First Value from the User
      6. Obtaining the Second Value from the User
      7. Calculating the Sum of the Values Input by the User
      8. Displaying the Result
    5. 2.5. Arithmetic
      1. Arithmetic Expressions in Straight-Line Form
      2. Parentheses for Grouping Subexpressions
      3. Rules of Operator Precedence
      4. Sample Algebraic and C++ Expressions
      5. Evaluation of a Second-Degree Polynomial
      6. Redundant Parentheses
    6. 2.6. Decision Making: Equality and Relational Operators
      1. Using the if Statement
      2. using Declarations
      3. Variable Declarations and Reading the Inputs from the User
      4. Comparing Numbers
      5. White Space
      6. Operator Precedence
    7. 2.7. Wrap-Up
  8. 3. Introduction to Classes, Objects and Strings
    1. 3.1. Introduction
    2. 3.2. Defining a Class with a Member Function
      1. Class GradeBook
      2. Testing Class GradeBook
      3. UML Class Diagram for Class GradeBook
    3. 3.3. Defining a Member Function with a Parameter
      1. Defining and Testing Class GradeBook
      2. More on Arguments and Parameters
      3. Updated UML Class Diagram for Class GradeBook
    4. 3.4. Data Members, set Member Functions and get Member Functions
      1. GradeBook Class with a Data Member, and set and get Member Functions
      2. Access Specifiers public and private
      3. Member Functions setCourseName and getCourseName
      4. Member Function displayMessage
      5. Testing Class GradeBook
      6. Software Engineering with Set and Get Functions
      7. GradeBook’s UML Class Diagram with a Data Member and set and get Functions
    5. 3.5. Initializing Objects with Constructors
      1. Defining a Constructor
      2. Testing Class GradeBook
      3. Ways to Provide a Default Constructor for a Class
      4. Adding the Constructor to Class GradeBook’s UML Class Diagram
    6. 3.6. Placing a Class in a Separate File for Reusability
      1. Headers
      2. Use std:: with Standard Library Components in Headers
      3. Including a Header That Contains a User-Defined Class
      4. How Headers Are Located
      5. Additional Software Engineering Issues
    7. 3.7. Separating Interface from Implementation
      1. Interface of a Class
      2. Separating the Interface from the Implementation
      3. GradeBook.h: Defining a Class’s Interface with Function Prototypes
      4. GradeBook.cpp: Defining Member Functions in a Separate Source-Code File
      5. Testing Class GradeBook
      6. The Compilation and Linking Process
    8. 3.8. Validating Data with set Functions
      1. GradeBook Class Definition
      2. Validating the Course Name with GradeBook Member Function setCourseName
      3. Testing Class GradeBook
      4. Additional Notes on Set Functions
    9. 3.9. Wrap-Up
  9. 4. Control Statements: Part 1; Assignment, ++ and -- Operators
    1. 4.1. Introduction
    2. 4.2. Control Structures
      1. Sequence Structure in C++
      2. Selection Statements in C++
      3. Repetition Statements in C++
      4. Summary of Control Statements in C++
    3. 4.3. if Selection Statement
    4. 4.4. if...else Double-Selection Statement
      1. Conditional Operator (?:)
      2. Nested if...else Statements
      3. Dangling-else Problem
      4. Blocks
    5. 4.5. while Repetition Statement
    6. 4.6. Counter-Controlled Repetition
      1. Enhancing GradeBook Validation
      2. Implementing Counter-Controlled Repetition in Class GradeBook
      3. Demonstrating Class GradeBook
      4. Notes on Integer Division and Truncation
      5. A Note About Arithmetic Overflow
      6. A Deeper Look at Receiving User Input
    7. 4.7. Sentinel-Controlled Repetition
      1. Implementing Sentinel-Controlled Repetition in Class GradeBook
      2. Program Logic for Sentinel-Controlled Repetition vs. Counter-Controlled Repetition
      3. Floating-Point Number Precision and Memory Requirements
      4. Converting Between Fundamental Types Explicitly and Implicitly
      5. Formatting for Floating-Point Numbers
      6. A Note About Unsigned Integers
    8. 4.8. Nested Control Statements
      1. Program That Solves Examination Results
      2. C++11 List Initialization
    9. 4.9. Assignment Operators
    10. 4.10. Increment and Decrement Operators
    11. 4.11. Wrap-Up
  10. 5. Control Statements: Part 2; Logical Operators
    1. 5.1. Introduction
    2. 5.2. Essentials of Counter-Controlled Repetition
    3. 5.3. for Repetition Statement
      1. for Statement Header Components
      2. Off-By-One Errors
      3. General Format of a for Statement
      4. Comma-Separated Lists of Expressions
      5. Expressions in the for Statement’s Header Are Optional
      6. Increment Expression Acts Like a Standalone Statement
      7. for Statement: Notes and Observations
      8. for Statement UML Activity Diagram
    4. 5.4. Examples Using the for Statement
      1. Application: Summing the Even Integers from 2 to 20
      2. Application: Compound Interest Calculations
      3. A Caution about Using Type float or double for Monetary Amounts
      4. Using Stream Manipulators to Format Numeric Output
    5. 5.5. do...while Repetition Statement
      1. do...while Statement UML Activity Diagram
      2. Braces in a do...while Statement
    6. 5.6. switch Multiple-Selection Statement
      1. GradeBook Class with switch Statement to Count A, B, C, D and F Grades
      2. GradeBook Class Header
      3. GradeBook Class Source-Code File
      4. Reading Character Input
      5. Entering the EOF Indicator
      6. switch Statement Details
      7. Providing a default Case
      8. Ignoring Newline, Tab and Blank Characters in Input
      9. Testing Class GradeBook
      10. switch Statement UML Activity Diagram
      11. Notes on Data Types
      12. C++11 In-Class Initializers
    7. 5.7. break and continue Statements
      1. break Statement
      2. continue Statement
    8. 5.8. Logical Operators
      1. Logical AND (&&) Operator
      2. Logical OR (||) Operator
      3. Logical Negation (!) Operator
      4. Logical Operators Example
      5. Summary of Operator Precedence and Associativity
    9. 5.9. Confusing the Equality (==) and Assignment (=) Operators
      1. lvalues and rvalues
    10. 5.10. Wrap-Up
  11. 6. Functions and an Introduction to Recursion
    1. 6.1. Introduction
    2. 6.2. Math Library Functions
    3. 6.3. Function Definitions with Multiple Parameters
      1. Function Prototype for maximum
      2. Logic of Function maximum
      3. Returning Control from a Function to Its Caller
    4. 6.4. Function Prototypes and Argument Coercion
      1. Function Signatures
      2. Argument Coercion
      3. Argument Promotion Rules and Implicit Conversions1
      4. Conversions Can Result in Incorrect Values
    5. 6.5. C++ Standard Library Headers
    6. 6.6. Case Study: Random Number Generation
      1. Rolling a Six-Sided Die
      2. Rolling a Six-Sided Die 6,000,000 Times
      3. Randomizing the Random Number Generator
      4. Seeding the Random Number Generator with srand
      5. Seeding the Random Number Generator with the Current Time
      6. Scaling and Shifting Random Numbers
    7. 6.7. Case Study: Game of Chance; Introducing enum
      1. enum Type Status
      2. Winning or Losing on the First Roll
      3. Continuing to Roll
      4. C++11—Scoped enums
      5. C++11—Specifying the Type of an enum’s Constants
    8. 6.8. C++11 Random Numbers
      1. Rolling a Six-Sided Die
    9. 6.9. Storage Classes and Storage Duration
      1. Storage Duration
      2. Scope
      3. Linkage
      4. Storage Duration
      5. Local Variables and Automatic Storage Duration
      6. Register Variables
      7. Static Storage Duration
      8. Identifiers with Static Storage Duration
      9. static Local Variables
    10. 6.10. Scope Rules
      1. Block Scope
      2. Function Scope
      3. Global Namespace Scope
      4. Function-Prototype Scope
      5. Scope Demonstration
    11. 6.11. Function Call Stack and Activation Records
      1. Function-Call Stack
      2. Stack Frames
      3. Automatic Variables and Stack Frames
      4. Stack Overflow
      5. Function Call Stack in Action
    12. 6.12. Functions with Empty Parameter Lists
    13. 6.13. Inline Functions
    14. 6.14. References and Reference Parameters
      1. Reference Parameters
      2. Passing Arguments by Value and by Reference
      3. References as Aliases within a Function
      4. Returning a Reference from a Function
    15. 6.15. Default Arguments
    16. 6.16. Unary Scope Resolution Operator
    17. 6.17. Function Overloading
      1. Overloaded square Functions
      2. How the Compiler Differentiates Among Overloaded Functions
      3. Overloaded Operators
    18. 6.18. Function Templates
      1. C++11—Trailing Return Types for Functions
    19. 6.19. Recursion
      1. Recursion Concepts
      2. Factorial
      3. Iterative Factorial
      4. Recursive Factorial
      5. Evaluating 5!
      6. Using a Recursive factorial Function to Calculate Factorials
      7. Why We Chose Type unsigned long in This Example
      8. C++11 Type unsigned long long int
      9. Representing Even Larger Numbers
    20. 6.20. Example Using Recursion: Fibonacci Series
      1. Recursive Fibonacci Definition
      2. Evaluating fibonacci(3)
      3. Order of Evaluation of Operands
      4. Exponential Complexity
    21. 6.21. Recursion vs. Iteration
      1. Iterative Factorial Implementation
      2. Negatives of Recursion
    22. 6.22. Wrap-Up
  12. 7. Class Templates array and vector; Catching Exceptions
    1. 7.1. Introduction
    2. 7.2. arrays
    3. 7.3. Declaring arrays
    4. 7.4. Examples Using arrays
      1. 7.4.1. Declaring an array and Using a Loop to Initialize the array’s Elements
      2. 7.4.2. Initializing an array in a Declaration with an Initializer List
      3. 7.4.3. Specifying an array’s Size with a Constant Variable and Setting array Elements with Calculations
      4. 7.4.4. Summing the Elements of an array
      5. 7.4.5. Using Bar Charts to Display array Data Graphically
      6. 7.4.6. Using the Elements of an array as Counters
      7. 7.4.7. Using arrays to Summarize Survey Results
      8. 7.4.8. Static Local arrays and Automatic Local arrays
    5. 7.5. Range-Based for Statement
      1. Using the Range-Based for to Display an array’s Contents
      2. Using the Range-Based for to Modify an array’s Contents
      3. Using an Element’s Subscript
    6. 7.6. Case Study: Class GradeBook Using an array to Store Grades
      1. Storing Student Grades in an array in Class GradeBook
      2. Constructor
      3. Member Function processGrades
      4. Member Function getAverage
      5. Member Functions getMinimum and getMaximum
      6. Member Function outputBarChart
      7. Testing Class GradeBook
    7. 7.7. Sorting and Searching arrays
      1. Sorting
      2. Searching
      3. Demonstrating Functions sort and binary_search
    8. 7.8. Multidimensional arrays
      1. Nested Range-Based for Statements
      2. Nested Counter-Controlled for Statements
      3. Other Common array Manipulations
    9. 7.9. Case Study: Class GradeBook Using a Two-Dimensional array
      1. Storing Student Grades in a Two-Dimensional array in Class GradeBook
      2. Overview of Class GradeBook’s Functions
      3. Functions getMinimum and getMaximum
      4. Function outputBarChart
      5. Function outputGrades
      6. Function getAverage
      7. Testing Class GradeBook
    10. 7.10. Introduction to C++ Standard Library Class Template vector
      1. Creating vector Objects
      2. vector Member Function size; Function outputVector
      3. Function inputVector
      4. Comparing vector Objects for Inequality
      5. Initializing One vector with the Contents of Another
      6. Assigning vectors and Comparing vectors for Equality
      7. Using the [] Operator to Access and Modify vector Elements
      8. Exception Handling: Processing an Out-of-Range Subscript
      9. The try Statement
      10. Executing the catch Block
      11. what Member Function of the Exception Parameter
      12. Changing the Size of a vector
      13. C++11: List Initializing a vector
    11. 7.11. Wrap-Up
  13. 8. Pointers
    1. 8.1. Introduction
    2. 8.2. Pointer Variable Declarations and Initialization
      1. Indirection
      2. Declaring Pointers
      3. Initializing Pointers
      4. Null Pointers Prior to C++11
    3. 8.3. Pointer Operators
      1. Address (&) Operator
      2. Indirection (*) Operator
      3. Using the Address (&) and Indirection (*) Operators
      4. Precedence and Associativity of the Operators Discussed So Far
    4. 8.4. Pass-by-Reference with Pointers
      1. An Example of Pass-By-Value
      2. An Example of Pass-By-Reference with Pointers
      3. Insight: All Arguments Are Passed By Value
      4. Graphical Analysis of Pass-By-Value and Pass-By-Reference
    5. 8.5. Built-In Arrays
      1. Declaring a Built-In Array
      2. Accessing a Built-In Array’s Elements
      3. Initializing Built-In Arrays
      4. Passing Built-In Arrays to Functions
      5. Declaring Built-In Array Parameters
      6. C++11: Standard Library Functions begin and end
      7. Built-In Array Limitations
      8. Sometimes Built-In Arrays Are Required
    6. 8.6. Using const with Pointers
      1. 8.6.1. Nonconstant Pointer to Nonconstant Data
      2. 8.6.2. Nonconstant Pointer to Constant Data
      3. 8.6.3. Constant Pointer to Nonconstant Data
      4. 8.6.4. Constant Pointer to Constant Data
    7. 8.7. sizeof Operator
      1. Determining the Sizes of the Fundamental Types, a Built-In Array and a Pointer
    8. 8.8. Pointer Expressions and Pointer Arithmetic
      1. Adding Integers to and Subtracting Integers from Pointers
      2. Subtracting Pointers
      3. Pointer Assignment
      4. Cannot Dereference a void *
      5. Comparing Pointers
    9. 8.9. Relationship Between Pointers and Built-In Arrays
      1. Pointer/Offset Notation
      2. Pointer/Offset Notation with the Built-In Array’s Name as the Pointer
      3. Pointer/Subscript Notation
      4. The Name of a Built-In Array Is Not Modifiable
      5. Demonstrating the Relationship Between Pointers and Built-In Arrays
    10. 8.10. Pointer-Based Strings
      1. Characters and Character Constants
      2. Strings
      3. Pointer-Based Strings
      4. String Literals as Initializers
      5. Character Constants as Initializers
      6. Accessing Characters in a C String
      7. Reading Strings into char Built-In Arrays with cin
      8. Reading Lines of Text into char Built-In Arrays with cin.getline
      9. Displaying C Strings
    11. 8.11. Wrap-Up
  14. 9. Classes: A Deeper Look; Throwing Exceptions
    1. 9.1. Introduction
    2. 9.2. Time Class Case Study
      1. Time Class Definition
      2. Time Class Member Functions
      3. Time Class Member Function setTime and Throwing Exceptions
      4. Time Class Member Function printUniversal
      5. Time Class Member Function printStandard
      6. Defining Member Functions Outside the Class Definition; Class Scope
      7. Member Functions vs. Global Functions (Also Called Free Functions)
      8. Using Class Time
      9. Calling setTime with Invalid Values
      10. Looking Ahead to Composition and Inheritance
      11. Object Size
    3. 9.3. Class Scope and Accessing Class Members
      1. Class Scope and Block Scope
      2. Dot (.) and Arrow (->) Member Selection Operators
      3. Accessing public Class Members Through Objects, References and Pointers
    4. 9.4. Access Functions and Utility Functions
      1. Access Functions
      2. Utility Functions
    5. 9.5. Time Class Case Study: Constructors with Default Arguments
      1. Notes Regarding Class Time’s Set and Get Functions and Constructor
      2. C++11: Using List Initializers to Call Constructors
      3. C++11: Overloaded Constructors and Delegating Constructors
    6. 9.6. Destructors
    7. 9.7. When Constructors and Destructors Are Called
      1. Constructors and Destructors for Objects in Global Scope
      2. Constructors and Destructors for Local Objects
      3. Constructors and Destructors for static Local Objects
      4. Demonstrating When Constructors and Destructors Are Called
    8. 9.8. Time Class Case Study: A Subtle Trap—Returning a Reference or a Pointer to a private Data Member
    9. 9.9. Default Memberwise Assignment
    10. 9.10. const Objects and const Member Functions
      1. Using const and Non-const Member Functions
    11. 9.11. Composition: Objects as Members of Classes
      1. Employee Constructor’s Member Initializer List
      2. Date Class’s Default Copy Constructor
      3. Testing Classes Date and Employee
      4. What Happens When You Do Not Use the Member Initializer List?
    12. 9.12. friend Functions and friend Classes
      1. Declaring a friend
      2. Modifying a Class’s private Data with a Friend Function
      3. Overloaded friend Functions
    13. 9.13. Using the this Pointer
      1. Using the this Pointer to Avoid Naming Collisions
      2. Type of the this Pointer
      3. Implicitly and Explicitly Using the this Pointer to Access an Object’s Data Members
      4. Using the this Pointer to Enable Cascaded Function Calls
    14. 9.14. static Class Members
      1. Motivating Class-Wide Data
      2. Scope and Initialization of static Data Members
      3. Accessing static Data Members
      4. Demonstrating static Data Members
    15. 9.15. Wrap-Up
  15. 10. Operator Overloading; Class string
    1. 10.1. Introduction
    2. 10.2. Using the Overloaded Operators of Standard Library Class string
    3. 10.3. Fundamentals of Operator Overloading
      1. Operators That Cannot Be Overloaded
      2. Rules and Restrictions on Operator Overloading
    4. 10.4. Overloading Binary Operators
      1. Binary Overloaded Operators as Member Functions
      2. Binary Overloaded Operators as Non-Member Functions
    5. 10.5. Overloading the Binary Stream Insertion and Stream Extraction Operators
      1. Overloading the Stream Extraction (>>) Operator
      2. Overloading the Stream Insertion (<<) Operator
      3. Overloaded Operators as Non-Member friend Functions
      4. Why Overloaded Stream Insertion and Stream Extraction Operators Are Overloaded as Non-Member Functions
    6. 10.6. Overloading Unary Operators
      1. Unary Overloaded Operators as Member Functions
      2. Unary Overloaded Operators as Non-Member Functions
    7. 10.7. Overloading the Unary Prefix and Postfix ++ and -- Operators
      1. Overloading the Prefix Increment Operator
      2. Overloading the Postfix Increment Operator
    8. 10.8. Case Study: A Date Class
      1. Date Class Prefix Increment Operator
      2. Date Class Postfix Increment Operator
    9. 10.9. Dynamic Memory Management
      1. Obtaining Dynamic Memory with new
      2. Releasing Dynamic Memory with delete
      3. Initializing Dynamic Memory
      4. Dynamically Allocating Built-In Arrays with new []
      5. C++11: Using a List Initializer with a Dynamically Allocated Built-In Array
      6. Releasing Dynamically Allocated Built-In Arrays with delete []
      7. C++11: Managing Dynamically Allocated Memory with unique_ptr
    10. 10.10. Case Study: Array Class
      1. 10.10.1. Using the Array Class
      2. 10.10.2. Array Class Definition
    11. 10.11. Operators as Member vs. Non-Member Functions
      1. Commutative Operators
    12. 10.12. Converting Between Types
      1. Conversion Operators
      2. Overloaded Cast Operator Functions
      3. Implicit Calls to Cast Operators and Conversion Constructors
    13. 10.13. explicit Constructors and Conversion Operators
      1. Accidentally Using a Single-Argument Constructor as a Conversion Constructor
      2. Preventing Implicit Conversions with Single-Argument Constructors
      3. C++11: explicit Conversion Operators
    14. 10.14. Overloading the Function Call Operator ()
    15. 10.15. Wrap-Up
  16. 11. Object-Oriented Programming: Inheritance
    1. 11.1. Introduction
    2. 11.2. Base Classes and Derived Classes
      1. CommunityMember Class Hierarchy
      2. Shape Class Hierarchy
    3. 11.3. Relationship between Base and Derived Classes
      1. 11.3.1. Creating and Using a CommissionEmployee Class
      2. 11.3.2. Creating a BasePlusCommissionEmployee Class Without Using Inheritance
      3. 11.3.3. Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy
      4. 11.3.4. CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data
      5. 11.3.5. CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data
    4. 11.4. Constructors and Destructors in Derived Classes
      1. C++11: Inheriting Base Class Constructors
    5. 11.5. public, protected and private Inheritance
    6. 11.6. Software Engineering with Inheritance
    7. 11.7. Wrap-Up
  17. 12. Object-Oriented Programming: Polymorphism
    1. 12.1. Introduction
      1. Implementing for Extensibility
      2. Optional Discussion of Polymorphism “Under the Hood”
    2. 12.2. Introduction to Polymorphism: Polymorphic Video Game
    3. 12.3. Relationships Among Objects in an Inheritance Hierarchy
      1. 12.3.1. Invoking Base-Class Functions from Derived-Class Objects
      2. 12.3.2. Aiming Derived-Class Pointers at Base-Class Objects
      3. 12.3.3. Derived-Class Member-Function Calls via Base-Class Pointers
      4. 12.3.4. Virtual Functions and Virtual Destructors
    4. 12.4. Type Fields and switch Statements
    5. 12.5. Abstract Classes and Pure virtual Functions
      1. Pure Virtual Functions
      2. Device Drivers and Polymorphism
    6. 12.6. Case Study: Payroll System Using Polymorphism
      1. 12.6.1. Creating Abstract Base Class Employee
      2. 12.6.2. Creating Concrete Derived Class SalariedEmployee
      3. 12.6.3. Creating Concrete Derived Class CommissionEmployee
      4. 12.6.4. Creating Indirect Concrete Derived Class BasePlusCommissionEmployee
      5. 12.6.5. Demonstrating Polymorphic Processing
    7. 12.7. (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”
      1. Employee Class vtable
      2. SalariedEmployee Class vtable
      3. CommissionEmployee Class vtable
      4. BasePlusCommissionEmployee Class vtable
      5. Inheriting Concrete virtual Functions
      6. Three Levels of Pointers to Implement Polymorphism
    8. 12.8. Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info
      1. Determining an Object’s Type with dynamic_cast
      2. Calculating the Current Employee’s Earnings
      3. Displaying an Employee’s Type
      4. Compilation Errors That We Avoided By Using dynamic_cast
    9. 12.9. Wrap-Up
  18. 13. Stream Input/Output: A Deeper Look
    1. 13.1. Introduction
    2. 13.2. Streams
      1. 13.2.1. Classic Streams vs. Standard Streams
      2. 13.2.2. iostream Library Headers
      3. 13.2.3. Stream Input/Output Classes and Objects
    3. 13.3. Stream Output
      1. 13.3.1. Output of char * Variables
      2. 13.3.2. Character Output Using Member Function put
    4. 13.4. Stream Input
      1. 13.4.1. get and getline Member Functions
      2. 13.4.2. istream Member Functions peek, putback and ignore
      3. 13.4.3. Type-Safe I/O
    5. 13.5. Unformatted I/O Using read, write and gcount
    6. 13.6. Introduction to Stream Manipulators
      1. 13.6.1. Integral Stream Base: dec, oct, hex and setbase
      2. 13.6.2. Floating-Point Precision (precision, setprecision)
      3. 13.6.3. Field Width (width, setw)
      4. 13.6.4. User-Defined Output Stream Manipulators
    7. 13.7. Stream Format States and Stream Manipulators
      1. 13.7.1. Trailing Zeros and Decimal Points (showpoint)
      2. 13.7.2. Justification (left, right and internal)
      3. 13.7.3. Padding (fill, setfill)
      4. 13.7.4. Integral Stream Base (dec, oct, hex, showbase)
      5. 13.7.5. Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)
      6. 13.7.6. Uppercase/Lowercase Control (uppercase)
      7. 13.7.7. Specifying Boolean Format (boolalpha)
      8. 13.7.8. Setting and Resetting the Format State via Member Function flags
    8. 13.8. Stream Error States
    9. 13.9. Tying an Output Stream to an Input Stream
    10. 13.10. Wrap-Up
  19. 14. File Processing
    1. 14.1. Introduction
    2. 14.2. Files and Streams
      1. File-Processing Class Templates
    3. 14.3. Creating a Sequential File
      1. Opening a File
      2. Opening a File via the open Member Function
      3. Testing Whether a File Was Opened Successfully
      4. The Overloaded void * Operator
      5. Processing Data
      6. Closing a File
      7. The Sample Execution
    4. 14.4. Reading Data from a Sequential File
      1. Opening a File for Input
      2. Ensuring That the File Was Opened
      3. Reading from the File
      4. File Position Pointers
      5. Credit Inquiry Program
    5. 14.5. Updating Sequential Files
    6. 14.6. Random-Access Files
    7. 14.7. Creating a Random-Access File
      1. Writing Bytes with ostream Member Function write
      2. Converting Between Pointer Types with the reinterpret_cast Operator
      3. Credit Processing Program
      4. Opening a File for Output in Binary Mode
    8. 14.8. Writing Data Randomly to a Random-Access File
      1. Opening a File for Input and Output in Binary Mode
      2. Positioning the File Position Pointer
    9. 14.9. Reading from a Random-Access File Sequentially
    10. 14.10. Case Study: A Transaction-Processing Program
    11. 14.11. Object Serialization
    12. 14.12. Wrap-Up
  20. 15. Standard Library Containers and Iterators
    1. 15.1. Introduction
      1. Containers, Iterators and Algorithms
      2. Common Member Functions Among Containers
      3. Iterators
      4. Algorithms
      5. Custom Templatized Data Structures
    2. 15.2. Introduction to Containers
      1. Containers Overview
      2. Near Containers
      3. Common Container Functions
      4. First-Class Container Common Nested Types
      5. Requirements for Container Elements
    3. 15.3. Introduction to Iterators
      1. Using istream_iterator for Input and ostream_iterator for Output
      2. istream_iterator
      3. ostream_iterator
      4. Iterator Categories and Iterator Category Hierarchy
      5. Container Support for Iterators
      6. Predefined Iterator typedefs
      7. Iterator Operations
    4. 15.4. Introduction to Algorithms
    5. 15.5. Sequence Containers
      1. Performance and Choosing the Appropriate Container
      2. 15.5.1. vector Sequence Container
      3. 15.5.2. list Sequence Container
      4. 15.5.3. deque Sequence Container
    6. 15.6. Associative Containers
      1. 15.6.1. multiset Associative Container
      2. 15.6.2. set Associative Container
      3. 15.6.3. multimap Associative Container
      4. 15.6.4. map Associative Container
    7. 15.7. Container Adapters
      1. 15.7.1. stack Adapter
      2. 15.7.2. queue Adapter
      3. 15.7.3. priority_queue Adapter
    8. 15.8. Class bitset
    9. 15.9. Wrap-Up
      1. Recommended Reading
  21. 16. Standard Library Algorithms
    1. 16.1. Introduction
    2. 16.2. Minimum Iterator Requirements
      1. Iterator Invalidation
    3. 16.3. Algorithms
      1. 16.3.1. fill, fill_n, generate and generate_n
      2. 16.3.2. equal, mismatch and lexicographical_compare
      3. 16.3.3. remove, remove_if, remove_copy and remove_copy_if
      4. 16.3.4. replace, replace_if, replace_copy and replace_copy_if
      5. 16.3.5. Mathematical Algorithms
      6. 16.3.6. Basic Searching and Sorting Algorithms
      7. 16.3.7. swap, iter_swap and swap_ranges
      8. 16.3.8. copy_backward, merge, unique and reverse
      9. 16.3.9. inplace_merge, unique_copy and reverse_copy
      10. 16.3.10. Set Operations
      11. 16.3.11. lower_bound, upper_bound and equal_range
      12. 16.3.12. Heapsort
      13. 16.3.13. min, max, minmax and minmax_element
    4. 16.4. Function Objects
      1. Advantages of Function Objects Over Function Pointers
      2. Predefined Function Objects of the Standard Template Library
      3. Using the accumulate Algorithm
      4. Function sumSquares
      5. Class SumSquaresClass
      6. Passing Function Pointers and Function Objects to Algorithm accumulate
    5. 16.5. Lambda Expressions
    6. 16.6. Standard Library Algorithm Summary
      1. Mutating Sequence Algorithms
      2. Nonmodifying Sequence Algorithms
      3. Sorting and Related Algorithms
      4. Numerical Algorithms
    7. 16.7. Wrap-Up
  22. 17. Exception Handling: A Deeper Look
    1. 17.1. Introduction
    2. 17.2. Example: Handling an Attempt to Divide by Zero
      1. Defining an Exception Class to Represent the Type of Problem That Might Occur
      2. Demonstrating Exception Handling
      3. Enclosing Code in a try Block
      4. Defining a catch Handler to Process a DivideByZeroException
      5. Termination Model of Exception Handling
      6. Flow of Program Control When the User Enters a Nonzero Denominator
      7. Flow of Program Control When the User Enters a Denominator of Zero
    3. 17.3. Rethrowing an Exception
    4. 17.4. Stack Unwinding
    5. 17.5. When to Use Exception Handling
      1. C++11: Declaring Functions That Do Not Throw Exceptions
    6. 17.6. Constructors, Destructors and Exception Handling
      1. Initializing Local Objects to Acquire Resources
    7. 17.7. Exceptions and Inheritance
    8. 17.8. Processing new Failures
      1. new Throwing bad_alloc on Failure
      2. new Returning nullptr on Failure
      3. Handling new Failures Using Function set_new_handler
    9. 17.9. Class unique_ptr and Dynamic Memory Allocation
      1. unique_ptr Notes
      2. unique_ptr to a Built-In Array
    10. 17.10. Standard Library Exception Hierarchy
    11. 17.11. Wrap-Up
  23. 18. Introduction to Custom Templates
    1. 18.1. Introduction
    2. 18.2. Class Templates
      1. Creating Class Template Stack<T>
      2. Class Template Stack<T>’s Data Representation
      3. Class Template Stack<T>’s Member Functions
      4. Declaring a Class Template’s Member Functions Outside the Class Template Definition
      5. Testing Class Template Stack<T>
    3. 18.3. Function Template to Manipulate a Class-Template Specialization Object
    4. 18.4. Nontype Parameters
    5. 18.5. Default Arguments for Template Type Parameters
    6. 18.6. Overloading Function Templates
      1. Matching Process for Overloaded Functions
    7. 18.7. Wrap-Up
  24. 19. Class string and String Stream Processing: A Deeper Look
    1. 19.1. Introduction
      1. Initializing a string Object
      2. strings Are Not Necessarily Null Terminated
      3. Length of a string
      4. Processing strings
      5. string I/O
      6. Validating Input
    2. 19.2. string Assignment and Concatenation
    3. 19.3. Comparing strings
    4. 19.4. Substrings
    5. 19.5. Swapping strings
    6. 19.6. string Characteristics
    7. 19.7. Finding Substrings and Characters in a string
    8. 19.8. Replacing Characters in a string
    9. 19.9. Inserting Characters into a string
    10. 19.10. Conversion to Pointer-Based char * Strings
    11. 19.11. Iterators
    12. 19.12. String Stream Processing
      1. Demonstrating ostringstream
      2. Demonstrating istringstream
    13. 19.13. C++11 Numeric Conversion Functions
      1. Converting Numeric Values to string Objects
      2. Converting string Objects to Numeric Values
      3. Functions That Convert strings to Integral Types
      4. Functions That Convert strings to Floating-Point Types
    14. 19.14. Wrap-Up
  25. 20. Bits, Characters, C Strings and structs
    1. 20.1. Introduction
    2. 20.2. Structure Definitions
    3. 20.3. typedef
    4. 20.4. Example: Card Shuffling and Dealing Simulation
    5. 20.5. Bitwise Operators
      1. Printing a Binary Representation of an Integral Value
      2. Bitwise AND Operator (&)
      3. Bitwise Inclusive OR Operator (|)
      4. Bitwise Exclusive OR (^)
      5. Bitwise Complement (~)
      6. Bitwise Shift Operators
      7. Left-Shift Operator
      8. Right-Shift Operator
      9. Bitwise Assignment Operators
    6. 20.6. Bit Fields
    7. 20.7. Character-Handling Library
    8. 20.8. C String-Manipulation Functions
      1. Copying Strings with strcpy and strncpy
      2. Concatenating Strings with strcat and strncat
      3. Comparing Strings with strcmp and strncmp
      4. Tokenizing a String with strtok
      5. Determining String Lengths
    9. 20.9. C String-Conversion Functions
    10. 20.10. Search Functions of the C String-Handling Library
    11. 20.11. Memory Functions of the C String-Handling Library
    12. 20.12. Wrap-Up
  26. 21. Other Topics
    1. 21.1. Introduction
    2. 21.2. const_cast Operator
    3. 21.3. mutable Class Members
      1. Mechanical Demonstration of a mutable Data Member
    4. 21.4. namespaces
      1. Defining namespaces
      2. Accessing namespace Members with Qualified Names
      3. using Directives Should Not Be Placed in Headers
      4. Aliases for namespace Names
    5. 21.5. Operator Keywords
    6. 21.6. Pointers to Class Members (.* and ->*)
    7. 21.7. Multiple Inheritance
      1. Resolving Ambiguity Issues That Arise When a Derived Class Inherits Member Functions of the Same Name from Multiple Base Classes
      2. Demonstrating the Is-A Relationships in Multiple Inheritance
    8. 21.8. Multiple Inheritance and virtual Base Classes
      1. Compilation Errors Produced When Ambiguity Arises in Diamond Inheritance
      2. Eliminating Duplicate Subobjects with virtual Base-Class Inheritance
      3. Constructors in Multiple-Inheritance Hierarchies with virtual Base Classes
    9. 21.9. Wrap-Up
  27. 22. ATM Case Study, Part 1: Object-Oriented Design with the UML
    1. 22.1. Introduction
    2. 22.2. Introduction to Object-Oriented Analysis and Design
    3. 22.3. Examining the ATM Requirements Document
      1. Requirements Document
      2. Analyzing the ATM System
      3. Use Case Diagrams
      4. Designing the ATM System
      5. Web Resources
      6. Self-Review Exercises for Section 22.3
    4. 22.4. Identifying the Classes in the ATM Requirements Document
      1. Identifying the Classes in a System
      2. Modeling Classes
      3. Self-Review Exercises for Section 22.4
    5. 22.5. Identifying Class Attributes
      1. Identifying Attributes
      2. Modeling Attributes
      3. Self-Review Exercises for Section 22.5
    6. 22.6. Identifying Objects’ States and Activities
      1. State Machine Diagrams
      2. Activity Diagrams
      3. Self-Review Exercises for Section 22.6
    7. 22.7. Identifying Class Operations
      1. Identifying Operations
      2. Modeling Operations
      3. Operations of Class BankDatabase and Class Account
      4. Operations of Class Screen
      5. Operations of Class Keypad
      6. Operations of Class CashDispenser and Class DepositSlot
      7. Operations of Class ATM
      8. Identifying and Modeling Operation Parameters
      9. Self-Review Exercises for Section 22.7
    8. 22.8. Indicating Collaboration Among Objects
      1. Identifying the Collaborations in a System
      2. Interaction Diagrams
      3. Communication Diagrams
      4. Sequence of Messages in a Communication Diagram
      5. Sequence Diagrams
      6. Self-Review Exercises for Section 22.8
    9. 22.9. Wrap-Up
      1. Answers to Self-Review Exercises
  28. 23. ATM Case Study, Part 2: Implementing an Object-Oriented Design
    1. 23.1. Introduction
    2. 23.2. Starting to Program the Classes of the ATM System
      1. Visibility
      2. Navigability
      3. Implementing the ATM System from Its UML Design
      4. Self-Review Exercises for Section 23.2
    3. 23.3. Incorporating Inheritance into the ATM System
      1. Processing Transactions Polymorphically
      2. Additional Attribute of Classes Withdrawal and Deposit
      3. Class Diagram with Transaction Hierarchy Incorporated
      4. Implementing the ATM System Design Incorporating Inheritance
      5. ATM Case Study Wrap-Up
      6. Self-Review Exercises for Section 23.3
    4. 23.4. ATM Case Study Implementation
      1. 23.4.1. Class ATM
      2. 23.4.2. Class Screen
      3. 23.4.3. Class Keypad
      4. 23.4.4. Class CashDispenser
      5. 23.4.5. Class DepositSlot
      6. 23.4.6. Class Account
      7. 23.4.7. Class BankDatabase
      8. 23.4.8. Class Transaction
      9. 23.4.9. Class BalanceInquiry
      10. 23.4.10. Class Withdrawal
      11. 23.4.11. Class Deposit
      12. 23.4.12. Test Program ATMCaseStudy.cpp
    5. 23.5. Wrap-Up
      1. Answers to Self-Review Exercises
  29. A. Operator Precedence and Associativity
  30. B. ASCII Character Set
  31. C. Fundamental Types
  32. D. Number Systems
    1. D.1. Introduction
    2. D.2. Abbreviating Binary Numbers as Octal and Hexadecimal Numbers
    3. D.3. Converting Octal and Hexadecimal Numbers to Binary Numbers
    4. D.4. Converting from Binary, Octal or Hexadecimal to Decimal
    5. D.5. Converting from Decimal to Binary, Octal or Hexadecimal
    6. D.6. Negative Binary Numbers: Two’s Complement Notation
  33. E. Preprocessor
    1. E.1. Introduction
    2. E.2. #include Preprocessing Directive
    3. E.3. #define Preprocessing Directive: Symbolic Constants
    4. E.4. #define Preprocessing Directive: Macros
    5. E.5. Conditional Compilation
    6. E.6. #error and #pragma Preprocessing Directives
    7. E.7. Operators # and ##
    8. E.8. Predefined Symbolic Constants
    9. E.9. Assertions
    10. E.10. Wrap-Up
  34. Index