Essential reading for experienced developers who are determined to master the latest release of C++
Although C++ is often the language of choice from game programming to major commercial software applications, it is also one of the most difficult to master. With this no-nonsense book, you will learn to conquer the latest release of C++. The author deciphers little-known features of C++, shares detailed code examples that you can then plug into your own code, and reveals the significant changes to C++ that accompany the latest release. You'll discover how to design and build applications that solve real-world problems and then implement the solution using the full capabilities of the language.
Appeals to experienced developers who are looking for a higher level of learning
Drills down the extensive changes to the latest C++ standard, C++11, including enhancements made to run-time performance, standard library, language usability, and core language Zeroes in on explaining the more poorly understood elements of the C++ feature set and addresses common pitfalls to avoid Includes case studies that feature extensive, working code that has been tested on Windows and Linux platforms Intertwines text with useful tips, tricks, and workarounds Packed with best practices for programming, testing, and debugging applications, this book is vital for taking your C++ skills to the next level.
Author(s): Marc Gregoire; Nicholas A. Solter; Scott J. Kleper
Edition: 2
Publisher: Wrox Press
Year: 2011
Language: English
Pages: 1072
Cover
Professional C++ (Second Edition)
Copyright
Contents
Introduction
Part I: Introduction to Professional C++
Chapter 1: A Crash Course in C++
The Basics of C++
The Obligatory Hello, World
Namespaces
Variables
Operators
Types
Conditionals
Loops
Arrays
Functions
Those Are the Basics
Diving Deeper into C++
Pointers and Dynamic Memory
Strings in C++
References
Exceptions
The Many Uses of const
C++ as an Object- Oriented Language
Declaring a Class
The Standard Library
Your First Useful C++ Program
An Employee Records System
The Employee Class
The Database Class
The User Interface
Evaluating the Program
Summary
Chapter 2: Designing Professional C++ Programs
What Is Programming Design?
The Importance of Programming Design
What's Different about C++ Design?
Two Rules for C++ Design
Abstraction
Reuse
Reusing Code
A Note on Terminology
Deciding Whether or Not to Reuse Code
Strategies for Reusing Code
Bundling Third-Party Applications
Open-Source Libraries
The C++ Standard Library
Designing with Patterns and Techniques
Designing a Chess Program
Requirements
Design Steps
Summary
Chapter 3: Designing With Objects
Am I Thinking Procedurally?
The Object-Oriented Philosophy
Classes
Components
Properties
Behaviors
Bringing It All Together
Living in a World of Objects
Overobjectification
Overly General Objects
Object Relationships
The Has-A Relationship
The Is-A Relationship (Inheritance)
The Fine Line between Has-A and Is-A
The Not-A Relationship
Hierarchies
Multiple Inheritance
Mix-in Classes
Abstraction
Interface versus Implementation
Deciding on an Exposed Interface
Designing a Successful Abstraction
Summary
Chapter 4: Designing for Reuse
The Reuse Philosophy
How to Design Reusable Code
Use Abstraction
Structure Your Code for Optimal Reuse
Design Usable Interfaces
Reconciling Generality and Ease of Use
Summary
Chapter 5: Coding with Style
The Importance of Looking Good
Thinking Ahead
Elements of Good Style
Documenting Your Code
Reasons to Write Comments
Commenting Styles
Comments in This Book
Decomposition
Decomposition through Refactoring
Decomposition by Design
Decomposition in This Book
Naming
Choosing a Good Name
Naming Conventions
Using Language Features with Style
Use Constants
Use References Instead of Pointers
Use Custom Exceptions
Formatting
The Curly Brace Alignment Debate
Coming to Blows over Spaces and Parentheses
Spaces and Tabs
Stylistic Challenges
Summary
Part II: C++ Coding the Professional Way
Chapter 6: Gaining Proficiency with Classes and Objects
Introducing the Spreadsheet Example
Writing Classes
Class Definitions
Defining Methods
Using Objects
Object Life Cycles
Object Creation
Object Destruction
Assigning to Objects
Distinguishing Copying from Assignment
Summary
Chapter 7: mastering classes and Objects
Dynamic Memory Allocation in Objects
The Spreadsheet Class
Freeing Memory with Destructors
Handling Copying and Assignment
Different Kinds of Data Members
static Data Members
const Data Members
Reference Data Members
const Reference Data Members
More about Methods
static Methods
const Methods
Method Overloading
Default Parameters
Inline Methods
Nested Classes
Enumerated Types Inside Classes
Friends
Operator Overloading
Example: Implementing Addition for SpreadsheetCells
Overloading Arithmetic Operators
Overloading Comparison Operators
Building Types with Operator Overloading
Building Stable Interfaces
Using Interface and Implementation Classes
Summary
Chapter 8: Discovering Inheritance Techniques
Building Classes with Inheritance
Extending Classes
Overriding Methods
Inheritance for Reuse
The WeatherPrediction Class
Adding Functionality in a Subclass
Replacing Functionality in a Subclass
Respect Your Parents
Parent Constructors
Parent Destructors
Referring to Parent Names
Casting Up and Down
Inheritance for Polymorphism
Return of the Spreadsheet
Designing the Polymorphic Spreadsheet Cell
The Spreadsheet Cell Base Class
The Individual Subclasses
Leveraging Polymorphism
Future Considerations
Multiple Inheritance
Inheriting from Multiple Classes
Naming Collisions and Ambiguous Base Classes
Interesting and Obscure Inheritance Issues
Changing the Overridden Method's Characteristics
Inherited Constructors
Special Cases in Overriding Methods
Copy Constructors and the Equals Operator in Subclasses
The Truth about virtual
Run Time Type Facilities
Non-Public Inheritance
Virtual Base Classes
Summary
Chapter 9: Understanding C++ Quirks and Oddities
References
Reference Variables
Reference Data Members
Reference Parameters
Reference Return Values
Deciding between References and Pointers
Rvalue References
Keyword Confusion
The const Keyword
The static Keyword
Order of Initialization of Nonlocal Variables
Types and Casts
typedefs
typedefs for Function Pointers
Type Aliases
Casts
Scope Resolution
C++11
Uniform Initialization
Alternative Function Syntax
Null Pointer Constant
Angle Brackets
Initializer Lists
Explicit Conversion Operators
Attributes
User Defined Literals
Header Files
C Utilities
Variable-Length Argument Lists
Preprocessor Macros
Summary
Chapter 10: Handling Errors
Errors and Exceptions
What Are Exceptions, Anyway?
Why Exceptions in C++ Are a Good Thing
Why Exceptions in C++ Are a Bad Thing
Our Recommendation
Exception Mechanics
Throwing and Catching Exceptions
Exception Types
Throwing and Catching Multiple Exceptions
Uncaught Exceptions
Throw Lists
Exceptions and Polymorphism
The Standard Exception Hierarchy
Catching Exceptions in a Class Hierarchy
Writing Your Own Exception Classes
Nested Exceptions
Stack Unwinding and Cleanup
Use Smart Pointers
Catch, Cleanup, and Rethrow
Common Error-Handling Issues
Memory Allocation Errors
Errors in Constructors
Function-Try-Blocks for Constructors
Errors in Destructors
Putting It All Together
Summary
Chapter 11: Delving into the Standard Library
Coding Principles
Use of Templates
Use of Operator Overloading
Overview of the C++ Standard Library
Strings
I/O Streams
Localization
Smart Pointers
Exceptions
Mathematical Utilities
Time Utilities
Random Numbers
Compile-Time Rational Arithmetic
Tuples
Regular Expressions
The Standard Template Library
STL Algorithms
What's Missing from the STL
Summary
Chapter 12: Understanding Containers and Iterators
Containers Overview
Requirements on Elements
Exceptions and Error Checking
Iterators
C++11 Changes
Sequential Containers
vector
The vector Specialization
deque
list
array
forward_list
Container Adapters
queue
priority_queue
stack
Associative Containers
The pair Utility Class
map
multimap
set
multiset
Unordered Associative Containers/Hash Tables
Hash Functions
unordered_map
unordered_map Example: Phone Book
unordered_multimap
unordered_set/unordered_multiset
Other Containers
Standard C-Style Arrays
strings
Streams
bitset
Summary
Chapter 13: Mastering STL Algorithms
Overview of Algorithms
The find and find_if Algorithms
The accumulate Algorithms
C++11 Move Semantics with Algorithms
Lambda Expressions
Syntax
Capture Block
Lambda Expressions as Return Type
Lambda Expressions as Parameters
Examples
Function Objects
Arithmetic Function Objects
Comparison Function Objects
Logical Function Objects
Bitwise Function Objects
Function Object Adapters
Writing Your Own Function Objects
Algorithm Details
Utility Algorithms
Non-Modifying Algorithms
Modifying Algorithms
Sorting Algorithms
Set Algorithms
Algorithms Example: Auditing Voter Registrations
The Voter Registration Audit Problem Statement
The auditVoterRolls Function
The getDuplicates Function
Testing the auditVoterRolls Function
Summary
Chapter 14: Using Strings and Regular Expressions
Dynamic Strings
C-Style Strings
String Literals
The C++ string Class
Raw String Literals
Localization
Localizing String Literals
Wide Characters
Non-Western Character Sets
Locales and Facets
Regular Expressions
ECMAScript Syntax
The regex Library
regex_match()
regex_search()
regex_iterator
regex_token_iterator
regex_replace()
Summary
Chapter 15: Demystifying C++ I/ O
Using Streams
What Is a Stream, Anyway?
Stream Sources and Destinations
Output with Streams
Input with Streams
Input and Output with Objects
String Streams
File Streams
Jumping around with seek() and tell()
Linking Streams Together
Bidirectional I/O
Summary
Chapter 16: Additional Library Utilities
std::function
Ratios
The Chrono Library
Duration
Clock
Time Point
Random Number Generation
Random Number Engines
Random Number Engine Adapters
Predefined Engines and Engine Adapters
Generating Random Numbers
Random Number Distributions
Tuples
Summary
Chapter 17: Customizing and Extending the STL
Allocators
Iterator Adapters
Reverse Iterators
Stream Iterators
Insert Iterators
Move Iterators
Extending the STL
Why Extend the STL?
Writing an STL Algorithm
Writing an STL Container
Summary
Part III: Mastering Advanced Features of C++
Chapter 18: Overloading C++ Operators
Overview of Operator Overloading
Why Overload Operators?
Limitations to Operator Overloading
Choices in Operator Overloading
Operators You Shouldn't Overload
Summary of Overloadable Operators
Rvalue References
Overloading the Arithmetic Operators
Overloading Unary Minus and Unary Plus
Overloading Increment and Decrement
Overloading the Bitwise and Binary Logical Operators
Overloading the Insertion and Extraction Operators
Overloading the Subscripting Operator
Providing Read-Only Access with operator[]
Non-Integral Array Indices
Overloading the Function Call Operator
Overloading the Dereferencing Operators
Implementing operator*
Implementing operator->
What in the World Is operator ->* ?
Writing Conversion Operators
Ambiguity Problems with Conversion Operators
Conversions for Boolean Expressions
Overloading the Memory Allocation and Deallocation Operators
How new and delete Really Work
Overloading operator new and operator delete
Overloading operator new and operator delete with Extra Parameters
Explicitly Deleting/ Defaulting operator new and operator delete
Summary
Chapter 19: Writing Generic Code with Templates
Overview of Templates
Class Templates
Writing a Class Template
How the Compiler Processes Templates
Distributing Template Code between Files
Template Parameters
Method Templates
Template Class Specialization
Subclassing Template Classes
Inheritance versus Specialization
Template Aliases
Alternative Function Syntax
Function Templates
Function Template Specialization
Function Template Overloading
Friend Function Templates of Class Templates
Summary
Chapter 20: Advanced Templates
More about Template Parameters
More about Template Type Parameters
Introducing Template Template Parameters
More about Non-Type Template Parameters
Template Class Partial Specialization
Another Form of Partial Specialization
Emulating Function Partial Specialization with Overloading
More on Deduction
Template Recursion
An N-Dimensional Grid: First Attempt
A Real N-Dimensional Grid
Type Inference
The auto Keyword
The decltype Keyword
auto and decltype with Templates
Variadic Templates
Type-Safe Variable-Length Argument Lists
Variable Number of Mix-In Classes
Metaprogramming
Factorial at Compile Time
Loop Unrolling
Printing Tuples
Type Traits
Conclusion
Summary
Chapter 21: Effective Memory Management
Working with Dynamic Memory
How to Picture Memory
Allocation and Deallocation
Arrays
Working with Pointers
Array-Pointer Duality
Arrays Are Pointers!
Not All Pointers Are Arrays!
Low-Level Memory Operations
Pointer Arithmetic
Custom Memory Management
Garbage Collection
Object Pools
Function Pointers
Pointers to Methods and Members
Smart Pointers
The Old Deprecated auto_ptr
The New C++11 Smart Pointers
Writing Your Own Smart Pointer Class
Common Memory Pitfalls
Underallocating Strings
Memory Leaks
Double-Deleting and Invalid Pointers
Accessing Out-of-Bounds Memory
Summary
Chapter 22: Multithreaded Programming with C++
Introduction
Race Conditions and Deadlocks
Atomic Operations Library
Atomic Type Example
Atomic Operations
Threads
Thread with Function Pointer
Thread with Function Object
Thread with Lambda
Thread with Member Function
Thread Local Storage
Cancelling Threads
Retrieving Results from Threads
Copying and Rethrowing Exceptions
Mutual Exclusion
Mutex Classes
Locks
std::call_once
Mutex Usage Examples
Condition Variables
Futures
Example: Multithreaded Logger Class
Thread Pools
Threading Design and Best Practices
Summary
Part IV: C++ Software Engineering
Chapter 23: Maximizing Software Engineering Methods
The Need for Process
Software Life Cycle Models
The Stagewise Model and Waterfall Model
The Spiral Model
The Rational Unified Process
Software Engineering Methodologies
Agile
Scrum
Extreme Programming (XP)
Software Triage
Building Your Own Process and Methodology
Be Open to New Ideas
Bring New Ideas to the Table
Recognize What Works and What Doesn't Work
Don't Be a Renegade
Source Code Control
Summary
Chapter 24: Writing Efficient C++
Overview of Performance and Efficiency
Two Approaches to Efficiency
Two Kinds of Programs
Is C++ an Inefficient Language?
Language-Level Efficiency
Handle Objects Efficiently
Use Inline Methods and Functions
Design-Level Efficiency
Cache as Much as Possible
Use Object Pools
Profiling
Profiling Example with gprof
Profiling Example with Visual C++ 2010
Summary
Chapter 25: Developing Cross-Platform and Cross-Language Applications
Cross-Platform Development
Architecture Issues
Implementation Issues
Platform-Specific Features
Cross-Language Development
Mixing C and C++
Shifting Paradigms
Linking with C Code
Mixing C# with C++
Mixing Java and C++ with JNI
Mixing C++ with Perl and Shell Scripts
Mixing C++ with Assembly Code
Summary
Chapter 26: Becoming Adept at Testing
Quality Control
Whose Responsibility Is Testing?
The Life Cycle of a Bug
Bug-Tracking Tools
Unit Testing
Approaches to Unit Testing
The Unit Testing Process
Unit Testing in Action
Higher-Level Testing
Integration Tests
System Tests
Regression Tests
Tips for Successful Testing
Summary
Chapter 27: Conquering Debugging
The Fundamental Law of Debugging
Bug Taxonomies
Avoiding Bugs
Planning for Bugs
Error Logging
Debug Traces
Asserts
Static Asserts
Debugging Techniques
Reproducing Bugs
Debugging Reproducible Bugs
Debugging Nonreproducible Bugs
Debugging Memory Problems
Debugging Multithreaded Programs
Debugging Example: Article Citations
Lessons from the ArticleCitations Example
Summary
Chapter 28: Incorporating Design Techniques and Frameworks
" I Can Never Remember How to . . ."
. . . Write a Class
. . . Subclass an Existing Class
. . . Throw and Catch Exceptions
. . . Read from a File
. . . Write to a File
. . . Write a Template Class
There Must Be a Better Way
Double Dispatch
Mix-In Classes
Object-Oriented Frameworks
Working with Frameworks
The Model-View-Controller Paradigm
Summary
Chapter 29: Applying Design Patterns
The Iterator Pattern
The Singleton Pattern
Example: A Logging Mechanism
Implementation of a Singleton
Using a Singleton
Singletons and Multithreading
The Factory Pattern
Example: A Car Factory Simulation
Implementation of a Factory
Using a Factory
Other Uses of Factories
The Proxy Pattern
Example: Hiding Network Connectivity Issues
Implementation of a Proxy
Using a Proxy
The Adapter Pattern
Example: Adapting a Logger Class
Implementation of an Adapter
Using an Adapter
The Decorator Pattern
Example: Defining Styles in Web Pages
Implementation of a Decorator
Using a Decorator
The Chain of Responsibility Pattern
Example: Event Handling
Implementation of a Chain of Responsibility
Using a Chain of Responsibility
The Observer Pattern
Example: Event Handling
Implementation of an Observer
Using an Observer
Summary
Appendix A: C++ Interviews
Appendix B: Annotated Bibliography
Appendix C: Standard Library Header Files
Index