C++ Crash Course: A Fast-Paced Introduction 1593278888, 9781593278885

A fast-paced, thorough introduction to modern C++ written for experienced programmers. After reading C++ Crash Course, y

748 146 146MB

English Pages 733 [794] Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

C++ Crash Course: A Fast-Paced Introduction
 1593278888, 9781593278885

Table of contents :
Brief Contents
Contents in Detail
Foreword
Acknowledgments
Introduction
About This Book
Who Should Read This Book?
What’s in This Book?
Part I: The C++ Core Language
Part II: C++ Libraries and Frameworks
An Overture to C Programmers
Upgrading to Super C
Function Overloading
References
auto Initialization
Namespaces and Implicit typedef of struct, union, and enum
Intermingling C and C++ Object Files
C++ Themes
Expressing Ideas Concisely and Reusing Code
The C++ Standard Library
Lambdas
Generic Programming with Templates
Class Invariants and Resource Management
Move Semantics
Relax and Enjoy Your Shoes
Part I: The C++ Core Language
Chapter 1: Up and Running
The Structure of a Basic C++ Program
Creating Your First C++ Source File
Main: A C++ Program’s Starting Point
Libraries: Pulling in External Code
The Compiler Tool Chain
Setting Up Your Development Environment
Windows 10 and Later: Visual Studio
macOS: Xcode
Linux and GCC
Text Editors
Bootstrapping C++
The C++ Type System
Declaring Variables
Initializing a Variable’s State
Conditional Statements
Functions
printf Format Specifiers
Revisiting step_function
Comments
Debugging
Visual Studio
Xcode
GCC and Clang Debugging with GDB and LLDB
Summary
Chapter 2: Types
Fundamental Types
Integer Types
Floating-Point Types
Character Types
Boolean Types
The std::byte Type
The size_t Type
void
Arrays
Array Initialization
Accessing Array Elements
A Nickel Tour of for Loops
C-Style Strings
User-Defined Types
Enumeration Types
Plain-Old-Data Classes
Unions
Fully Featured C++ Classes
Methods
Access Controls
Constructors
Initialization
The Destructor
Summary
Chapter 3: Reference Types
Pointers
Addressing Variables
Dereferencing Pointers
The Member-of-Pointer Operator
Pointers and Arrays
Pointers Are Dangerous
void Pointers and std::byte Pointers
nullptr and Boolean Expressions
References
Usage of Pointers and References
Forward-Linked Lists: The Canonical Pointer-Based Data Structure
Employing References
this Pointers
const Correctness
const Member Variables
Member Initializer Lists
auto Type Deduction
Initialization with auto
auto and Reference Types
auto and Code Refactorings
Summary
Chapter 4: The Object Life Cycle
An Object’s Storage Duration
Allocation, Deallocation, and Lifetime
Memory Management
Automatic Storage Duration
Static Storage Duration
Thread-Local Storage Duration
Dynamic Storage Duration
Tracing the Object Life Cycle
Exceptions
The throw Keyword
Using try-catch Blocks
stdlib Exception Classes
Handling Exceptions
User-Defined Exceptions
The noexcept Keyword
Call Stacks and Exceptions
A SimpleString Class
Appending and Printing
Using SimpleString
Composing a SimpleString
Call Stack Unwinding
Exceptions and Performance
Alternatives to Exceptions
Copy Semantics
Copy Constructors
Copy Assignment
Default Copy
Copy Guidelines
Move Semantics
Copying Can Be Wasteful
Value Categories
lvalue and rvalue References
The std::move Function
Move Construction
Move Assignment
The Final Product
Compiler-Generated Methods
Summary
Chapter 5: Runtime Polymorphism
Polymorphism
A Motivating Example
Adding New Loggers
Interfaces
Object Composition and Implementation Inheritance
Defining Interfaces
Base Class Inheritance
Member Inheritance
virtual Methods
Pure-Virtual Classes and Virtual Destructors
Implementing Interfaces
Using Interfaces
Updating the Bank Logger
Constructor Injection
Property Injection
Choosing Constructor or Property Injection
Summary
Chapter 6: Compile-Time Polymorphism
Templates
Declaring Templates
Template Class Definitions
Template Function Definitions
Instantiating Templates
Named Conversion Functions
const_cast
static_cast
reinterpret_cast
narrow_cast
mean: A Template Function Example
Genericizing mean
Template Type Deduction
SimpleUniquePointer: A Template Class Example
Type Checking in Templates
Concepts
Defining a Concept
Type Traits
Requirements
Building Concepts from Requires Expressions
Using Concepts
Ad Hoc Requires Expressions
static_assert: The Preconcepts Stopgap
Non-Type Template Parameters
Variadic Templates
Advanced Template Topics
Template Specialization
Name Binding
Type Function
Template Metaprogramming
Template Source Code Organization
Polymorphism at Runtime vs. Compile Time
Summary
Chapter 7: Expressions
Operators
Logical Operators
Arithmetic Operators
Assignment Operators
Increment and Decrement Operators
Comparison Operators
Member Access Operators
Ternary Conditional Operator
The Comma Operator
Operator Overloading
Overloading Operator new
Operator Precedence and Associativity
Evaluation Order
User-Defined Literals
Type Conversions
Implicit Type Conversions
Explicit Type Conversion
C-Style Casts
User-Defined Type Conversions
Constant Expressions
A Colorful Example
The Case for constexpr
Volatile Expressions
Summary
Chapter 8: Statements
Expression Statements
Compound Statements
Declaration Statements
Functions
Namespaces
Type Aliasing
Structured Bindings
Attributes
Selection Statements
if Statements
switch Statements
Iteration Statements
while Loops
do-while Loops
for Loops
Ranged-Based for Loops
Jump Statements
break Statements
continue Statements
goto Statements
Summary
Chapter 9: Functions
Function Declarations
Prefix Modifiers
Suffix Modifiers
auto Return Types
auto and Function Templates
Overload Resolution
Variadic Functions
Variadic Templates
Programming with Parameter Packs
Revisiting the sum Function
Fold Expressions
Function Pointers
Declaring a Function Pointer
Type Aliases and Function Pointers
The Function-Call Operator
A Counting Example
Lambda Expressions
Usage
Lambda Parameters and Bodies
Default Arguments
Generic Lambdas
Lambda Return Types
Lambda Captures
constexpr Lambda Expressions
std::function
Declaring a Function
An Extended Example
The main Function and the Command Line
The Three main Overloads
Exploring Program Parameters
A More Involved Example
Exit Status
Summary
Part II: C++ Libraries and Frameworks
Chapter 10: Testing
Unit Tests
Integration Tests
Acceptance Tests
Performance Tests
An Extended Example: Taking a Brake
Implementing AutoBrake
Test-Driven Development
Adding a Service-Bus Interface
Unit-Testing and Mocking Frameworks
The Catch Unit-Testing Framework
Google Test
Boost Test
Summary: Testing Frameworks
Mocking Frameworks
Google Mock
HippoMocks
A Note on Other Mocking Options: FakeIt and Trompeloeil
Summary
Chapter 11: Smart Pointers
Smart Pointers
Smart Pointer Ownership
Scoped Pointers
Constructing
Bring in the Oath Breakers
Implicit bool Conversion Based on Ownership
RAII Wrapper
Pointer Semantics
Comparison with nullptr
Swapping
Resetting and Replacing a scoped_ptr
Non-transferability
boost::scoped_array
A Partial List of Supported Operations
Unique Pointers
Constructing
Supported Operations
Transferable, Exclusive Ownership
Unique Arrays
Deleters
Custom Deleters and System Programming
A Partial List of Supported Operations
Shared Pointers
Constructing
Specifying an Allocator
Supported Operations
Transferable, Non-Exclusive Ownership
Shared Arrays
Deleters
A Partial List of Supported Operations
Weak Pointers
Constructing
Obtaining Temporary Ownership
Advanced Patterns
Supported Operations
Intrusive Pointers
Summary of Smart Pointer Options
Allocators
Summary
Chapter 12: Utilities
Data Structures
tribool
optional
pair
tuple
any
variant
Date and Time
Boost DateTime
Chrono
Numerics
Numeric Functions
Complex Numbers
Mathematical Constants
Random Numbers
Numeric Limits
Boost Numeric Conversion
Compile-Time Rational Arithmetic
Summary
Chapter 13: Containers
Sequence Containers
Arrays
Vectors
Niche Sequential Containers
Associative Containers
Sets
Unordered Sets
Maps
Niche Associative Containers
Graphs and Property Trees
The Boost Graph Library
Boost Property Trees
Initializer Lists
Summary
Chapter 14: Iterators
Iterator Categories
Output Iterators
Input Iterators
Forward Iterators
Bidirectional Iterators
Random-Access Iterators
Contiguous Iterators
Mutable Iterators
Auxiliary Iterator Functions
std::advance
std::next and std::prev
std::distance
std::iter_swap
Additional Iterator Adapters
Move Iterator Adapters
Reverse Iterator Adapters
Summary
Chapter 15: Strings
std::string
Constructing
String Storage and Small String Optimizations
Element and Iterator Access
String Comparisons
Manipulating Elements
Search
Numeric Conversions
String View
Constructing
Supported string_view Operations
Ownership, Usage, and Efficiency
Regular Expressions
Patterns
basic_regex
Algorithms
Boost String Algorithms
Boost Range
Predicates
Classifiers
Finders
Modifying Algorithms
Splitting and Joining
Searching
Boost Tokenizer
Localizations
Summary
Chapter 16: Streams
Streams
Stream Classes
Stream State
Buffering and Flushing
Manipulators
User-Defined Types
String Streams
File Streams
Stream Buffers
Random Access
Summary
Chapter 17: Filesystems
Filesystem Concepts
std::filesystem::path
Constructing Paths
Decomposing Paths
Modifying Paths
Summary of Filesystem Path Methods
Files and Directories
Error Handling
Path-Composing Functions
Inspecting File Types
Inspecting Files and Directories
Manipulating Files and Directories
Directory Iterators
Constructing
Directory Entries
Recursive Directory Iteration
fstream Interoperation
Summary
Chapter 18: Algorithms
Algorithmic Complexity
Execution Policies
Non-Modifying Sequence Operations
all_of
any_of
none_of
for_each
for_each_n
find, find_if, and find_if_not
find_end
find_first
adjacent_find
count
mismatch
equal
is_permutation
search
search_n
Mutating Sequence Operations
copy
copy_n
copy_backward
move
move_backward
swap_ranges
transform
replace
fill
generate
remove
unique
reverse
sample
shuffle
Sorting and Related Operations
sort
stable_sort
partial_sort
is_sorted
nth_element
Binary Search
lower_bound
upper_bound
equal_range
binary_search
Partitioning Algorithms
is_partitioned
partition
partition_copy
stable_partition
Merging Algorithms
merge
Extreme-Value Algorithms
min and max
min_element and max_element
clamp
Numeric Operations
Useful Operators
iota
accumulate
reduce
inner_product
adjacent_difference
partial_sum
Other Algorithms
Boost Algorithms
Chapter 19: Concurrency and Parallelism
Concurrent Programming
Asynchronous Tasks
Sharing and Coordinating
Low-Level Concurrency Facilities
Parallel Algorithms
An Example: Parallel sort
Parallel Algorithms Are Not Magic
Summary
Chapter 20: Network Programming with Boost Asio
The Boost Asio Programming Model
Network Programming with Asio
The Internet Protocol Suite
Hostname Resolution
Connecting
Buffers
Reading and Writing Data with Buffers
The Hypertext Transfer Protocol (HTTP)
Implementing a Simple Boost Asio HTTP Client
Asynchronous Reading and Writing
Serving
Multithreading Boost Asio
Summary
Chapter 21: Writing Applications
Program Support
Handling Program Termination and Cleanup
Communicating with the Environment
Managing Operating System Signals
Boost ProgramOptions
The Options Description
Parsing Options
Storing and Accessing Options
Putting It All Together
Special Topics in Compilation
Revisiting the Preprocessor
Compiler Optimization
Linking with C
Summary
Index
Blank Page
Blank Page

Polecaj historie