Textbook in PDF format
This book is intended for knowledgeable users of C (or any other language using a C-like grammar, like Perl or Java) who would like to know more about, or make the transition to, C++. This document is the main textbook for the author's C++ programming courses, which are yearly organized at the University of Groningen, the Netherlands. The C++ Annotations do not cover all aspects of C++, though. In particular, when equal to C's grammar, C++'s basic grammar is not covered. Any basic book on C may be consulted to refresh that part of C++'s grammar.
Overview Of The Chapters
Introduction
What's new in the C++ Annotations
C++'s history
History of the C++ Annotations
Compiling a C program using a C++ compiler
Compiling a C++ program
C++: advantages and claims
What is Object-Oriented Programming?
Differences between C and C++
The function main' End-of-line comment Strict type checking Function Overloading Default function arguments NULL-pointers vs. 0-pointers and nullptr (C++11) The
void' parameter list
The #define __cplusplus' Using standard C functions Header files for both C and C++ Defining local variables The keyword
typedef'
Functions as part of a struct
A First Impression Of C++
Extensions to C
Namespaces
The scope resolution operator ::
Using the keyword const'
cout', cin', and
cerr'
Functions as part of structs
Data hiding: public, private and class
Structs in C vs. structs in C++
More extensions to C
References
Rvalue References (C++11)
Strongly typed enumerations (C++11)
Initializer lists (C++11)
Type inference using auto' (C++11) Defining types and 'using' declarations (C++11, 4.7) Range-based for-loops (C++11) Raw String Literals (C++11) New language-defined data types The data type
bool'
The data type wchar_t' Unicode encoding (C++11) The data type
long long int' (C++11)
The data type size_t' A new syntax for casts The
static_cast'-operator
The const_cast'-operator The
reinterpret_cast'-operator
The dynamic_cast'-operator Keywords and reserved names in C++ Name Spaces Namespaces Defining namespaces Referring to entities The standard namespace Nesting namespaces and namespace aliasing The
string' Data Type
Operations on strings
A std::string reference
Initializers
Iterators
Operators
Member functions
The IO-stream Library
Special header files
The foundation: the class ios_base' Interfacing
streambuf' objects: the class ios' Condition states Formatting output and input Output Basic output: the class
ostream'
Output to files: the class ofstream' Output to memory: the class
ostringstream'
Input
Basic input: the class istream' Input from files: the class
ifstream'
Input from memory: the class istringstream' Copying streams Coupling streams Advanced topics Redirecting streams Reading AND Writing streams Classes The constructor A first application Constructors: with and without arguments Objects inside objects: composition Composition and const objects: const member initializers Composition and reference objects: reference member initializers Data member initializers (C++11, 4.7) Delegating constructors (C++11, 4.7) Uniform initialization (C++11) Defaulted and deleted class members (C++11) Const member functions and const objects Anonymous objects The keyword
inline'
Defining members inline
When to use inline functions
Local classes: classes inside functions
The keyword mutable' Header file organization Using namespaces in header files Sizeof applied to class data members (C++11) Static Data And Functions Static data Private static data Public static data Initializing static const data Generalized constant expressions (C++11) Static member functions Calling conventions Classes And Memory Allocation Operators
new' and delete' Allocating arrays Deleting arrays Enlarging arrays Managing
raw' memory
The placement new' operator The destructor Object pointers revisited The function set_new_handler The assignment operator Overloading the assignment operator The
this' pointer
Sequential assignments and this
The copy constructor: initialization vs. assignment
Revising the assignment operator
Swapping
Moving data (C++11)
The move constructor (dynamic data) (C++11)
The move constructor (composition) (C++11)
Move-assignment (C++11)
Revising the assignment operator (part II)
Moving and the destructor (C++11)
Move-only classes (C++11)
Default move constructors and assignment operators (C++11)
Moving: implications for class design (C++11)
Copy Elision and Return Value Optimization
Plain Old Data (C++11)
Conclusion
Exceptions
Exception syntax
An example using exceptions
Anachronisms: setjmp' and
longjmp'
Exceptions: the preferred alternative
Throwing exceptions
The empty throw' statement The try block Catching exceptions The default catcher Declaring exception throwers Iostreams and exceptions Standard Exceptions Exception guarantees The basic guarantee The strong guarantee The nothrow guarantee Function try blocks Exceptions in constructors and destructors More Operator Overloading Overloading
operator'
Overloading the insertion and extraction operators
Conversion operators
The keyword explicit' Explicit conversion operators (C++11) Overloading the increment and decrement operators Overloading binary operators Overloading
operator new(size_t)'
Overloading operator delete(void *)' Operators
new' and delete' Overloading
new'
Overloading delete'
new', delete' and exceptions Function Objects Constructing manipulators The case of [io]fstream::open User-defined literals (C++11, 4.7) Overloadable operators Abstract Containers Notations used in this chapter The
pair' container
Sequential Containers
The vector' container The
list' container
The queue' container The
priority_queue' container
The deque' container The
map' container
The multimap' container The
set' container
The multiset' container The
stack' container
Unordered containers (hash tables') (C++11) Regular Expressions (C++11, ?) The
complex' container
Unrestricted Unions (C++11)
Inheritance
Related types
Inheritance depth: desirable?
Access rights: public, private, protected
Public, protected and private derivation
Promoting access rights
The constructor of a derived class
Move construction (C++11)
Move assignment (C++11)
Inheriting constructors (C++11, ?)
The destructor of a derived class
Redefining member functions
i/ostream::init
Multiple inheritance
Conversions between base classes and derived classes
Conversions with object assignments
Conversions with pointer assignments
Using non-default constructors with new
Polymorphism
Virtual functions
Virtual destructors
Pure virtual functions
Implementing pure virtual functions
Explicit virtual overrides (C++11, 4.7)
Virtual functions and multiple inheritance
Ambiguity in multiple inheritance
Virtual base classes
When virtual derivation is not appropriate
Run-time type identification
The dynamic_cast operator
The typeid' operator Inheritance: when to use to achieve what? The
streambuf' class
Protected streambuf' members The class
filebuf'
A polymorphic exception class
How polymorphism is implemented
Undefined reference to vtable ...
Virtual constructors
Friends
Friend functions
Extended friend declarations (C++11, 4.7)
Classes Having Pointers To Members
Pointers to members: an example
Defining pointers to members
Using pointers to members
Pointers to static members
Pointer sizes
Nested Classes
Defining nested class members
Declaring nested classes
Accessing private members in nested classes
Nesting enumerations
Empty enumerations
Revisiting virtual constructors
The Standard Template Library
Predefined function objects
Arithmetic function objects
Relational function objects
Logical function objects
Function adaptors
Iterators
Insert iterators
Iterators for istream' objects Iterators for
ostream' objects
The class 'unique_ptr' (C++11)
Defining unique_ptr' objects (C++11) Creating a plain
unique_ptr' (C++11)
Moving another unique_ptr' (C++11) Pointing to a newly allocated object (C++11) Operators and members (C++11) Using
unique_ptr' objects for arrays (C++11)
The legacy class 'auto_ptr' (deprecated)
The class 'shared_ptr' (C++11)
Defining shared_ptr' objects (C++11) Creating a plain
shared_ptr' (C++11)
Pointing to a newly allocated object (C++11)
Operators and members (C++11)
Casting shared pointers (C++11)
Using shared_ptr' objects for arrays (C++11) Classes having pointer data members (C++11) Multi Threading (C++11) The class 'std::thread' (C++11) Synchronization (mutexes) (C++11) Event handling (condition variables) (C++11) Lambda functions (C++11) Randomization and Statistical Distributions (C++11) Random Number Generators (C++11) Statistical distributions (C++11) The STL Generic Algorithms The Generic Algorithms accumulate adjacent_difference adjacent_find binary_search copy copy_backward count count_if equal equal_range fill fill_n find find_end find_first_of find_if for_each generate generate_n includes inner_product inplace_merge iter_swap lexicographical_compare lower_bound max max_element merge min min_element mismatch next_permutation nth_element partial_sort partial_sort_copy partial_sum partition prev_permutation random_shuffle remove remove_copy remove_copy_if remove_if replace replace_copy replace_copy_if replace_if reverse reverse_copy rotate rotate_copy search search_n set_difference set_intersection set_symmetric_difference set_union sort stable_partition stable_sort swap swap_ranges transform unique unique_copy upper_bound Heap algorithms STL: More function adaptors Member function adaptors Adaptable functions Function Templates Defining function templates Considerations regarding template parameters Late-specified return type (C++11) Passing arguments by reference (reference wrappers) (C++11) Using Local and unnamed types as template arguments (C++11) Template parameter deduction Lvalue transformations Qualification transformations Transformation to a base class The template parameter deduction algorithm Template type contractions Declaring function templates Instantiation declarations Instantiating function templates Instantiations: no
code bloat'
Using explicit template types
Overloading function templates
An example using overloaded function templates
Ambiguities when overloading function templates
Declaring overloaded function templates
Specializing templates for deviating types
Avoiding too many specializations
Declaring specializations
Complications when using the insertion operator
Static assertions (C++11)
Numeric limits
Polymorphous wrappers for function objects (C++11)
Compiling template definitions and instantiations
The function selection mechanism
Determining the template type parameters
SFINAE: Substitution Failure Is Not An Error
Summary of the template declaration syntax
Class Templates
Defining class templates
Constructing the circular queue: CirQue
Non-type parameters
Member templates
CirQue's constructors and member functions
Using CirQue objects
Default class template parameters
Declaring class templates
Preventing template instantiations (C++11)
Static data members
Extended use of the keyword typename' Specializing class templates for deviating types Example of a class specialization Partial specializations Intermezzo: some simple matrix algebraic concepts The Matrix class template The MatrixRow partial specialization The MatrixColumn partial specialization The 1x1 matrix: avoid ambiguity Variadic templates (C++11) Defining and using variadic templates (C++11) Perfect forwarding (C++11) The unpack operator (C++11) Non-type variadic templates (C++11) Tuples (C++11) Computing the return type of function objects (C++11) Instantiating class templates Processing class templates and instantiations Declaring friends Non-templates used as friends in templates Templates instantiated for specific types as friends Unbound templates as friends Extended friend declarations (C++11, 4.7) Class template derivation Deriving ordinary classes from class templates Deriving class templates from class templates Deriving class templates from ordinary classes Class templates and nesting Constructing iterators Implementing a
RandomAccessIterator'
Implementing a reverse_iterator' Advanced Template Use Subtleties Returning types nested under class templates Type resolution for base class members ::template, .template and ->template Template Meta Programming Values according to templates Selecting alternatives using templates Templates: Iterations by Recursion User-defined literals (C++11, 4.7) Template template parameters Policy classes - I Policy classes - II: template template parameters Structure by Policy Template aliases (C++11, 4.7) Trait classes Distinguishing class from non-class types Available type traits (C++11) More conversions to class types Types to types An empty type Type convertibility Template TypeList processing The length of a TypeList Searching a TypeList Selecting from a TypeList Prefixing/Appending to a TypeList Erasing from a TypeList Using a TypeList The Wrap and Multi class templates The MultiBase class template Support templates Using Multi Concrete Examples Using file descriptors with
streambuf' classes
Classes for output operations
Classes for input operations
Fixed-sized field extraction from istream objects
The fork' system call A basic Fork class Parents and Children Redirection revisited The
Daemon' program
The class Pipe' The class
ParentSlurp'
Communicating with multiple children
Function objects performing bitwise operations
A text to anything converter
Adding binary operators to classes
Binary operators allowing promotions
Range-based for-loops and pointer-ranges (C++11)
Distinguishing lvalues from rvalues with operator
Implementing a reverse_iterator' Using
bisonc++' and flex' Using
flex' to create a scanner
Using bisonc++' and
flex'
Using polymorphic semantic values with Bisonc++
Using unrestricted unions as semantic values (C++11)