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

Book Description

Exploring C++ divides C++ up into bite-sized chunks that will help you learn the language one step at a time. Assuming no familiarity with C++, or any other C-based language, you'll be taught everything you need to know in a logical progression of small lessons that you can work through as quickly or as slowly as you need.

C++ can be a complicated language. Writing even the most straight-forward of programs requires you to understand many disparate aspects of the language and how they interact with one another. C++ doesn't lend itself to neat compartmentalization the way other languages do. Rather than baffle you with complex chapters explaining functions, classes and statements in isolation we'll focus on teaching you how to achieve results. By learning a little bit of this and a little of that you'll soon have amassed enough knowledge to be writing non-trivial programs and will have built a solid foundation of experience that puts those previously baffling concepts into context.

In this fully-revised second edition of Exploring C++, you'll learn how to use the standard library early in the book. Next, you'll learn to work with operators, objects and data-sources in increasingly realistic situations. Finally, you'll start putting the pieces together to create sophisticated programs of your own design confident that you've built a firm base of experience from which to grow.

What you'll learn

  • Learn how to use C++ from first principles in a practical hands-on way.

  • Understand how to use Custom types, virtual functions and objects to structure your code

  • Build your own function templates, namespaces and containers from the ground up.

  • Put everything together to create sophisticated programs that work with pointers, dynamic memory and overloaded functions to achieve the results you want.

  • Who this book is for

    Read this book if you want to learn C++ and have a basic understanding of how computer programs work. You don't need to know a C-based language before you start, but a basic understanding of how programs are structured is helpful.

    Table of Contents

    1. Title Page
    2. Contents at a Glance
    3. Contents
    4. About the Author
    5. About the Technical Reviewers
    6. Acknowledgments
    7. Introduction
    8. Exploration 1: Honing Your Tools
      1. Ray’s Recommendations
      2. Read the Documentation
      3. Your First Program
    9. Exploration 2: Reading C++ Code
      1. Comments
      2. Headers
      3. Main Program
      4. Variable Definitions
      5. Statements
      6. Output
    10. Exploration 3: Integer Expressions
    11. Exploration 4: Strings
    12. Exploration 5: Simple Input
    13. Exploration 6: Error Messages
      1. Misspelling
      2. Bogus Character
      3. Unknown Operator
      4. Unknown Name
      5. Symbol Errors
      6. Fun with Errors
    14. Exploration 7: For Loops
      1. Bounded Loops
      2. Your Turn
    15. Exploration 8: Formatted Output
      1. The Problem
      2. Field Width
      3. Fill Character
      4. std Prefix
      5. Alignment
      6. Exploring Formatting
      7. Alternative Syntax
      8. On Your Own
    16. Exploration 9: Arrays and Vectors
      1. Vectors for Arrays
      2. Vectors
      3. Iterators
    17. Exploration 10: Algorithms and Iterators
      1. Algorithms
      2. Member Types
      3. A Simpler Loop
      4. Using Iterators and Algorithms
    18. Exploration 11: Increment and Decrement
      1. Increment
      2. Decrement
    19. Exploration 12: Conditions and Logic
      1. I/O and bool
      2. Boolean Type
      3. Logic Operators
      4. Old-Fashioned Syntax
      5. Comparison Operators
    20. Exploration 13: Compound Statements
      1. Statements
      2. Local Definitions and Scope
      3. Definitions in for Loop Headers
    21. Exploration 14: Introduction to File I/O
      1. Reading Files
      2. Writing Files
    22. Exploration 15: The Map Data Structure
      1. Using Maps
      2. Pairs
      3. Searching in Maps
    23. Exploration 16: Type Synonyms
      1. typedef Declarations
      2. Common typedefs
      3. Type Aliases
    24. Exploration 17: Characters
      1. Character Type
      2. Character I/O
      3. Newlines and Portability
      4. Character Escapes
    25. Exploration 18: Character Categories
      1. Character Sets
      2. Character Categories
      3. Locales
    26. Exploration 19: Case-Folding
      1. Simple Cases
      2. Harder Cases
    27. Exploration 20: Writing Functions
      1. Functions
      2. Function Call
      3. Declarations and Definitions
      4. Counting Words—Again
      5. The main( ) Function
    28. Exploration 21: Function Arguments
      1. Argument Passing
      2. Pass-by-Reference
      3. const References
      4. const_iterator
      5. Multiple Output Parameters
    29. Exploration 22: Using Algorithms
      1. Transforming Data
      2. Predicates
      3. Other Algorithms
    30. Exploration 23: Unnamed Functions
      1. Lambdas
      2. Naming an Unnamed Function
      3. Capturing Local Variables
      4. const Capture
      5. Return Type
    31. Exploration 24: Overloading Function Names
      1. Overloading
    32. Exploration 25: Big and Little Numbers
      1. The Long and Short of It
      2. Integer Literals
      3. Byte-Sized Integers
      4. Type Casting
      5. Make Up Your Own Literals
      6. Integer Arithmetic
      7. Overload Resolution
    33. Exploration 26: Very Big and Very Little Numbers
      1. Floating-Point Numbers
      2. Floating-Point Literals
      3. Floating-Point Traits
      4. Floating-Point I/O
    34. Exploration 27: Documentation
      1. Doxygen
      2. Structured Comments
      3. Documentation Tags and Markdown
      4. Using Doxygen
    35. Exploration 28: Project 1: Body-Mass Index
      1. Hints
    36. Exploration 29: Custom Types
      1. Defining a New Type
      2. Member Functions
      3. Constructors
      4. Overloading Constructors
    37. Exploration 30: Overloading Operators
      1. Comparing Rational Numbers
      2. Arithmetic Operators
      3. Math Functions
    38. Exploration 31: Custom I/O Operators
      1. Input Operator
      2. Output Operator
      3. Error State
    39. Exploration 32: Assignment and Initialization
      1. Assignment Operator
      2. Constructors
      3. Putting It All Together
    40. Exploration 33: Writing Classes
      1. Anatomy of a Class
      2. Member Functions
      3. Constructor
      4. Defaulted and Deleted Constructors
    41. Exploration 34: More About Member Functions
      1. Revisiting Project 1
      2. Const Member Functions
    42. Exploration 35: Access Levels
      1. Public vs. Private
      2. class vs. struct
      3. Plain Old Data
      4. Public or Private?
    43. Exploration 36: Introduction to Object-Oriented Programming
      1. Books and Magazines
      2. Classification
      3. Inheritance
      4. Liskov’s Substitution Principle
      5. Type Polymorphism
    44. Exploration 37: Inheritance
      1. Deriving a Class
      2. Member Functions
      3. Destructors
      4. Access Level
      5. Programming Style
    45. Exploration 38: Virtual Functions
      1. Type Polymorphism
      2. Virtual Functions
      3. References and Slices
      4. Pure Virtual Functions
      5. Virtual Destructors
    46. Exploration 39: Classes and Types
      1. Classes vs. typedefs
      2. Value Types
      3. Resource Acquisition Is Initialization
    47. Exploration 40: Declarations and Definitions
      1. Declaration vs. Definition
      2. inline Functions
      3. Variable Declarations and Definitions
      4. Static Variables
      5. Static Data Members
      6. Declarators
    48. Exploration 41: Using Multiple Source Files
      1. Multiple Source Files
      2. Declarations and Definitions
      3. #include Files
      4. extern Variables
      5. inline Functions
      6. One-Definition Rule
    49. Exploration 42: Function Objects
      1. The Function Call Operator
      2. Function Objects
    50. Exploration 43: Useful Algorithms
      1. Searching
      2. Comparing
      3. Rearranging Data
      4. Copying Data
      5. Deleting Elements
      6. Iterators
    51. Exploration 44: Iterators
      1. Kinds of Iterators
      2. Working with Iterators
      3. const_iterator vs. const iterator
      4. Error Messages
      5. Specialized Iterators
    52. Exploration 45: Exceptions
      1. Introducing Exceptions
      2. Catching Exceptions
      3. Throwing Exceptions
      4. Program Stack
      5. Standard Exceptions
      6. I/O Exceptions
      7. Custom Exceptions
      8. Don’t Throw Exceptions
      9. Exceptional Advice
    53. Exploration 46: More Operators
      1. Conditional Operator
      2. Short-Circuit Operators
      3. Comma Operator
      4. Arithmetic Assignment Operators
      5. Increment and Decrement
    54. Exploration 47: Project 2: Fixed-Point Numbers
      1. value_type
      2. places
      3. places10
      4. fixed( )
      5. fixed(value_type integer, value_type fraction)
      6. fixed(double val)
      7. to_string( )
      8. round( )
      9. integer( )
      10. fraction( )
    55. Exploration 48: Function Templates
      1. Generic Functions
      2. Using Function Templates
      3. Writing Function Templates
      4. Template Parameters
      5. Template Arguments
      6. Declarations and Definitions
      7. Member Function Templates
    56. Exploration 49: Class Templates
      1. Parameterizing a Type
      2. Parameterizing the rational Class
      3. Using Class Templates
      4. Overloaded Operators
      5. Mixing Types
    57. Exploration 50: Template Specialization
      1. Instantiation and Specialization
      2. Custom Comparators
      3. Specializing Function Templates
      4. Traits
    58. Exploration 51: Partial Template Specialization
      1. Degenerate Pairs
      2. Partial Specialization
      3. Partially Specializing Function Templates
      4. Value Template Parameters
    59. Exploration 52: Names and Namespaces
      1. Namespaces
      2. Nested Namespaces
      3. Global Namespace
      4. The std Namespace
      5. Using Namespaces
      6. Unnamed Namespaces
      7. Name Lookup
    60. Exploration 53: Containers
      1. Properties of Containers
      2. Member Types
      3. What Can Go into a Container
      4. Inserting and Erasing
      5. Iterators and References
      6. Sequence Containers
      7. Associative Containers
    61. Exploration 54: Locales and Facets
      1. The Problem
      2. Locales to the Rescue
      3. Locales and I/O
      4. Facets
      5. Character Categories
      6. Collation Order
    62. Exploration 55: International Characters
      1. Why Wide?
      2. Using Wide Characters
      3. Wide Strings
      4. Wide Character I/O
      5. Multi-Byte Character Sets
      6. Unicode
      7. Universal Character Names
      8. Unicode Difficulties
    63. Exploration 56: Text I/O
      1. File Modes
      2. String Streams
      3. Text Conversion
    64. Exploration 57: Project 3: Currency Type
    65. Exploration 58: Pointers
      1. A Programming Problem
      2. The Solution
      3. Addresses vs. Pointers
      4. Dependency Graphs
    66. Exploration 59: Dynamic Memory
      1. Allocating Memory
      2. Freeing Memory
      3. Pointer to Nothing
      4. Implementing Standard Containers
      5. Adding Variables
      6. Special Member Functions
    67. Exploration 60: Moving Data with Rvalue References
      1. Temporary Objects
      2. Lvalues, Rvalues, and More
      3. Implementing Move
      4. Rvalue or Lvalue?
      5. Special Member Functions
    68. Exploration 61: Exception-Safety
      1. Memory Leaks
      2. Exceptions and Dynamic Memory
      3. Automatically Deleting Pointers
      4. Exceptions and Constructors
    69. Exploration 62: Old-Fashioned Arrays
      1. C-Style Arrays
      2. Array Limitations
      3. Dynamically Allocating an Array
      4. The array Type
      5. Multidimensional Arrays
      6. C-Style Strings
      7. Command-Line Arguments
      8. Pointer Arithmetic
    70. Exploration 63: Smart Pointers
      1. Revisiting unique_ptr
      2. Copyable Smart Pointers
      3. Smart Arrays
      4. Pimpls
      5. Iterators
    71. Exploration 64: Working with Bits
      1. Integer As a Set of Bits
      2. Bitmasks
      3. Shifting Bits
      4. Safe Shifting with Unsigned Types
      5. Overflow
      6. Introducing Bitfields
      7. Portability
      8. The bitset Class Template
    72. Exploration 65: Enumerations
      1. Scoped Enumerations
      2. Unscoped Enumerations
      3. Strings and Enumerations
      4. Revisiting Projects
    73. Exploration 66: Multiple Inheritance
      1. Multiple Base Classes
      2. Virtual Base Classes
      3. Java-Like Interfaces
      4. Interfaces vs. Templates
      5. Mix-Ins
    74. Exploration 67: Traits and Policies
      1. Case Study: Iterators
      2. Iterator Traits
      3. Type Traits
      4. Case Study: char_traits
      5. Policy-Based Programming
    75. Exploration 68: Names, Namespaces, and Templates
      1. Common Rules
      2. Name Lookup in Templates
      3. Three Kinds of Name Lookup
      4. Member Access Operators
      5. Qualified Name Lookup
      6. Unqualified Name Lookup
      7. Argument-Dependent Lookup
    76. Exploration 69: Overloaded Functions and Operators
      1. Type Conversion
      2. Review of Overloaded Functions
      3. Overload Resolution
      4. Default Arguments
    77. Exploration 70: Metaprogramming
      1. Use constexpr for Compile-Time Values
      2. Variable-Length Template Argument Lists
      3. Types as Values
      4. Conditional Types
      5. Checking Traits
      6. Substitution Failure Is Not An Error (SFINAE)
    78. Exploration 71: Project 4: Calculator
    79. Index