C++ in a Nutshell

Examples and Source Code

The following table lists all the example programs, with a brief description of each one, and an indication of which compilers can handle the program correctly. See the main examples page for the full compiler names and links to the vendors' web sites. File names of the form exCCNN.cpp are the source code for Example CC-NN, in Chapter CC. Many of the examples demonstrate errors, so the correct behavior is usually for the compiler to detect and report the error. The C++ standard does not require compilers to detect all errors, though.

Legend
OK Compiler correctly compiles the code
Err Compiler correctly detects and report an error
NoE Compiler fails to detect an error, but the standard does not require the compiler to report this particular error
FE Compiler failed to detect an error it was required to diagnose
F Compiler fails to compile correctly

C++ in a Nutshell examples (Skip to examples for Chapter 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13):

Filename Description bcc como gcc icc kylix msc mwcc
cistring.cpp
cistring.h
Case-insenstive character traits OK OK OK OK OK OK OK
netstream.cpp
netstream.h
The basic_networkbuf class template OK OK OK OK OK OK OK
program.cpp
point.cc
point.h
config.h
Declaring and defining a template OK OK OK OK OK OK OK
rational.cpp
rational.h
Rational number class template OK OK OK OK OK OK OK
slist.cpp
slist.h
Singly-linked list container F OK OK OK F OK OK
ex0201.cpp Disambiguating declarations OK OK OK OK OK OK OK
ex0202.cpp Names in inner scopes can hide names in outer scopes Err Err Err Err Err Err Err
ex0203.cpp Name lookup trumps overload resolution OK OK OK OK OK OK OK
ex0204.cpp Using the global scope operator OK OK OK OK OK OK OK
ex0205.cpp Using the scope resolution operator OK OK OK OK OK OK OK
ex0206.cpp Looking up names in a friend function Err Err Err Err Err Err Err
ex0207.cpp Declaring friend member functions F OK OK OK F OK OK
ex0208.cpp Defining members outside of a class declaration OK OK OK OK OK OK OK
ex0209.cpp Using argument-dependent name lookup OK OK OK OK OK OK OK
ex0210.cpp Overloading operators for enumerations OK OK OK OK OK OK OK
ex0211.cpp Simplifying function pointer declarations with typedef OK OK OK OK OK OK OK
ex0212.cpp Returning results in function arguments OK OK OK OK OK OK OK
ex0213.cpp Encapsulating reference traits Err Err Err Err Err Err Err
ex0214.cpp Using an empty initializer OK OK OK OK OK OK OK
ex0216.cpp Using unnamed namespaces Err Err Err Err Err Err Err
ex0217.cpp Using namespace aliases Err Err Err Err Err FE FE
ex0218.cpp Using the using declaration Err Err Err Err Err Err Err
ex0219.cpp Creating synonym declarations with using declarations Err Err Err Err Err Err Err
ex0220.cpp using declarations in classes OK OK OK OK OK OK OK
ex0221.cpp Using the using directive Err Err Err Err Err Err Err
ex0301.cpp Lvalues and rvalues OK OK OK OK OK OK OK
ex0302.cpp Demonstrating order of evaluation OK OK OK OK OK OK OK
ex0304.cpp Using dynamic_cast<> OK OK OK OK OK OK OK
ex0305.cpp Using reinterpret_cast<> OK OK OK OK OK OK OK
ex0306.cpp Using static_cast<> OK OK OK OK OK OK OK
ex0307.cpp Using typeid OK OK OK OK OK OK OK
ex0308.cpp Using the & operator OK OK OK OK OK OK OK
ex0309.cpp Using the sizeof operator OK OK OK OK OK OK OK
ex0310.cpp Comparing pointers OK OK OK OK OK OK OK
ex0401.cpp Mixing declarations and statements OK OK OK OK OK OK OK
ex0402.cpp Declarations that seem like expressions OK OK OK OK OK OK F
ex0404.cpp Switch statements OK OK OK OK OK OK OK
ex0405.cpp Controlling I/O with a while loop OK OK OK OK OK OK OK
ex0406.cpp Multiple uses of for loops OK OK OK OK OK OK OK
ex0407.cpp Using break in the find_if algorithm OK OK OK OK OK OK OK
ex0408.cpp Using continue in a loop OK OK OK OK OK OK OK
ex0409.cpp Using goto statements OK OK OK OK OK OK OK
ex0410.cpp Throwing and catching exceptions OK OK OK OK OK OK OK
ex0501.cpp Declaring functions OK OK OK OK OK OK OK
ex0502.cpp Declaring and using default arguments OK OK OK OK OK OK OK
ex0503.cpp Accumulating default arguments OK OK OK OK OK OK OK
ex0504.cpp Default arguments in member functions Err Err Err Err Err Err Err
ex0505.cpp Using qualifiers with member functions OK OK OK OK OK OK OK
ex0506.cpp Declaring exception specifications Err Err Err Err Err FE FE
ex0507.cpp Declaring and defining functions OK OK OK OK OK OK OK
ex0508.cpp Equivalent pointer types OK OK OK OK OK OK OK
ex0509.cpp Using typedef in equivalent parameter types OK OK OK OK OK OK OK
ex0512.cpp Overloading functions Err Err Err Err Err Err Err
ex0513.cpp Overloading inherited functions OK OK OK OK OK OK OK
ex0514.cpp Avoiding ambiguous base class overloads Err Err Err Err Err Err Err
ex0515.cpp Finding candidate member functions OK OK OK OK OK OK OK
ex0516.cpp Calling a class-type object as a function OK OK OK OK OK OK OK
ex0517.cpp Calling overloaded operators Err Err Err Err Err Err Err
ex0518.cpp Calling an overloaded constructor OK OK OK OK OK OK OK
ex0519.cpp Resolving assignment-like initialization OK OK OK OK OK OK OK
ex0520.cpp Initializing non-class objects by calling conversion functions OK OK OK OK OK OK OK
ex0521.cpp Binding references to conversion lvalues OK OK OK OK OK OK OK
ex0522.cpp Taking the address of an overloaded function OK OK OK OK OK OK OK
ex0523.cpp Resolving overloaded functions Err Err Err Err Err Err Err
ex0524.cpp Overloading the increment operator OK OK OK OK OK OK OK
ex0525.cpp Overloading new and delete OK OK OK OK OK OK OK
ex0601.cpp Defining classes OK OK OK OK OK OK OK
ex0602.cpp Declaring POD and non-POD types OK OK OK OK OK OK OK
ex0603.cpp Using an anonymous union OK OK OK OK OK OK OK
ex0604.cpp Using a local class OK OK OK OK OK OK OK
ex0606.cpp Using bit-fields for a control word OK OK OK OK OK OK OK
ex0608.cpp Defining member functions OK OK OK OK OK OK OK
ex0609.cpp Using a pointer-to-member OK OK OK OK OK OK OK
ex0611.cpp Using static member functions Err Err Err Err Err Err Err
ex0613.cpp Exception-safe constructor OK OK OK OK OK OK OK
ex0614.cpp Declaring a destructor OK OK OK OK OK OK OK
ex0615.cpp Implicit member functions OK OK OK OK OK OK OK
ex0616.cpp Declaring and using virtual functions OK OK OK OK F OK OK
ex0617.cpp Using covariant return types OK OK OK OK OK OK OK
ex0618.cpp Pure virtual functions OK OK OK OK OK OK OK
ex0620.cpp Using multiple inheritance Err Err Err Err Err Err Err
ex0622.cpp Constructing and destructing an object OK OK OK OK OK OK OK
ex0623.cpp Access levels and overloading Err Err Err Err Err Err Err
ex0624.cpp Adjusting accessibility with using declarations Err Err Err Err Err Err Err
ex0625.cpp Accessing a protected member Err Err Err Err Err Err Err
ex0701.cpp Declaring and using templates OK OK OK OK OK OK OK
ex0703.cpp Deducing template arguments Err Err Err Err Err Err Err
ex0704.cpp Overloading functions and function templates Err Err Err Err Err Err Err
ex0705.cpp Defining class templates OK OK OK OK OK OK OK
ex0706.cpp Declaring friend templates and friends of templates OK OK OK OK OK OK OK
ex0707.cpp Specializing a function template F OK OK OK F OK OK
ex0710.cpp Instantiating templates Err Err NoE Err Err Err NoE
ex0711.cpp Dependent names OK OK OK OK OK OK OK
ex0712.cpp Resolving dependent names OK OK OK OK OK OK OK
ex0714.cpp Computing at compile-time OK OK OK OK OK OK OK
ex0717.cpp
typelist.h
Using type lists OK OK OK OK OK OK OK
ex0802.cpp Implementing the distance function template OK OK OK OK OK OK OK
ex0804.cpp Wrapping a dynamic object OK OK OK OK OK OK OK
ex0902.cpp Reading a number from a fixed-width field OK OK OK OK OK OK OK
ex0903.cpp Copying streams one character at a time OK OK OK OK OK OK OK
ex0904.cpp Copying streams with explicit buffers OK OK OK OK OK OK OK
ex0905.cpp Copying streams via stream buffers OK OK OK OK OK OK OK
ex0909.cpp Writing a manipulator that takes an argument OK OK OK OK OK OK OK
ex0910.cpp Handling serious I/O errors OK OK OK OK OK OK OK
ex1002.cpp Removing matching items from a sequence container F OK OK OK OK OK OK
ex1003.cpp Determining whether a container contains an item OK OK F OK OK OK OK
ex1004.cpp Copying streams with streambuf iterators OK OK OK OK OK OK OK
ex1005.cpp Inserting numbers in a vector OK OK OK OK OK OK OK
ex1007.cpp Using a reverse iterator OK OK OK OK OK OK OK
ex1008.cpp Computing statistics with a functor OK OK OK OK OK OK OK
ex1009.cpp One way to implement the copy_if function OK OK OK OK OK OK OK
ex1010.cpp Finding the median of a range OK OK OK OK OK OK OK
ex1101.cpp
isinteger.h
Defining type traits with a macro OK OK OK OK OK OK OK
ex1102.cpp Testing functions OK OK OK OK OK OK OK
ex1103.cpp Expanding macros OK OK OK OK OK OK OK
ex1104.cpp Nesting conditional directives OK F OK F OK F OK
ex1301.cpp Using for_each to test whether a list is sorted OK OK OK OK OK OK OK
ex1302.cpp Using generate to fill a vector with integers OK OK OK OK OK OK OK
ex1303.cpp Using generate_n to print a series of integers OK OK OK OK OK OK OK
ex1304.cpp Generating permutations OK OK OK OK OK OK OK
ex1305.cpp Computing a logarithm to any base OK OK OK OK OK OK OK
ex1307.cpp Reading input until interrupted F OK OK OK OK OK OK
ex1308.cpp Finding the maximum value of any number of arguments OK F OK OK OK OK OK
ex1309.cpp Copying a file using ifstream and ofstream OK OK OK OK OK OK OK
ex1310.cpp Functional to round off a floating point number OK OK OK OK OK OK OK
ex1311.cpp Wrapping a member function as a function object OK OK OK OK OK OK OK
ex1312.cpp Wrapping a member function as a function object OK OK OK OK OK OK OK
ex1313.cpp Wrapping the pow function in a function object OK OK OK OK OK OK OK
ex1314.cpp Copying information associated with streams OK OK OK OK OK OK OK
ex1315.cpp Ensuring proper initialization of standard I/O streams OK OK OK OK OK OK OK
ex1318.cpp Using back_inserter to add numbers to a vector OK OK OK OK OK OK OK
ex1319.cpp A simple implementation of distance OK OK OK OK OK OK OK
ex1320.cpp Using a front_inserter to add numbers to a list OK OK OK OK OK OK OK
ex1321.cpp Copying files using streambuf iterators OK OK OK OK OK OK OK
ex1323.cpp Using infinity or maximum finite value to mean "no data." OK OK OK OK OK OK OK
ex1325.cpp Using locales for input and output OK OK OK OK OK OK OK
ex1326.cpp A simple facet for working with units of measure F OK OK OK OK OK OK
ex1327.cpp Reading and writing monetary values OK OK OK OK OK OK OK
ex1328.cpp One way to modify a key in a map OK OK OK OK OK OK OK
ex1329.cpp Using a map to count word frequencies OK OK OK OK OK OK OK
ex1330.cpp Sample allocator implementation OK OK OK OK OK OK OK
ex1332.cpp Implementing new and delete with malloc and free F OK OK OK OK OK OK
ex1334.cpp One way to modify a key in a set OK OK OK OK OK OK OK
ex1335.cpp Converting a value to a string OK OK OK OK OK OK OK
ex1336.cpp Interpreting an HTML color string OK OK OK OK OK OK OK
ex1337.cpp Classifying a string OK OK OK OK OK OK OK
ex1338.cpp Storing type information OK OK OK OK OK OK OK
ex1339.cpp Making pairs of objects OK OK OK OK OK OK OK
ex1341.cpp
valarrayutil.h
Generalized slicing of a valarray OK OK OK OK OK OK OK
ex1342.cpp
valarrayutil.h
Using gslice for multidimensional arrays OK OK OK OK OK OK OK
ex1343.cpp
valarrayutil.h
Using gslice_array OK OK OK OK OK OK OK
ex1344.cpp
valarrayutil.h
Using indirect_array: OK OK OK OK OK OK OK
ex1345.cpp
valarrayutil.h
Using mask_array: OK OK OK OK OK OK OK
ex1346.cpp A simple 2D matrix class OK OK OK OK OK OK OK
ex1347.cpp
valarrayutil.h
Slicing a valarray OK OK OK OK OK OK OK
Filename Description bcc como gcc icc kylix msc mwcc