You are previewing Practical C++.
O'Reilly logo
Practical C++

Book Description

Practical C++ provides the essential pieces of C++ programming information needed by beginning-intermediate programmers to execute the most frequently used tasks and functions. Each chapter is a task-oriented module covering a group of related features or tasks full of real-world, practical examples. Practical C++ provides a complete condensed package of easily navigable reference tutorial information on C++.

Table of Contents

  1. Practical C++
    1. About the Author
    2. Acknowledgments
    3. Tell Us What You Think!
    4. Introduction
        1. Who Should Read This Book
        2. What Experience Do You Need?
        3. The History and Evolution of C++
        4. How This Book Is Organized
          1. Part I: C++ Basics
          2. Part II: Beyond the Basics
          3. Part III: Object-Oriented Programming
          4. Part IV: Using the Standard C++ Library
        5. Formatting Conventions
        6. Source Code on the Web: The Practical C++ Web Site
        7. How to Contact the Author
    5. I. C++ Basics
      1. 1. Getting Started with C++
          1. Creating a C++ Program
            1. Writing Source Code
            2. Compiling and Linking Source Files
              1. Building Executable Programs
              2. Compiling
              3. Linking
            3. Testing Your Code
            4. Case Sensitivity
          2. Introducing Functions
            1. Declaring Functions
            2. Making a Sandwich: A Study in Problem Solving
            3. Creating and Using Code Blocks
            4. Introducing the main() Function
          3. Using C-Style and C++-Style Comments
            1. C-Style Comments
            2. C++-Style Comments
            3. Important C++ Keywords
          4. Using Basic Input/Output Streams
            1. Realizing That Input/Output Streams Are C++ Objects
            2. Using the #include Directive
            3. The Standard Output Stream
            4. The Standard Input Stream
            5. The Standard Error Stream
      2. 2. Exploring C++ Data Types, Variables, and Constants
          1. Understanding C++ Data Types
            1. Exploring the Fundamental Data Types
              1. The Integer Type
              2. The Character Type
              3. The Boolean Type
              4. Floating-Point and Double-Precision Types
            2. Introducing Derived Data Types
              1. Arrays
              2. Pointers and References
              3. Structures, Unions, and Classes
            3. Introducing the Standard String
          2. Using Variables
            1. Declaring and Defining Variables
            2. Initializing Variables
          3. Using Named Constants
            1. Declaring and Initializing Named Constants
            2. Types of Constants
              1. Integer Constants
              2. Character Constants
              3. Floating-Point Constants
              4. String Literals
            3. Using Named Constants in Source Code
          4. Resolving the Scope of Variables and Constants
          5. Understanding Declarations and Definitions
            1. Declaration Specifiers and Declarators
            2. Specifying Linkage
            3. Specifying Storage Class
          6. Casting Data Types
            1. Casting Implicitly
            2. Casting Explicitly
      3. 3. Examining Expressions and Statements
          1. Overview
          2. Understanding Expressions
            1. Using Operators and Operands
            2. Primary Expressions
            3. Postfix Operators and Postfix Expressions
            4. Unary Operator Expressions
            5. Binary Operator Expressions
            6. Pointer-to-Member Operator Expressions
            7. Conditional Operator Expressions
            8. Explicit Cast Expressions
          3. Using Statements
            1. Expression Statements
            2. Compound Statements
            3. Selection Statements
            4. Iteration Statements
            5. Declaration Statements
            6. Jump Statements
            7. Label Statements
      4. 4. Using Conditional Expressions and Selection Statements
          1. Using Boolean Conditions and Expressions
            1. Understanding Boolean Expressions
            2. Using the Logical Operators
              1. Logical AND
              2. Logical OR
              3. Logical NOT
            3. Evaluating Boolean Conditions with Truth Tables
              1. A Truth Table for the AND Operator
              2. A Truth Table for the OR Operator
              3. A Truth Table for the NOT Operator
              4. Creating More Complex Truth Tables
            4. Using the Conditional Operator
            5. Understanding Operator Precedence
          2. Using Selection Statements
            1. Using the if Statement
              1. The else Clause
              2. Nesting if Statements
            2. Using the switch Statement
          3. Using Enumerations
      5. 5. Building Iteration Loops
          1. Why Use Loops?
          2. Phases of Loop Execution
          3. Examining Iteration Statements
            1. Using the for Statement
              1. Using a for Loop for Temperature Conversion
              2. Creating Infinite Loops Using the for Statement
            2. Using the while Statement
              1. Using a while Loop for Temperature Conversion
              2. Creating Infinite Loops Using the while Statement
            3. Using the do Statement
              1. Using a do Loop for Temperature Conversion
              2. Creating Infinite Loops Using the do Statement
          4. Designing Loops
            1. Designing the Flow of Interactive Program Control
            2. Using break and continue for Greater Loop Control
            3. Using Nested Loops
            4. Putting It All Together
      6. 6. Using Arrays
          1. Why Use Arrays?
          2. Understanding One-Dimensional Arrays
            1. Defining an Array
            2. Accessing Array Elements
            3. Accessing Array Elements Through Iteration
            4. Initializing One-Dimensional Arrays During Declaration
            5. Using Parallel Arrays
          3. Understanding Two-Dimensional Arrays
            1. Processing Two-Dimensional Arrays
            2. Working with Columns
            3. Working with Rows
            4. Implementing a Multiplication Table
          4. Multi-Dimensional Arrays
      7. 7. Creating and Using Functions
          1. Why Use Functions?
          2. Various Types of Functions
            1. Void Functions
            2. Functions That Return a Value
            3. Inline Functions
          3. Components of a Function
            1. Function Return Types and Values
            2. Function Parameters
            3. Default Parameters
          4. Creating Your Own Functions
            1. Declaring a Function
            2. Defining a Function
            3. Using Local Variables
          5. Using Command-Line Arguments
            1. Getting the Number of Arguments
            2. Getting and Using the Arguments
              1. A Command-Line Argument-Processing Program
              2. Analyzing the Program Code
          6. Using Variable-Argument Lists
          7. Getting Modular with Functions
          8. Specifying Function Linkage
    6. II. Beyond the Basics
      1. 8. Overloading Functions
          1. Why Use Function Overloading?
          2. Types of Function Overloading
            1. Overloading by Parameter Type
              1. Changing the Data Types of Function Parameters
              2. Changing the Number of Function Parameters
              3. Changing the Number of Function Parameters and Their Data Types
              4. Changing a Function’s Return Data Type
            2. Overloading Functions by Operation
          3. Examining the Internals of Function Overloading
            1. Mangling Function Names for Type-Safe Linkage
            2. Function Calls and the Stack
              1. Stepping Through the Stack of Function Calls
      2. 9. Using Structures and Unions
          1. Creating New Data Types with Structures
            1. Adding Data Types as Structure Members
            2. Creating Arrays of Structures
            3. Structures as Data Members
            4. Presenting a Payroll Processing Program
            5. Examining the Payroll Processing Program
            6. Adding Functions as Members
          2. Using Unions for Efficient Storage
            1. Accessing Union Members
            2. Defining Structures as Union Members
            3. Defining Unions as Structure Members
      3. 10. Using Pointers and References
          1. Overview
          2. Understanding Pointers
            1. Why Use Pointers?
            2. Memory Addressing
            3. Declaring a Pointer
            4. Dereferencing a Pointer
            5. Getting Pointers to Structures
            6. Using Pointer-to-Member Notation
            7. Using Pointers as Function Parameters
            8. Using Arrays as Pointers
            9. Passing Arrays to Functions
              1. Passing Array Elements
              2. Passing an Entire Array
            10. Using Pointer Arithmetic
          3. Understanding References
            1. Declaring and Initializing a Reference
            2. Using References as Function Parameters
            3. Using References as Function Return Values
      4. 11. Allocating Memory in C++
          1. Allocating Static Memory
          2. Allocating Dynamic Memory
            1. The Stack
            2. The Free Store (The Heap)
          3. Using the new and delete Operators
            1. Using the new Operator
            2. Using the delete Operator
          4. Allocating Arrays
          5. Creating Dynamic Arrays of Structures
          6. Dealing with Allocation Errors
            1. Detecting Memory Allocation Failures
            2. Using the set_new_handler() Function
          7. Creating Your Own new and delete Operators
      5. 12. Understanding the Preprocessor
          1. Introducing the Preprocessor
          2. Understanding Preprocessing Phases
          3. Understanding Preprocessor Directives
            1. Including Files
            2. Defining and Undefining Symbols
            3. Compiling Conditionally
              1. Getting Platform Independent
              2. Single-File Inclusion
              3. Excluding Debug Code
            4. Outputting Errors
            5. Understanding Pragmas
          4. Exploring Macros
            1. Defining Macros
            2. Expanding Macros
          5. Using Preprocessor Operators
            1. Using the # Operator
            2. Using the ## Operator
          6. Considering Internationalization Issues
            1. Digraph Sequences
            2. Trigraph Sequences
            3. Keyword Sequences
          7. Examining Predefined Macros
      6. 13. Creating and Using Function Pointers
          1. How Can a Function Have a Pointer?
          2. Some Reasons for Declaring Pointers to Functions
          3. Declaring and Using Function Pointers
            1. Declaring a New Data Type
            2. Using a Function Pointer Type
            3. Creating Arrays of Function Pointers
            4. Using Pointers to Overloaded Functions
            5. Using Pointers to Member Functions
      7. 14. Overloading Operators
          1. Why Overload Operators?
            1. An Operator Is a Function Call
            2. Overloading an Operator
          2. Some Examples of Operator Overloading
            1. Overloading the Assignment Operator
            2. Overloading Relational Operators
            3. Overloading the Function Call Operator
      8. 15. Bulletproofing Programs with Assertions and Exceptions
          1. Devising Error Handling Strategies
            1. Handling Runtime Errors
            2. Handling Programming Errors
          2. Using Assertions
            1. Understanding the assert() Macro
            2. Some Guidelines for Using Assertions
            3. Demonstrating Assertions
          3. Handling Exceptions
            1. The try Block
            2. The catch Exception Handler
            3. The throw Statement
            4. Using Exception Handling
            5. Catching Unexpected Exceptions
            6. Cleaning Up Allocated Resources
      9. 16. Exploring Namespaces
          1. What Is a Namespace?
          2. Declaring a Namespace
            1. Unnamed Namespaces
          3. Getting Explicit Access to Namespaces
            1. The using Declaration
            2. The using Directive
          4. Using Namespace Aliases
          5. Using Composite Namespaces
          6. The Global Namespace
          7. The Standard Namespace
      10. 17. Analyzing the Mathematics of Memory
          1. Overview
          2. Examining Bases of Numeration Systems
          3. Reviewing the Decimal Numeration System (Base 10)
          4. Analyzing the Binary Numeration System (Base 2)
            1. Converting Binary to Decimal
            2. Converting Decimal to Binary
          5. Analyzing the Octal Numeration System (Base 8)
            1. Converting Octal to Decimal
            2. Converting Decimal to Octal
          6. A Base Conversion Program: CONVERT
            1. Analyzing the CONVERT Program
          7. Analyzing the Hexadecimal Numeration System (Base 16)
            1. Converting Hexadecimal to Decimal
            2. Converting Decimal to Hexadecimal
            3. Converting Hexadecimal to Binary
            4. Converting Binary to Hexadecimal
      11. 18. Manipulating Bits
          1. Working with Bits
            1. Understanding Bit Numbering
          2. Understanding Bit Fields
            1. Declaring a Bit Field
            2. Using a Bit Field
          3. Using Bit Operators
            1. The Bitwise AND Operator
            2. The Bitwise Inclusive OR Operator
            3. The Bitwise Exclusive OR Operator (XOR)
            4. The One’s Complement (NOT) Operator
          4. Manipulating Bits in Signed and Unsigned Numbers
          5. Exploring Bit-Shifting
            1. Using the Left-Shift Operator
            2. Using the Right-Shift Operator
      12. 19. Compiling, Linking, and Debugging
          1. Exploring the Compiler
            1. Understanding How Compilers Work
            2. Generated Object Code
          2. Resolving Compiler Errors
          3. Exploring the Linker
          4. Resolving Linker Errors
          5. Automating Builds Using Make Files
          6. Finding and Fixing Buggy Code
            1. Adding Debug Code
            2. Using the Preprocessor for Debugging
          7. Strategies for Testing and Debugging
    7. III. Object-Oriented Programming
      1. 20. Introducing Object-Oriented Programming
          1. Fundamentals of Object-Oriented Programming
            1. Moving from Procedural Programming to OOP
              1. Black Boxes and Bugs
              2. The Object Advantage
            2. Conceptualizing Objects
            3. Inheritance
            4. Introducing Polymorphism and Virtual Methods
            5. Object-Oriented Software Strategies
            6. Virtual Functions, Inherited Data Members, and Code Bloat
          2. Understanding Objects and Classes in C++
          3. Examining the Benefits of Code Reuse
            1. Stop Reinventing the Wheel
            2. Using Code Libraries
      2. 21. Understanding C++ Classes
          1. Creating C++ Classes
            1. Thinking About Class Design
            2. Encapsulating Data
            3. Declaring Classes
          2. Specifying Access Control
            1. Specifying Public Access
            2. Specifying Private Access
            3. Specifying Protected Access
          3. Defining Data Members
            1. Defining Non-Static Data Members
            2. Defining Static Data Members
            3. Using Constructors and Destructors
            4. Constructor Functions
            5. The Destructor Function
          4. Understanding the this Pointer
          5. Creating Copy Constructors
          6. Creating Member Operators
          7. Defining Member Functions
            1. Defining Static Member Functions
            2. Defining Non-Static Member Functions
          8. Using Pointer-to-Member Operator Expressions
          9. Designing and Implementing a Rectangle Class
            1. Designing the CRect Interface
            2. Implementing the CRect Class
      3. 22. Implementing Class Inheritance
          1. Understanding Base and Derived Classes
          2. Creating Class Hierarchies
            1. Designing Constructors and Destructors in a Hierarchy
            2. Overriding Base Class Functions
            3. Providing Scope Resolution for Base and Derived Classes
            4. Calling Base Class Functions
          3. Implementing a Class Hierarchy
          4. Using Friend Functions and Classes
            1. Using Friend Functions
            2. Using Friend Classes
          5. Creating Class Libraries
            1. Creating a Binary Library of Your Classes
            2. Using the Library
      4. 23. Understanding Polymorphism
          1. What Is Polymorphism?
          2. Getting Virtual
            1. Understanding Virtual Functions
            2. Creating Abstract Base Classes
            3. Defining Pure Virtual Functions
            4. Understanding Virtual Tables
          3. Using Pointers to Classes in a Hierarchy
            1. Polymorphing with Pointers
            2. Creating a Polymorphic Class Hierarchy
          4. Resolving Class Slicing
          5. Using Virtual Destructors
      5. 24. Implementing Dynamic Storage in C++
          1. Container Classes
          2. Creating Dynamic Arrays
            1. Creating a Dynamic Array Class
              1. Adding an Item
              2. Inserting an Item
              3. Finding an Item
              4. Deleting an Item
              5. Clearing All Array Elements
              6. Creating an Array Assignment Operator
              7. The Dynamic Array Class Interface
            2. Implementing the Dynamic Array Class
            3. Creating a Driver Program
          3. Creating Linked Lists
            1. Creating a Linked List Class
            2. Implementing a Node Class
            3. Adding a Node
            4. Traversing the List
            5. Finding an Item
            6. Inserting a Node
            7. Deleting All Nodes from the List
            8. Deleting a Node
            9. The Linked List Class Interface
            10. Implementing the Linked List Class
            11. Creating a Driver Program
      6. 25. Designing and Developing Object-Oriented Programs
          1. Problem-Solving Techniques
          2. Exploring the Development Process
            1. Analyzing System Requirements
            2. Undertaking a Project
            3. Identifying Classes Needed to Build the System
            4. Designing Interfaces
          3. Using Modular Source Files
            1. Creating Interface (Header) Files
            2. Creating Implementation Files
          4. Devising Class Methods
            1. Interfaces in Action
            2. Accessibility Issues
          5. Testing and Integrating Modules
            1. Avoiding Multiple Definitions
            2. Testing Functionality
            3. Integrating Modules
          6. Reusing Code
      7. 26. Using Templates
          1. Conceptualizing Templates
            1. Avoiding Redundant Code
            2. Creating Function Templates
            3. Using Multiple Generic Class Types
          2. Creating a Basic Class Template
            1. Designing a Template Skeleton
            2. Fleshing Out the Template
          3. Creating a More Complex Class Template
          4. Converting Existing Classes into Class Templates
          5. Creating a Generic Linked List Class
            1. Implementing a Generic Node Class
            2. Problems with Separating the Class Interface and Implementation
            3. Altering the List Class Interface and Implementation
            4. Adding a Node
            5. Traversing the List
            6. Finding an Item
            7. Inserting a Node
            8. Deleting All Nodes from the List
            9. Deleting a Node
            10. Creating a Driver Program
      8. 27. Using Dynamic Casting and Runtime Type Identification
          1. Getting and Using Runtime Type Information
            1. Old-School Type Casting (C Style)
            2. New-School Type Casting (C++ Style)
            3. Using the dynamic_cast Operator
            4. Using the const_cast Operator
            5. Using the reinterpret_cast Operator
            6. Using the static_cast Operator
    8. IV. Using the Standard C++ Library
      1. 28. Introducing the Standard C++ Library
          1. An Overview of the Standard C++ Library
            1. Using the Standard Namespace
            2. Identifying Standard C++ Header Files
            3. Introducing the Standard Template Library
            4. Using the Standard C Library
          2. Identifying the Components of the Standard C++ Library
            1. Diagnostics
            2. Input/Output
            3. Language Support
            4. Localization
            5. Numerics
            6. Strings
            7. Utilities
          3. Identifying STL Components
            1. Algorithms
            2. Containers
            3. Iterators
            4. Function Objects
            5. Adaptors
            6. Allocators
      2. 29. Using Iterators and Containers
          1. Understanding Iterators
            1. Creating an Iterator Class
            2. Types of Standard Iterators
            3. Exploring Iterator Operations
            4. Iterator Classes
              1. Using Input Iterators
              2. Using Output Iterators
              3. Using Forward Iterators
              4. Using Bidirectional Iterators
              5. Using Random-Access Iterators
          2. Understanding Containers
            1. Sequence Containers
              1. Vectors
              2. Deques
              3. Lists
            2. Associative Containers
              1. Sets and Multisets
              2. Maps and Multimaps
      3. 30. Using Strings
          1. Characters and Standard Strings
            1. Creating Character Strings
            2. Identifying International Concerns
              1. Single-Byte Character Sets
              2. Multibyte Character Sets
            3. Creating Other Types of Strings
            4. Defining Character Traits
          2. Investigating the basic_string Class
            1. Using String Operators
            2. String Iterators
            3. String Construction
            4. Using String Functions
              1. Assignment Methods
              2. Appending and Inserting Methods
              3. Informational Methods
              4. Standard C-Style String Conversion Methods
              5. String Comparison Methods
              6. String Find and Replace Methods
              7. Substring Manipulation Methods
      4. 31. Working with Numerics
          1. Identifying Numeric Headers
          2. Using Standard Math Functions
          3. Generating Random Numbers
          4. Working with Complex Numbers
          5. Introducing valarrays
          6. Exploring Numeric Algorithms
            1. The accumulate Algorithm
            2. The inner_product Algorithm
            3. The partial_sum Algorithm
            4. The adjacent_difference Algorithm
      5. 32. Working with Files and Streams
          1. Working with Streams
          2. Identifying Stream Types
            1. Introducing the ios_base Class
              1. Format Flags
              2. I/O State Flags
              3. Open Mode Flags
              4. Seek Direction Flags
              5. Event Flags
            2. Introducing the basic_ios Class
            3. Introducing the basic_istream Class
            4. Introducing the basic_ostream Class
            5. Introducing the basic_iostream Class
            6. Introducing the basic_fstream Class
          3. Using Stream Manipulators
            1. Creating Formatted Stream Data
          4. Using File Streams
            1. Writing and Reading Text Files
            2. Working with Multiple Files
            3. Writing and Reading a Binary File
      6. 33. Examining Algorithms
          1. Generic Algorithms
          2. Exploring the Nonmodifying Sequence Algorithms
            1. Counting Algorithms
              1. The count Algorithm
              2. The count_if Algorithm
            2. Find Algorithms
              1. The adjacent_find Algorithm
              2. The find Algorithm
              3. The find_first_of Algorithm
              4. The find_if Algorithm
            3. The for_each Algorithm
            4. Search Algorithms
              1. The find_end Algorithm
              2. The search Algorithm
              3. The search_n Algorithm
            5. Sequence Comparison Algorithms
              1. The equal Algorithm
              2. The mismatch Algorithm
          3. Exploring the Modifying Sequence Algorithms
            1. Copy Algorithms
              1. The copy Algorithm
              2. The copy_backward Algorithm
            2. Fill and Generate Algorithms
              1. The fill Algorithm
              2. The fill_n Algorithm
              3. The generate Algorithm
              4. The generate_n Algorithm
            3. Replace Algorithms
              1. The replace Algorithm
              2. The replace_copy Algorithm
              3. The replace_copy_if Algorithm
              4. The replace_if Algorithm
            4. Remove Algorithms
              1. The remove Algorithm
              2. The remove_copy Algorithm
              3. The remove_copy_if Algorithm
              4. The remove_if Algorithm
            5. Reverse and Rotate Algorithms
              1. The random_shuffle Algorithm
              2. The reverse Algorithm
              3. The reverse_copy Algorithm
              4. The rotate Algorithm
              5. The rotate_copy Algorithm
            6. Swap Algorithms
              1. The iter_swap Algorithm
              2. The swap Algorithm
              3. The swap_ranges Algorithm
            7. The transform Algorithm
            8. Unique Algorithms
              1. The unique Algorithm
              2. The unique_copy Algorithm
          4. Exploring the Sorted Sequence Algorithms
            1. Binary Search Algorithms
              1. The binary_search Algorithm
              2. The equal_range Algorithm
              3. The lower_bound Algorithm
              4. The upper_bound Algorithm
            2. Merge Algorithms
              1. The inplace_merge Algorithm
              2. The merge Algorithm
            3. Partition Algorithms
              1. The partition Algorithm
              2. The stable_partition Algorithm
            4. Set Operation Algorithms
              1. The includes Algorithm
              2. The set_difference Algorithm
              3. The set_intersection Algorithm
              4. The set_symmetric_difference Algorithm
              5. The set_union Algorithm
            5. Sorting Algorithms
              1. The nth_element Algorithm
              2. The partial_sort Algorithm
              3. The partial_sort_copy Algorithm
              4. The sort Algorithm
              5. The stable_sort Algorithm
          5. Exploring the Heap Operation Algorithms
            1. The make_heap Algorithm
            2. The pop_heap Algorithm
            3. The push_heap Algorithm
            4. The sort_heap Algorithm
          6. Exploring the Comparison Algorithms
            1. The lexicographical_compare Algorithm
            2. The max Algorithm
            3. The max_element Algorithm
            4. The min Algorithm
            5. The min_element Algorithm
          7. Exploring the Permutation Algorithms
            1. The next_permutation Algorithm
            2. The prev_permutation Algorithm
    9. Standard C++ Library Header Files
    10. Inside Front Cover
        1. A Quick Reference to Key Topics
          1. Arrays
          2. Assertions, Exceptions, and Namespaces
          3. Classes
          4. Compiling, Linking, and Debugging
          5. Data Types
          6. Expressions and Statements
          7. Functions
          8. Memory Management
          9. Object-Oriented Design and Development
          10. Pointers and References
          11. The Standard C++ Library
          12. Templates
    11. Inside Back Cover
        1. Important C++ Keywords