You are previewing C++ Primer Plus.
O'Reilly logo
C++ Primer Plus

Book Description

C++ Primer Plus, Sixth Edition

New C++11 Coverage


C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.

The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.

Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.

Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.

In C++ Primer Plus, you’ll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:

  • A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard

  • Complete, integrated discussion of both basic C language and additional C++ features

  • Clear guidance about when and why to use a feature

  • Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time

  • Hundreds of practical sample programs

  • Review questions and programming exercises at the end of each chapter to test your understanding

  • Coverage of generic C++ gives you the greatest possible flexibility

  • Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

  • Table of Contents

    1: Getting Started with C++

    2: Setting Out to C++

    3: Dealing with Data

    4: Compound Types

    5: Loops and Relational Expressions

    6: Branching Statements and Logical Operators

    7: Functions: C++’s Programming Modules

    8: Adventures in Functions

    9: Memory Models and Namespaces

    10: Objects and Classes

    11: Working with Classes

    12: Classes and Dynamic Memory Allocation

    13: Class Inheritance

    14: Reusing Code in C++

    15: Friends, Exceptions, and More

    16: The string Class and the Standard Template Library

    17: Input, Output, and Files

    18: The New C++11 Standard

    A Number Bases

    B C++ Reserved Words

    C The ASCII Character Set

    D Operator Precedence

    E Other Operators

    F The stringTemplate Class

    G The Standard Template Library Methods and Functions

    H Selected Readings and Internet Resources

    I Converting to ISO Standard C++

    J Answers to Chapter Reviews

    Table of Contents

    1. Developer’s Library
    2. Title Page
    3. Copyright Page
    4. Dedication
    5. Contents at Glance
    6. Table of Contents
    7. Acknowledgments
    8. About the Author
    9. We Want to Hear from You!
    10. Reader Services
    11. Introduction
    12. 1. Getting Started with C++
      1. The C Language
      2. C Programming Philosophy
      3. The C++ Shift: Object-Oriented Programming
      4. C++ and Generic Programming
      5. The Genesis of C++
      6. Language Growth
      7. This Book and C++ Standards
      8. Creating the Source Code File
      9. Compilation and Linking
    13. 2. Setting Out to C++
      1. Features of the main() Function
      2. C++ Comments
      3. The C++ Preprocessor and the iostream File
      4. Header Filenames
      5. Namespaces
      6. C++ Output with cout
      7. C++ Source Code Formatting
      8. Declaration Statements and Variables
      9. Assignment Statements
      10. A New Trick for cout
      11. Using cin
      12. Concatenating with cout
      13. cin and cout: A Touch of Class
      14. Using a Function That Has a Return Value
      15. Function Variations
      16. User-Defined Functions
      17. Using a User-Defined Function That Has a Return Value
      18. Placing the using Directive in Multifunction Programs
    14. 3. Dealing with Data
      1. Names for Variables
      2. Integer Types
      3. The short, int, long, and long long Integer Types
      4. Unsigned Types
      5. Choosing an Integer Type
      6. Integer Literals
      7. How C++ Decides What Type a Constant Is
      8. The char Type: Characters and Small Integers
      9. The bool Type
      10. Writing Floating-Point Numbers
      11. Floating-Point Types
      12. Floating-Point Constants
      13. Advantages and Disadvantages of Floating-Point Numbers
      14. Order of Operation: Operator Precedence and Associativity
      15. Division Diversions
      16. The Modulus Operator
      17. Type Conversions
      18. auto Declarations in C++11
    15. 4. Compound Types
      1. Program Notes
      2. Initialization Rules for Arrays
      3. C++11 Array Initialization
      4. Concatenating String Literals
      5. Using Strings in an Array
      6. Adventures in String Input
      7. Reading String Input a Line at a Time
      8. Mixing String and Numeric Input
      9. C++11 String Initialization
      10. Assignment, Concatenation, and Appending
      11. More string Class Operations
      12. More on string Class I/O
      13. Other Forms of String Literals
      14. Using a Structure in a Program
      15. C++11 Structure Initialization
      16. Can a Structure Use a string Class Member?
      17. Other Structure Properties
      18. Arrays of Structures
      19. Bit Fields in Structures
      20. Setting Enumerator Values
      21. Value Ranges for Enumerations
      22. Declaring and Initializing Pointers
      23. Pointer Danger
      24. Pointers and Numbers
      25. Allocating Memory with new
      26. Freeing Memory with delete
      27. Using new to Create Dynamic Arrays
      28. Program Notes
      29. Summarizing Pointer Points
      30. Pointers and Strings
      31. Using new to Create Dynamic Structures
      32. Automatic Storage, Static Storage, and Dynamic Storage
      33. The vector Template Class
      34. The array Template Class (C++11)
      35. Comparing Arrays, Vector Objects, and Array Objects
    16. 5. Loops and Relational Expressions
      1. Parts of a for Loop
      2. Back to the for Loop
      3. Changing the Step Size
      4. Inside Strings with the for Loop
      5. The Increment (++) and Decrement (--) Operators
      6. Side Effects and Sequence Points
      7. Prefixing Versus Postfixing
      8. The Increment/Decrement Operators and Pointers
      9. Combination Assignment Operators
      10. Compound Statements, or Blocks
      11. More Syntax Tricks—The Comma Operator
      12. Relational Expressions
      13. Assignment, Comparison, and a Mistake You’ll Probably Make
      14. Comparing C-Style Strings
      15. Comparing string Class Strings
      16. Program Notes
      17. for Versus while
      18. Just a Moment—Building a Time-Delay Loop
      19. Using Unadorned cin for Input
      20. cin.get(char) to the Rescue
      21. Which cin.get() Should You Use?
      22. The End-of-File Condition
      23. Yet Another Version of cin.get()
      24. Initializing a Two-Dimensional Array
      25. Using a Two-Dimensional Array
    17. 6. Branching Statements and Logical Operators
      1. The if else Statement
      2. Formatting if else Statements
      3. The if else if else Construction
      4. The Logical OR Operator: ||
      5. The Logical AND Operator: &&
      6. Setting Up Ranges with &&
      7. The Logical NOT Operator: !
      8. Logical Operator Facts
      9. Alternative Representations
      10. Using Enumerators as Labels
      11. switch and if else
      12. Program Notes
      13. Program Notes
      14. Text I/O and Text Files
      15. Writing to a Text File
      16. Reading from a Text File
    18. 7. Functions: C++’s Programming Modules
      1. Defining a Function
      2. Prototyping and Calling a Function
      3. Multiple Arguments
      4. Another Two-Argument Function
      5. How Pointers Enable Array-Processing Functions
      6. The Implications of Using Arrays as Arguments
      7. More Array Function Examples
      8. Functions Using Array Ranges
      9. Pointers and const
      10. Functions with C-Style String Arguments
      11. Functions That Return C-Style Strings
      12. Passing and Returning Structures
      13. Another Example of Using Functions with Structures
      14. Passing Structure Addresses
      15. Program Notes
      16. Recursion with a Single Recursive Call
      17. Recursion with Multiple Recursive Calls
      18. Function Pointer Basics
      19. A Function Pointer Example
      20. Variations on the Theme of Function Pointers
      21. Simplifying with typedef
    19. 8. Adventures in Functions
      1. Creating a Reference Variable
      2. References as Function Parameters
      3. Reference Properties and Oddities
      4. Using References with a Structure
      5. Using References with a Class Object
      6. Another Object Lesson: Objects, Inheritance, and References
      7. When to Use Reference Arguments
      8. Program Notes
      9. An Overloading Example
      10. When to Use Function Overloading
      11. Overloaded Templates
      12. Template Limitations
      13. Explicit Specializations
      14. Instantiations and Specializations
      15. Which Function Version Does the Compiler Pick?
      16. Template Function Evolution
    20. 9. Memory Models and Namespaces
      1. Scope and Linkage
      2. Automatic Storage Duration
      3. Static Duration Variables
      4. Static Duration, External Linkage
      5. Static Duration, Internal Linkage
      6. Static Storage Duration, No Linkage
      7. Specifiers and Qualifiers
      8. Functions and Linkage
      9. Language Linking
      10. Storage Schemes and Dynamic Allocation
      11. Traditional C++ Namespaces
      12. New Namespace Features
      13. A Namespace Example
      14. Namespaces and the Future
    21. 10. Objects and Classes
      1. What Is a Type?
      2. Classes in C++
      3. Implementing Class Member Functions
      4. Using Classes
      5. Changing the Implementation
      6. Reviewing Our Story to Date
      7. Declaring and Defining Constructors
      8. Using Constructors
      9. Default Constructors
      10. Destructors
      11. Improving the Stock Class
      12. Constructors and Destructors in Review
      13. Class Scope Constants
      14. Scoped Enumerations (C++11)
    22. 11. Working with Classes
      1. Adding an Addition Operator
      2. Overloading Restrictions
      3. More Overloaded Operators
      4. Creating Friends
      5. A Common Kind of Friend: Overloading the << Operator
      6. Using a State Member
      7. Overloading Arithmetic Operators for the Vector Class
      8. An Implementation Comment
      9. Taking the Vector Class on a Random Walk
      10. Program Notes
      11. Conversion Functions
      12. Conversions and Friends
    23. 12. Classes and Dynamic Memory Allocation
      1. A Review Example and Static Class Members
      2. Special Member Functions
      3. Back to Stringbad: Where the Copy Constructor Goes Wrong
      4. More Stringbad Problems: Assignment Operators
      5. The Revised Default Constructor
      6. Comparison Members
      7. Accessing Characters by Using Bracket Notation
      8. Static Class Member Functions
      9. Further Assignment Operator Overloading
      10. Don’ts and Dos
      11. Memberwise Copying for Classes with Class Members
      12. Returning a Reference to a const Object
      13. Returning a Reference to a Non-const Object
      14. Returning an Object
      15. Returning a const Object
      16. Looking Again at new and delete
      17. Pointers and Objects Summary
      18. Looking Again at Placement new
      19. Overloading the << Operator
      20. Conversion Functions
      21. Classes Whose Constructors Use new
      22. A Queue Class
      23. The Customer Class
      24. The ATM Simulation
    24. 13. Class Inheritance
      1. Deriving a Class
      2. Constructors: Access Considerations
      3. Using a Derived Class
      4. Special Relationships Between Derived and Base Classes
      5. Developing the Brass and BrassPlus Classes
      6. Pointer and Reference Type Compatibility
      7. Virtual Member Functions and Dynamic Binding
      8. Things to Know About Virtual Methods
      9. Applying the ABC Concept
      10. ABC Philosophy
      11. Case 1: Derived Class Doesn’t Use new
      12. Case 2: Derived Class Does Use new
      13. An Inheritance Example with Dynamic Memory Allocation and Friends
      14. Member Functions That the Compiler Generates for You
      15. Other Class Method Considerations
      16. Public Inheritance Considerations
      17. Class Function Summary
    25. 14. Reusing Code in C++
      1. The valarray Class: A Quick Look
      2. The Student Class Design
      3. The Student Class Example
      4. A New Version of the Student Class Example
      5. Containment or Private Inheritance?
      6. Protected Inheritance
      7. Redefining Access with using
      8. How Many Workers?
      9. Which Method?
      10. Multiple Inheritance Synopsis
      11. Defining a Class Template
      12. Using a Template Class
      13. A Closer Look at the Template Class
      14. An Array Template Example and Non-Type Arguments
      15. Template Versatility
      16. Template Specializations
      17. Member Templates
      18. Templates As Parameters
      19. Template Classes and Friends
      20. Template Aliases (C++11)
    26. 15. Friends, Exceptions, and More
      1. Friend Classes
      2. Friend Member Functions
      3. Other Friendly Relationships
      4. Shared Friends
      5. Nested Classes and Access
      6. Nesting in a Template
      7. Calling abort()
      8. Returning an Error Code
      9. The Exception Mechanism
      10. Using Objects as Exceptions
      11. Exception Specifications Meet C++11
      12. Unwinding the Stack
      13. More Exception Features
      14. The exception Class
      15. Exceptions, Classes, and Inheritance
      16. When Exceptions Go Astray
      17. Exception Cautions
      18. What Is RTTI For?
      19. How Does RTTI Work?
      20. Misusing RTTI
    27. 16. The string Class and the Standard Template Library
      1. Constructing a String
      2. The string Class Input
      3. Working with Strings
      4. What Else Does the string Class Offer?
      5. String Varieties
      6. Using Smart Pointers
      7. Smart Pointer Considerations
      8. Why unique_ptr Is Better than auto_ptr
      9. Selecting a Smart Pointer
      10. The vector Template Class
      11. Things to Do to Vectors
      12. More Things to Do to Vectors
      13. The Range-Based for Loop (C++11)
      14. Why Iterators?
      15. Kinds of Iterators
      16. Iterator Hierarchy
      17. Concepts, Refinements, and Models
      18. Kinds of Containers
      19. Associative Containers
      20. Unordered Associative Containers (C++11)
      21. Functor Concepts
      22. Predefined Functors
      23. Adaptable Functors and Function Adapters
      24. Algorithm Groups
      25. General Properties of Algorithms
      26. The STL and the string Class
      27. Functions Versus Container Methods
      28. Using the STL
      29. vector, valarray, and array
      30. The initializer_list Template (C++11)
      31. Using initializer_list
      32. Program Notes
    28. 17. Input, Output, and Files
      1. Streams and Buffers
      2. Streams, Buffers, and the iostream File
      3. Redirection
      4. The Overloaded << Operator
      5. The Other ostream Methods
      6. Flushing the Output Buffer
      7. Formatting with cout
      8. How cin >> Views Input
      9. Stream States
      10. Other istream Class Methods
      11. Other istream Methods
      12. Simple File I/O
      13. Stream Checking and is_open()
      14. Opening Multiple Files
      15. Command-Line Processing
      16. File Modes
      17. Random Access
    29. 18. Visiting with the New C++ Standard
      1. New Types
      2. Uniform Initialization
      3. Declarations
      4. nullptr
      5. Smart Pointers
      6. Exception Specification Changes
      7. Scoped Enumerations
      8. Class Changes
      9. Template and STL Changes
      10. The rvalue Reference
      11. The Need for Move Semantics
      12. A Move Example
      13. Move Constructor Observations
      14. Assignment
      15. Forcing a Move
      16. Special Member Functions
      17. Defaulted and Deleted Methods
      18. Delegating Constructors
      19. Inheriting Constructors
      20. Managing Virtual Methods: override and final
      21. The How of Function Pointers, Functors, and Lambdas
      22. The Why of Lambdas
      23. The function Wrapper and Template Inefficiencies
      24. Fixing the Problem
      25. Further Options
      26. Template and Function Parameter Packs
      27. Unpacking the Packs
      28. Using Recursion in Variadic Template Functions
      29. Concurrent Programming
      30. Library Additions
      31. Low-Level Programming
      32. Miscellaneous
      33. The Boost Project
      34. The TR1
      35. Using Boost
    30. Appendixes
      1. A. Number Bases
      2. B. C++ Reserved Words
      3. C. The ASCII Character Set
      4. D. Operator Precedence
      5. E. Other Operators
        1. The Shift Operators
        2. The Logical Bitwise Operators
        3. Alternative Representations of Bitwise Operators
        4. A Few Common Bitwise Operator Techniques
      6. F. The string Template Class
        1. Default Constructor
        2. Constructors That Use C-Style Strings
        3. Constructors That Use Part of a C-Style String
        4. Constructors That Use an Lvalue Reference
        5. Constructors That Use an Rvalue Reference (C++11)
        6. Constructor That Uses n Copies of a Character
        7. Constructor That Uses a Range
        8. Constructor That Uses an Initialization List (C++11)
        9. Memory Miscellany
        10. The find() Family
        11. The rfind() Family
        12. The find_first_of() Family
        13. The find_last_of() Family
        14. The find_first_not_of() Family
        15. The find_last_not_of() Family
        16. Methods for Appending and Adding
        17. More Assignment Methods
        18. Insertion Methods
        19. Erase Methods
        20. Replacement Methods
        21. Other Modifying Methods: copy() and swap()
      7. G. The Standard Template Library Methods and Functions
        1. New Containers
        2. Changes to C++98 Containers
        3. Nonmodifying Sequence Operations
        4. Mutating Sequence Operations
        5. Sorting and Related Operations
        6. Numeric Operations
      8. H. Selected Readings and Internet Resources
      9. I. Converting to ISO Standard C++
        1. Use const Instead of #define to Define Constants
        2. Use inline Instead of #define to Define Short Functions
      10. J. Answers to Chapter Reviews
    31. Index
    32. Footnotes