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

Book Description

Beginning C++ is a tutorial for beginners in C++ and discusses a subset of C++ that is suitable for beginners. The language syntax corresponds to the C++14 standard. This book is environment neutral and does not presume any specific operating system or program development system. There is no assumption of prior programming knowledge.

All language concepts that are explained in the book are illustrated with working program examples. Most chapters include exercises for you to test your knowledge. Code downloads are provided for examples from the text and solutions to the exercises and there is an additional download for a more substantial project for you to try when you have finished the book.

This book introduces the elements of the C++ standard library that provide essential support for the language syntax that is discussed. While the Standard Template Library (STL) is not discussed to a significant extent, a few elements from the STL that are important to the notion of modern C++ are introduced and applied.

Beginning C++ is based on and supersedes Ivor Hortons previous book, Beginning ANSI C++.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Introduction
  10. Chapter 1: Basic Ideas
    1. Modern C++
    2. C++ Program Concepts
      1. Comments and Whitespace
      2. Preprocessing Directives and Header Files
      3. Functions
      4. Statements
      5. Data Input and Output
      6. return Statements
      7. Namespaces
      8. Names and Keywords
    3. Classes and Objects
    4. Templates
    5. Program Files
    6. Standard Libraries
    7. Code Presentation Style
    8. Creating an Executable
    9. Representing Numbers
      1. Binary Numbers
      2. Hexadecimal Numbers
      3. Negative Binary Numbers
      4. Octal Values
      5. Big-Endian and Little-Endian Systems
      6. Floating-Point Numbers
    10. Representing Characters
      1. ASCII Codes
      2. UCS and Unicode
    11. C++ Source Characters
      1. Trigraph Sequences
      2. Escape Sequences
    12. Procedural and Object-Oriented Programming
    13. Summary
  11. Chapter 2: Introducing Fundamental Types of Data
    1. Variables, Data, and Data Types
      1. Defining Integer Variables
      2. Defining Variables with Fixed Values
    2. Integer Literals
      1. Decimal Integer Literals
    3. Calculations with Integers
      1. More on Assignment Operations
    4. The op= Assignment Operators
    5. using Declarations and Directives
    6. The sizeof Operator
    7. Incrementing and Decrementing Integers
      1. Postfix Increment and Decrement Operations
    8. Defining Floating-Point Variables
    9. Floating-Point Literals
    10. Floating-Point Calculations
      1. Mathematical Functions
    11. Formatting Stream Output
    12. Mixed Expressions and Type Conversion
    13. Explicit Type Conversion
      1. Old-Style Casts
    14. Finding the Limits
    15. Working with Character Variables
      1. Working with Unicode Characters
    16. The auto Keyword
    17. Lvalues and Rvalues
    18. Summary
  12. Chapter 3: Working with Fundamental Data Types
    1. Operator Precedence and Associativity
    2. Bitwise Operators
      1. The Bitwise Shift Operators
      2. Using the Bitwise AND
      3. Using the Bitwise OR
      4. Using the Bitwise Exclusive OR
    3. Enumerated Data Types
      1. Old-Style Enumerations
    4. Synonyms for Data Types
    5. The Lifetime of a Variable
    6. Positioning Variable Definitions
      1. Global Variables
      2. Static Variables
    7. External Variables
    8. Summary
  13. Chapter 4: Making Decisions
    1. Comparing Data Values
      1. Applying the Comparison Operators
      2. Comparing Floating Point Values
    2. The if Statement
      1. Nested if Statements
      2. Code-Neutral Character Handling
    3. The if-else Statement
      1. Nested if-else Statements
      2. Understanding Nested ifs
    4. Logical Operators
      1. Logical AND
      2. Logical OR
      3. Logical Negation
    5. The Conditional Operator
    6. The switch Statement
    7. Unconditional Branching
    8. Statement Blocks and Variable Scope
    9. Summary
  14. Chapter 5: Arrays and Loops
    1. Arrays
      1. Using an Array
    2. Understanding Loops
    3. The for Loop
    4. Avoiding Magic Numbers
    5. Defining the Array Size with the Initializer List
    6. Determining the Size of an Array
    7. Controlling a for Loop with Floating-Point Values
    8. More Complex for Loop Control Expressions
    9. The Comma Operator
    10. The Ranged-based for Loop
    11. The while Loop
    12. Allocating an Array at Runtime
    13. The do-while Loop
    14. Nested Loops
    15. Skipping Loop Iterations
    16. Breaking Out of a Loop
      1. Indefinite Loops
    17. Arrays of Characters
    18. Multidimensional Arrays
      1. Initializing Multidimensional Arrays
      2. Multidimensional Character Arrays
    19. Alternatives to Using an Array
      1. Using array<T,N> Containers
      2. Using std::vector<T> Containers
      3. The Capacity and Size of a Vector
      4. Deleting Elements from a Vector container
    20. Summary
  15. Chapter 6: Pointers and References
    1. What Is a Pointer?
    2. The Address-Of Operator
    3. The Indirection Operator
      1. Why Use Pointers?
    4. Pointers to Type char
      1. Arrays of Pointers
    5. Constant Pointers and Pointers to Constants
    6. Pointers and Arrays
      1. Pointer Arithmetic
      2. Using Pointer Notation with an Array Name
    7. Dynamic Memory Allocation
      1. The Stack and the Heap
      2. Using the new and delete Operators
      3. Dynamic Allocation of Arrays
      4. Member Selection through a Pointer
    8. Hazards of Dynamic Memory Allocation
      1. Memory Leaks
      2. Fragmentation of the Free Store
    9. Raw Pointers and Smart Pointers
      1. Using unique_ptr<T> Pointers
      2. Using shared_ptr<T> Pointers
      3. Comparing shared_ptr<T> Objects
      4. weak_ptr<T> Pointers
    10. Understanding References
      1. Defining lvalue References
      2. Using a Reference Variable in a Range-Based for Loop
      3. Defining rvalue References
    11. Summary
  16. Chapter 7: Working with Strings
    1. A Better Class of String
      1. Defining string Objects
      2. Operations with String Objects
      3. Accessing Characters in a String
      4. Accessing Substrings
      5. Comparing Strings
      6. Searching Strings
      7. Searching a String Backwards
      8. Modifying a String
    2. Strings of International Characters
      1. Strings of wchar_t Characters
    3. Objects that contain Unicode Strings
    4. Raw String Literals
    5. Summary
  17. Chapter 8: Defining Functions
    1. Segmenting Your Programs
      1. Functions in Classes
      2. Characteristics of a Function
    2. Defining Functions
      1. The Function Body
      2. Function Declarations
    3. Passing Arguments to a Function
      1. Pass-by-Value
      2. Pass-by-Reference
      3. Arguments to main()
    4. Default Argument Values
      1. Multiple Default Parameter Values
    5. Returning Values from a Function
      1. Returning a Pointer
      2. Returning a Reference
    6. Inline Functions
    7. Static Variables
    8. Function Overloading
      1. Overloading and Pointer Parameters
      2. Overloading and Reference Parameters
      3. Overloading and const Parameters
      4. Overloading and Default Argument Values
    9. A Sausage Machine for Functions
      1. Creating Instances of a Function Template
      2. Explicit Template Argument
      3. Function Template Specialization
      4. Function Templates and Overloading
      5. Function Templates with Multiple Parameters
      6. Non-Type Template Parameters
    10. Trailing Return Types
    11. Pointers to Functions
      1. Defining Pointers to Functions
    12. Recursion
      1. Applying Recursion
      2. The Quicksort Algorithm
      3. The main() Function
      4. The extract_words() Function
      5. The swap() Function
      6. The sort() function
      7. The max_word_length() Function
      8. The show_words() Function
    13. Summary
  18. Chapter 9: Lambda Expressions
    1. Introducing Lambda Expressions
    2. Defining a Lambda Expression
    3. Naming a Lambda Expression
    4. Passing a Lambda Expression to a Function
      1. Function Templates that Accept Lambda Expression Arguments
      2. A Function Parameter Type for Lambda Arguments
      3. Using the std::function Template Type
    5. The Capture Clause
      1. Capturing Specific Variables
    6. Using Lambda Expressions in a Template
    7. Recursion in Lambda Expressions
    8. Summary
  19. Chapter 10: Program Files and Preprocessing Directives
    1. Understanding Translation Units
      1. The “One Definition” Rule
      2. Program Files and Linkage
      3. Determining Linkage for a Name
      4. External Names
      5. const Variables with External Linkage
    2. Preprocessing Your Source Code
    3. Defining Preprocessing Identifiers
      1. Undefining an Identifier
    4. Including Header Files
      1. Preventing Duplication of Header File Contents
    5. Namespaces
      1. The Global Namespace
      2. Defining a Namespace
      3. Applying using Declarations
      4. Functions and Namespaces
      5. Unnamed Namespaces
      6. Namespace Aliases
      7. Nested Namespaces
    6. Logical Preprocessing Directives
      1. The Logical #if Directive
      2. Testing for Specific Identifier Values
      3. Multiple Choice Code Selection
      4. Standard Preprocessing Macros
    7. Debugging Methods
      1. Integrated Debuggers
      2. Preprocessing Directives in Debugging
      3. Using the assert( ) Macro
      4. Switching Off assert() Macros
    8. Static Assertions
    9. Summary
  20. Chapter 11: Defining Your Own Data Types
    1. Classes and Object-Oriented Programming
      1. Encapsulation
      2. Inheritance
      3. Polymorphism
    2. Terminology
    3. Defining a Class
    4. Constructors
      1. Defining Constructors Outside the Class
      2. Default Constructor Parameter Values
      3. Using a Constructor Initialization List
      4. Use of the explicit Keyword
      5. Delegating Constructors
      6. The Copy Constructor
    5. Accessing Private Class Members
    6. Friends
      1. The Friend Functions of a Class
      2. Friend Classes
    7. The this Pointer
      1. Returning this from a Function
    8. const Objects and const Member Functions
      1. Casting Away const
    9. Arrays of Class Objects
    10. The Size of a Class Object
    11. Static Members of a Class
      1. Static Data Members
      2. Accessing Static Data Members
      3. A Static Data Member of the Class Type
      4. Static Function Members
    12. Destructors
    13. Pointers and References to Class Objects
    14. Using Pointers As Class Members
      1. Defining the Package Class
      2. Defining the Truckload Class
      3. Implementing the Truckload Class
    15. Nested Classes
    16. Summary
  21. Chapter 12: Operator Overloading
    1. Implementing Operators for a Class
      1. Operator Overloading
      2. Operators That Can Be Overloaded
      3. Implementing an Overloaded Operator
      4. Global Operator Functions
      5. Implementing Full Support for an Operator
      6. Implementing All Comparison Operators in a Class
    2. Operator Function Idioms
    3. Default Class Members
      1. Defining the Destructor
      2. When to Define a Copy Constructor
      3. Implementing the Assignment Operator
      4. Implementing Move Operations
    4. Overloading the Arithmetic Operators
      1. Improving Output Operations
      2. Implementing One Operator in Terms of Another
    5. Overloading the Subscript Operator
      1. Lvalues and the Overloaded Subscript Operator
    6. Overloading Type Conversions
    7. Overloading the Increment and Decrement Operators
    8. Function Objects
    9. Summary
  22. Chapter 13: Inheritance
    1. Classes and Object-Oriented Programming
      1. Hierarchies
    2. Inheritance in Classes
      1. Inheritance vs. Aggregation
      2. Deriving Classes
    3. protected Members of a Class
    4. The Access Level of Inherited Class Members
      1. Choosing Access Specifiers in Class Hierarchies
      2. Changing the Access Specification of Inherited Members
    5. Constructor Operation in a Derived Class
      1. The Copy Constructor in a Derived Class
      2. The Default Constructor in a Derived Class
      3. Inheriting Constructors
    6. Destructors Under Inheritance
      1. The Order in Which Destructors Are Called
    7. Duplicate Data Member Names
    8. Duplicate Function Member Names
    9. Multiple Inheritance
      1. Multiple Base Classes
      2. Inherited Member Ambiguity
      3. Repeated Inheritance
      4. Virtual Base Classes
    10. Converting Between Related Class Types
    11. Summary
  23. Chapter 14: Polymorphism
    1. Understanding Polymorphism
      1. Using a Base Class Pointer
      2. Calling Inherited Functions
      3. Virtual Functions
      4. Default Argument Values in Virtual Functions
      5. Virtual Function Calls with Smart Pointers
      6. Using References to Call Virtual Functions
      7. Calling the Base Class Version of a Virtual Function
      8. Converting Between Pointers to Class Objects
      9. Dynamic Casts
      10. Converting References
      11. Determining the Polymorphic Type
    2. The Cost of Polymorphism
    3. Pure Virtual Functions
      1. Abstract Classes
      2. Indirect Abstract Base Classes
    4. Destroying Objects Through a Pointer
      1. Virtual Destructors
    5. Summary
  24. Chapter 15: Runtime Errors and Exceptions
    1. Handling Errors
    2. Understanding Exceptions
      1. Throwing an Exception
      2. The Exception Handling Process
      3. Code That Causes an Exception to Be Thrown
      4. Nested try Blocks
      5. How It Works
    3. Class Objects as Exceptions
      1. Matching a Catch Handler to an Exception
      2. How It Works
      3. Catching Derived Class Exceptions with a Base Class Handler
      4. Rethrowing Exceptions
      5. Catching All Exceptions
    4. Functions That Throw Exceptions
      1. Function try Blocks
      2. Functions That Don’t Throw Exceptions
      3. Constructor try Blocks
      4. Exceptions and Destructors
    5. Standard Library Exceptions
      1. The Exception Class Definitions
      2. Using Standard Exceptions
    6. Summary
  25. Chapter 16: Class Templates
    1. Understanding Class Templates
    2. Defining Class Templates
      1. Template Parameters
      2. A Simple Class Template
      3. Defining Function Members of a Class Template
    3. Instantiating a Class Template
    4. Static Members of a Class Template
    5. Non-Type Class Template Parameters
      1. Templates for Function Members with Non-Type Parameters
      2. Arguments for Non-Type Parameters
      3. Pointers and Arrays as Non-Type Parameters
    6. Default Values for Template Parameters
    7. Explicit Template Instantiation
    8. Special Cases
      1. Using static_assert( ) in a Class Template
      2. Defining a Class Template Specialization
      3. Partial Template Specialization
      4. Choosing between Multiple Partial Specializations
    9. Friends of Class Templates
    10. Class Templates with Nested Classes
      1. Function Templates for Stack Members
    11. Summary
  26. Chapter 17: File Input and Output
    1. Input and Output in C++
      1. Understanding Streams
      2. Advantages of Using Streams
    2. Stream Classes
      1. Standard Stream Objects
      2. Stream Insertion and Extraction Operations
      3. Stream Manipulators
    3. File Streams
      1. Writing a File in Text Mode
      2. Reading a File in Text Mode
    4. Setting the Stream Open Mode
      1. Managing the Current Stream Position
    5. Unformatted Stream Operations
      1. Unformatted Stream Input
      2. Unformatted Stream Output
    6. Errors in Stream Input/Output
      1. Input/Output Errors and Exceptions
    7. Stream Operations in Binary Mode
      1. Writing Numeric Data in Binary
    8. File Read/Write Operations
      1. Random Access to a File
    9. String Streams
    10. Objects and Streams
      1. Using the Insertion Operator with Objects
      2. Using the Extraction Operator with Objects
      3. Object I/O in Binary Mode
      4. More Complex Objects in Streams
    11. Summary
  27. Index