C++ Annotations Version 5.2.0a

C++ Annotations Version 5.2.0a

Frank B. Brokken

Computing Center, University of Groningen
Nettelbosje 1,
P.O. Box 11044,
9700 CA Groningen
The Netherlands
Published at the University of Groningen
ISBN 90 367 0470 7

1994 - 2002

This document is intended for knowledgeable users of C who would like to make the transition to C++. It is a guide for Frank's C++ programming courses, which are given yearly at the University of Groningen. As such, this document is not a complete C++ handbook. Rather, it serves as an addition to other documentation sources. If you want a hard-copy version of the C++ annotations: hard copies are available in postscript, pdf and other formats in
ftp://ftp.rug.nl/contrib/frank/documents/cplusplus.annotations,
in files having names starting with cplusplus.



Table of Contents

Chapter 1: Overview of the chapters

Chapter 2: Introduction

2.1: What's new in the C++ Annotations

2.2: The history of C++

2.2.1: History of the C++ Annotations
2.2.2: Compiling a C program by a C++ compiler
2.2.3: Compiling a C++ program

2.2.3.1: C++ under MS-Windows
2.2.3.2: Compiling a C++ source text

2.3: Advantages and pretensions of C++

2.4: What is Object-Oriented Programming?

2.5: Differences between C and C++

2.5.1: Namespaces
2.5.2: End-of-line comment
2.5.3: NULL-pointers vs. 0-pointers
2.5.4: Strict type checking
2.5.5: A new syntax for casts

2.5.5.1: The `static_cast'-operator
2.5.5.2: The `const_cast'-operator
2.5.5.3: The `reinterpret_cast'-operator
2.5.5.4: The `dynamic_cast'-operator

2.5.6: The `void' parameter list
2.5.7: The `#define __cplusplus'
2.5.8: The usage of standard C functions
2.5.9: Header files for both C and C++
2.5.10: The definition of local variables
2.5.11: Function Overloading
2.5.12: Default function arguments
2.5.13: The keyword `typedef'
2.5.14: Functions as part of a struct

Chapter 3: A first impression of C++

3.1: More extensions to C in C++

3.1.1: The scope resolution operator ::
3.1.2: `cout', `cin' and `cerr'
3.1.3: The keyword `const'
3.1.4: References

3.2: Functions as part of structs

3.3: Several new data types

3.3.1: The `bool' data type
3.3.2: The `wchar_t' data type

3.4: Data hiding: public, private and class

3.5: Structs in C vs. structs in C++

3.6: Namespaces

3.6.1: Defining namespaces

3.6.1.1: Declaring entities in namespaces
3.6.1.2: A closed namespace

3.6.2: Referring to entities

3.6.2.1: The `using' directive
3.6.2.2: `Koenig lookup'

3.6.3: The standard namespace
3.6.4: Nesting namespaces and namespace aliasing

3.6.4.1: Defining entities outside of their namespaces

Chapter 4: The `string' data type

4.1: Operations on strings

4.2: Overview of operations on strings

4.2.1: The string initializers
4.2.2: The string iterators
4.2.3: The string operators
4.2.4: The string member functions

Chapter 5: The IO-stream Library

5.1: Iostream header files

5.2: The foundation: the class `ios_base'

5.3: Interfacing `streambuf' objects: the class `ios'

5.3.1: Condition states
5.3.2: Formatting output and input

5.3.2.1: Formatting flags
5.3.2.2: Format modifying member functions

5.4: Output

5.4.1: Basic output: the class `ostream'

5.4.1.1: Writing to `ostream' objects
5.4.1.2: `ostream' positioning
5.4.1.3: `ostream' flushing

5.4.2: Output to files: the class `ofstream'

5.4.2.1: Modes for opening stream objects

5.4.3: Output to memory: the class `ostringstream'

5.5: Input

5.5.1: Basic input: the class `istream'

5.5.1.1: Reading from `istream' objects
5.5.1.2: `istream' positioning

5.5.2: Input from streams: the class `ifstream'
5.5.3: Input from memory: the class `istringstream'

5.6: Manipulators

5.7: The `streambuf' class

5.7.1: Protected `streambuf' members
5.7.2: The class `filebuf'

5.8: Advanced topics

5.8.1: Copying streams
5.8.2: Coupling streams
5.8.3: Redirection using streams
5.8.4: Reading AND Writing to a stream

Chapter 6: Classes

6.1: The constructor

6.1.1: A first application
6.1.2: Constructors: with and without arguments

6.1.2.1: The order of construction

6.2: Const member functions and const objects

6.3: The keyword `inline'

6.3.1: Inline functions within class declarations
6.3.2: Inline functions outside of class declarations
6.3.3: When to use inline functions

6.4: Objects in objects: composition

6.4.1: Composition and const objects: const member initializers
6.4.2: Composition and reference objects: reference member initializers

6.5: Header file organization with classes

6.5.1: Using namespaces in header files

6.6: The keyword `mutable'

Chapter 7: Classes and memory allocation

7.1: The operators `new' and `delete'

7.1.1: Allocating arrays
7.1.2: Deleting arrays
7.1.3: Enlarging arrays

7.2: The destructor

7.2.1: New and delete and object pointers
7.2.2: The function set_new_handler()

7.3: The assignment operator

7.3.1: Overloading the assignment operator

7.3.1.1: The function 'operator=()'

7.4: The this pointer

7.4.1: Preventing self-destruction with this
7.4.2: Associativity of operators and this

7.5: The copy constructor: Initialization vs. Assignment

7.5.1: Similarities between the copy constructor and operator=()
7.5.2: Preventing the use of certain member functions

7.6: Conclusion

Chapter 8: Exceptions

8.1: Using exceptions: syntax elements

8.2: An example using exceptions

8.2.1: No exceptions: `setjmp()' and `longjmp()'
8.2.2: Exceptions: the preferred alternative

8.3: Throwing exceptions

8.3.1: The empty `throw' statement

8.4: The try block

8.5: Catching exceptions

8.5.1: The default catcher

8.6: Declaring exception throwers

8.7: Iostreams and exceptions

Chapter 9: More Operator Overloading

9.1: Overloading `operator[]()'

9.2: overloading the insertion and extraction operators

9.3: Conversion operators

9.4: The `explicit' keyword

9.5: Overloading increment and decrement

9.6: Overloading `operator new(size_t)'

9.7: Overloading `operator delete(void *)'

9.8: Operators `new[]' and `delete[]'

9.9: Function Objects

9.9.1: Constructing manipulators

9.9.1.1: Manipulators requiring arguments

9.10: Overloadable Operators

Chapter 10: Static data and functions

10.1: Static data

10.1.1: Private static data
10.1.2: Public static data

10.2: Static member functions

Chapter 11: Friends

11.1: Friend-functions

11.2: Inline friends

Chapter 12: Abstract Containers

12.1: The `pair' container

12.2: Sequential Containers

12.2.1: The `vector' container
12.2.2: The `list' container
12.2.3: The `queue' container
12.2.4: The `priority_queue' container
12.2.5: The `deque' container
12.2.6: The `map' container
12.2.7: The `multimap' container
12.2.8: The `set' container
12.2.9: The `multiset' container
12.2.10: The `stack' container
12.2.11: The `hash_map' and other hashing-based containers

12.3: The `complex' container

Chapter 13: Inheritance

13.1: Related types

13.2: The constructor of a derived class

13.3: The destructor of a derived class

13.4: Redefining member functions

13.5: Multiple inheritance

13.6: Conversions between base classes and derived classes

13.6.1: Conversions in object assignments
13.6.2: Conversions in pointer assignments

Chapter 14: Polymorphism

14.1: Virtual functions

14.2: Virtual destructors

14.3: Pure virtual functions

14.4: Virtual functions in multiple inheritance

14.4.1: Ambiguity in multiple inheritance
14.4.2: Virtual base classes
14.4.3: When virtual derivation is not appropriate

14.5: Run-Time Type identification

14.5.1: The dynamic_cast operator
14.5.2: The typeid operator

14.6: Deriving classes from `streambuf'

14.7: A polymorphic exception class

14.8: How polymorphism is implemented

Chapter 15: Classes having pointers to members

15.1: Pointers to members: an example

15.2: Defining pointers to members

15.3: Using pointers to members

15.4: Pointers to static members

Chapter 16: Nested Classes

16.1: Defining nested class members

16.2: Declaring nested classes

16.3: Accessing private members in nested classes

16.4: Nesting enumerations

16.4.1: Empty enumerations

Chapter 17: The Standard Template Library, generic algorithms

17.1: Predefined function objects

17.1.1: Arithmetic Function Objects
17.1.2: Relational Function Objects
17.1.3: Logical Function Objects
17.1.4: Function Adaptors

17.2: Iterators

17.2.1: Insert iterators
17.2.2: istream iterators

17.2.2.1: istreambuf iterators

17.2.3: ostream iterators

17.2.3.1: ostreambuf iterators

17.3: The 'auto_ptr' class

17.3.1: Defining auto_ptr variables
17.3.2: Pointing to a newly allocated object
17.3.3: Pointing to another auto_ptr
17.3.4: Creating a plain auto_ptr
17.3.5: Auto_ptr: operators and members

17.4: The Generic Algorithms

17.4.1: accumulate()
17.4.2: adjacent_difference()
17.4.3: adjacent_find()
17.4.4: binary_search()
17.4.5: copy()
17.4.6: copy_backward()
17.4.7: count()
17.4.8: count_if()
17.4.9: equal()
17.4.10: equal_range()
17.4.11: fill()
17.4.12: fill_n()
17.4.13: find()
17.4.14: find_if()
17.4.15: find_end()
17.4.16: find_first_of()
17.4.17: for_each()
17.4.18: generate()
17.4.19: generate_n()
17.4.20: includes()
17.4.21: inner_product()
17.4.22: inplace_merge()
17.4.23: iter_swap()
17.4.24: lexicographical_compare()
17.4.25: lower_bound()
17.4.26: max()
17.4.27: max_element()
17.4.28: merge()
17.4.29: min()
17.4.30: min_element()
17.4.31: mismatch()
17.4.32: next_permutation()
17.4.33: nth_element()
17.4.34: partial_sort()
17.4.35: partial_sort_copy()
17.4.36: partial_sum()
17.4.37: partition()
17.4.38: prev_permutation()
17.4.39: random_shuffle()
17.4.40: remove()
17.4.41: remove_copy()
17.4.42: remove_if()
17.4.43: remove_copy_if()
17.4.44: replace()
17.4.45: replace_copy()
17.4.46: replace_if()
17.4.47: replace_copy_if()
17.4.48: reverse()
17.4.49: reverse_copy()
17.4.50: rotate()
17.4.51: rotate_copy()
17.4.52: search()
17.4.53: search_n()
17.4.54: set_difference()
17.4.55: set_intersection()
17.4.56: set_symmetric_difference()
17.4.57: set_union()
17.4.58: sort()
17.4.59: stable_partition()
17.4.60: stable_sort()
17.4.61: swap()
17.4.62: swap_ranges()
17.4.63: transform()
17.4.64: unique()
17.4.65: unique_copy()
17.4.66: upper_bound()
17.4.67: Heap algorithms

17.4.67.1: make_heap()
17.4.67.2: pop_heap()
17.4.67.3: push_heap()
17.4.67.4: sort_heap()
17.4.67.5: An example using the heap algorithms

Chapter 18: Templates

18.1: Template functions

18.1.1: Template function definitions
18.1.2: Instantiations of template functions

18.1.2.1: Declaring template functions

18.1.3: Argument deduction

18.1.3.1: Lvalue transformations
18.1.3.2: Qualification conversions
18.1.3.3: Conversion to a base class
18.1.3.4: Summary: the template argument deduction algorithm

18.1.4: Explicit arguments

18.1.4.1: Template explicit instantiation declarations

18.1.5: Template explicit specialization
18.1.6: Overloading template functions
18.1.7: Selecting an overloaded (template) function
18.1.8: Name resolution within template functions

18.2: Template classes

18.2.1: Template class definitions
18.2.2: Template class instantiations
18.2.3: Non-type parameters
18.2.4: Template class member functions
18.2.5: Template classes and friend declarations

18.2.5.1: Non-template friends
18.2.5.2: Bound friends
18.2.5.3: Unbound friends

18.2.6: Template classes and static data
18.2.7: Derived Template Classes
18.2.8: Nesting and template classes
18.2.9: Member templates
18.2.10: Template class specializations
18.2.11: Template class partial specializations
18.2.12: Name resolution within template classes

18.3: Constructing iterators

Chapter 19: Concrete examples of C++

19.1: `streambuf' classes using file descriptors

19.1.1: A class for output operations
19.1.2: Classes for input operations

19.1.2.1: Using a one-character buffer
19.1.2.2: Using an n-character buffer
19.1.2.3: Seeking positions in `streambuf' objects
19.1.2.4: Multiple `unget()' calls in `streambuf' objects

19.2: Using form() with ostream objects

19.3: Redirection revisited

19.4: The fork() system call

19.4.1: The `Daemon' program
19.4.2: The `Pipe' class
19.4.3: The `ParentSlurp' class
19.4.4: Communicating with multiple children

19.4.4.1: The `Select' class
19.4.4.2: The `Child' class
19.4.4.3: The `Monitor' class

19.5: Function objects performing bitwise operations

19.6: Implementing a reverse_iterator

19.7: Using Bison and Flex

19.7.1: Using Flex++ to create a scanner

19.7.1.1: The flex++ specification file
19.7.1.2: The derived class: Scanner
19.7.1.3: The main() function
19.7.1.4: Building the scanner-program

19.7.2: Using both bison++ and flex++

19.7.2.1: The bison++ specification file
19.7.2.2: The bison++ token section
19.7.2.3: The bison++ grammar rules
19.7.2.4: The flex++ specification file
19.7.2.5: The generation of the code