C++ Annotations Version 4.4.2

C++ Annotations Version 4.4.2

Frank B. Brokken
(and Karel Kubat until version 4.0.0)

Computing Center, University of Groningen
Landleven 1,
P.O. Box 800,
9700 AV Groningen
The Netherlands
Published at the University of Groningen
ISBN 90 367 0470 7

1994 - 2000

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: that's available in postscript, and other formats in our ftp-site, in several files starting with cplusplus.


Chapter 1: Overview of the chapters

Chapter 2: Introduction

2.0.1: History of the C++ Annotations

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

2.2: The history of C++

2.2.1: Compiling a C program by a C++ compiler

2.2.2: Compiling a C++ program

2.2.2.1: C++ under MS-Windows

2.2.2.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: End-of-line comment

2.5.2: NULL-pointers vs. 0-pointers

2.5.3: Strict type checking

2.5.4: A new syntax for casts

2.5.5: The 'static_cast'-operator

2.5.6: The 'const_cast'-operator

2.5.7: The 'reinterpret_cast'-operator

2.5.8: The void argument list

2.5.9: The #define __cplusplus

2.5.10: The usage of standard C functions

2.5.11: Header files for both C and C++

2.5.12: The definition of local variables

2.5.13: Function Overloading

2.5.14: Default function arguments

2.5.15: The keyword typedef

2.5.16: Functions as part of a struct

Chapter 3: A first impression of C++

3.1: More extensions of 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.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: Classes

5.1: Constructors and destructors

5.1.1: The constructor

5.1.2: The destructor

5.1.3: A first application

5.1.4: Constructors with arguments

5.1.4.1: The order of construction

5.2: Const member functions and const objects

5.3: The operators new and delete

5.3.1: Allocating and deallocating arrays

5.3.2: New and delete and object pointers

5.3.3: The function set_new_handler()

5.4: The keyword inline

5.4.1: Inline functions within class declarations

5.4.2: Inline functions outside of class declarations

5.4.3: When to use inline functions

5.5: Objects in objects: composition

5.5.1: Composition and const objects: const member initializers

5.5.2: Composition and reference objects: reference memberinitializers

5.6: Friend functions and friend classes

5.7: Header file organization with classes

5.8: Nesting Classes

5.8.1: Defining nested class members

5.8.2: Declaring nested classes

5.8.3: Access to private members in nested classes

5.8.4: Nesting enumerations

Chapter 6: Classes and memory allocation

6.1: Classes with pointer data members

6.2: The assignment operator

6.2.1: Overloading the assignment operator

6.2.1.1: The function 'operator=()'

6.3: The this pointer

6.3.1: Preventing self-destruction with this

6.3.2: Associativity of operators and this

6.4: The copy constructor: Initialization vs. Assignment

6.4.1: Similarities between the copy constructor and operator=()

6.5: Conclusion

Chapter 7: More About Operator Overloading

7.1: Overloading operator[]()

7.2: Overloading operator new(size_t)

7.3: Overloading operator delete(void *)

7.4: Cin, cout, cerr and their operators

7.5: Conversion operators

7.6: The `explicit' keyword

7.7: Overloading the increment and decrement operators

7.8: Function Objects

7.8.1: Categories of Function objects

7.8.1.1: Arithmetic Function Objects

7.8.1.2: Relational Function Objects

7.8.1.3: Logical Function Objects

7.8.2: Function Adaptors

7.9: Overloadable Operators

Chapter 8: Abstract Containers

8.1: The `pair' container

8.2: Sequential Containers

8.2.1: The `vector' container

8.2.2: The `list' container

8.2.3: The `queue' container

8.2.4: The `priority_queue' container

8.2.5: The `deque' container

8.2.6: The `map' container

8.2.7: The `multimap' container

8.2.8: The `set' container

8.2.9: The `multiset' container

8.2.10: The `stack' container

8.2.11: The `hash_map' and other hashing-based containers

8.3: The `complex' container

Chapter 9: Static data and functions

9.1: Static data

9.1.1: Private static data

9.1.2: Public static data

9.2: Static member functions

Chapter 10: Classes having pointers to members

10.1: Pointers to members: an example

10.2: Initializing pointers to members

10.3: Pointers to static members

10.4: Using pointers to members for real

10.4.1: Pointers to members: an implementation

Chapter 11: The Standard Template Library, generic algorithms

11.1: Iterators

11.1.1: Insert iterators

11.1.2: istream iterators

11.1.3: ostream iterators

11.2: The 'auto_ptr' class

11.2.1: Defining auto_ptr variables

11.2.2: Pointing to a newly allocated object

11.2.3: Pointing to another auto_ptr

11.2.4: Creating an plain auto_ptr

11.2.5: The get() member function

11.2.6: The reset() member function

11.2.7: The release() member function

11.3: The Generic Algorithms

11.3.1: accumulate()

11.3.2: adjacent_difference()

11.3.3: adjacent_find()

11.3.4: binary_search()

11.3.5: copy()

11.3.6: copy_backward()

11.3.7: count()

11.3.8: count_if()

11.3.9: equal()

11.3.10: equal_range()

11.3.11: fill()

11.3.12: fill_n()

11.3.13: find()

11.3.14: find_if()

11.3.15: find_end()

11.3.16: find_first_of()

11.3.17: for_each()

11.3.18: generate()

11.3.19: generate_n()

11.3.20: includes()

11.3.21: inner_product()

11.3.22: inplace_merge()

11.3.23: iter_swap()

11.3.24: lexicographical_compare()

11.3.25: lower_bound()

11.3.26: max()

11.3.27: max_element()

11.3.28: merge()

11.3.29: min()

11.3.30: min_element()

11.3.31: mismatch()

11.3.32: next_permutation()

11.3.33: nth_element()

11.3.34: partial_sort()

11.3.35: partial_sort_copy()

11.3.36: partial_sum()

11.3.37: partition()

11.3.38: prev_permutation()

11.3.39: random_shuffle()

11.3.40: remove()

11.3.41: remove_copy()

11.3.42: remove_if()

11.3.43: remove_copy_if()

11.3.44: replace()

11.3.45: replace_copy()

11.3.46: replace_if()

11.3.47: replace_copy_if()

11.3.48: reverse()

11.3.49: reverse_copy()

11.3.50: rotate()

11.3.51: rotate_copy()

11.3.52: search()

11.3.53: search_n()

11.3.54: set_difference()

11.3.55: set_intersection()

11.3.56: set_symmetric_difference()

11.3.57: set_union()

11.3.58: sort()

11.3.59: stable_partition()

11.3.60: stable_sort()

11.3.61: swap()

11.3.62: swap_ranges()

11.3.63: transform()

11.3.64: unique()

11.3.65: unique_copy()

11.3.66: upper_bound()

11.3.67: Heap algorithms

11.3.67.1: make_heap()

11.3.67.2: pop_heap()

11.3.67.3: push_heap()

11.3.67.4: sort_heap()

11.3.67.5: A small example using the heap algorithms

Chapter 12: The IO-stream Library

12.1: Streams: insertion (<<) and extraction (>>)

12.1.1: The insertion operator <<

12.1.2: The extraction operator >>

12.2: Four standard iostreams

12.3: Files and Strings in general

12.3.1: String stream objects: a summary

12.3.2: Writing streams

12.3.3: Reading streams

12.3.4: Reading and writing streams

12.3.5: Special functions

12.3.6: Good, bad, and ...: IOStream Condition States

12.3.7: Formating

12.3.7.1: The (v)form() and (v)scan() members

12.3.7.2: Manipulators: dec, hex, oct and other manipulators

12.3.7.3: Setting the precision: the member precision()

12.3.7.4: (Un)Setting display flags: the member (un)setf()

12.3.8: Constructing manipulators

Chapter 13: Exceptions

13.1: Using exceptions: an outline

13.2: An example using exceptions

13.2.1: No exceptions: the setjmp() and longjmp() approach

13.2.2: Exceptions: the preferred alternative

13.3: Throwing exceptions

13.3.1: The empty throw statement

13.4: The try block

13.5: Catching exceptions

13.5.1: The default catcher

13.6: Declaring exception throwers

Chapter 14: More about friends

14.1: Inserting String objects into streams

14.2: An initial solution

14.3: Friend-functions

14.3.1: Preventing the friend-keyword

14.4: Friend classes

Chapter 15: Inheritance

15.1: Related types

15.2: The constructor of a derived class

15.3: The destructor of a derived class

15.4: Redefining member functions

15.5: Multiple inheritance

15.6: Conversions between base classes and derived classes

15.6.1: Conversions in object assignments

15.6.2: Conversions in pointer assignments

15.7: Storing base class pointers

Chapter 16: Polymorphism, late binding and virtual functions

16.1: Virtual functions

16.1.1: Polymorphism in program development

16.1.2: How polymorphism is implemented

16.2: Pure virtual functions

16.3: Comparing only Persons

16.4: Virtual destructors

16.5: Virtual functions in multiple inheritance

16.5.1: Ambiguity in multiple inheritance

16.5.2: Virtual base classes

16.5.3: When virtual derivation is not appropriate

16.6: Run-Time Type identification

16.6.1: The dynamic_cast operator

16.6.2: The typeid operator

Chapter 17: Templates

17.1: Template functions

17.1.1: Template function definitions

17.1.1.1: The keyword 'typename'

17.1.2: Instantiations of template functions

17.1.3: Argument deduction

17.1.3.1: Lvalue transformations

17.1.3.2: Qualification conversions

17.1.3.3: Conversion to a base class

17.1.3.4: Summary: the template argument deduction algorithm

17.1.4: Explicit arguments

17.1.4.1: Template explicit instantiation declarations

17.1.5: Template explicit specialization

17.1.6: Overloading template functions

17.1.7: Selecting an overloaded (template) function

17.1.8: Name resolution within template functions

17.2: Template classes

17.2.1: Template class definitions

17.2.2: Template class instantiations

17.2.3: Nontype parameters

17.2.4: Template class member functions

17.2.5: Template classes and friend declarations

17.2.5.1: Nontemplate friends

17.2.5.2: Bound friends

17.2.5.3: Unbound friends

17.2.6: Template classes and static data

17.2.7: Derived Template Classes

17.2.8: Nesting and template classes

17.2.9: Template members

17.2.10: Template class specializations

17.2.11: Template class partial specializations

17.2.12: Name resolution within template classes

17.3: An example: the implementation of the bvector template

17.3.1: The reverse_iter template class

17.3.2: The final implementation

Chapter 18: Concrete examples of C++

18.1: Storing objects: Storable and Storage

18.1.1: The global setup

18.1.1.1: Interface functions of the class Storage

18.1.1.2: To copy or not to copy?

18.1.1.3: Who makes the copy?

18.1.2: The class Storable

18.1.2.1: Converting an existing class to a Storable

18.1.3: The class Storage

18.2: A binary tree

18.2.1: The Node class

18.2.2: The Tree class

18.2.2.1: Constructing a tree

18.2.2.2: The `standard' functions

18.2.2.3: Adding an object to the tree

18.2.2.4: Scanning the tree

18.2.2.5: The primitive operations copy() and destroy()

18.2.3: Using Tree and Node

18.3: Classes to process program options

18.3.1: Functionality of the class Configuration

18.3.1.1: The interface of the class Configuration

18.3.1.2: An example of a program using the class Configuration

18.3.2: Implementation of the class Configuration

18.3.2.1: The constructor

18.3.2.2: loadResourceFile()

18.3.2.3: loadCommandLineOptions()

18.3.3: The class Option

18.3.3.1: The interface of the class Option

18.3.3.2: The static member nextOptionDefinition

18.3.4: Derived from Option: The class TextOption

18.3.4.1: The interface of the class TextOption

18.3.4.2: The implementation of the assign() function

18.3.5: The class Object

18.3.6: The class Hashtable

18.3.6.1: The Hashtable constructor

18.3.6.2: The function mayInsert()

18.3.6.3: The function expanded()

18.3.7: Auxiliary classes

18.3.7.1: The class Mem

18.3.7.2: The class String

18.3.7.3: The class StringTokenizer

18.3.7.4: The class Ustream

18.3.7.5: The class Util

18.4: Using Bison and Flex

18.4.1: Using Flex++ to create a scanner

18.4.1.1: The flex++ specification file

18.4.1.2: The derived class: Scanner

18.4.1.3: The main() function

18.4.1.4: Building the scanner-program

18.4.2: Using both bison++ and flex++

18.4.2.1: The bison++ specification file

18.4.2.2: The bison++ token section

18.4.2.3: The bison++ grammar rules

18.4.2.4: The flex++ specification file

18.4.2.5: The generation of the code