Front Pages Preface What’s new in the second edition What’s in Volume 2 of this book How to get Volume 2 Prerequisites Learning C++ Goals Chapters Exercises Exercise solutions Source code Language standards Language support The book’s CD ROM CD ROMs, seminars, and consulting Errors About the cover Book design and production Acknowledgements 1: Introduction to Objects The progress of abstraction An object has an interface The hidden implementation Reusing the implementation Inheritance: reusing the interface Is-a vs. is-like-a relationships Interchangeable objects with polymorphism Creating and destroying objects Exception handling: dealing with errors Analysis and design Phase 0: Make a plan The mission statement Phase 1: What are we making? Phase 2: How will we build it? Five stages of object design Guidelines for object development Phase 3: Build the core Phase 4: Iterate the use cases Phase 5: Evolution Plans pay off Extreme programming Write tests first Pair programming Why C++ succeeds A better C You’re already on the learning curve Efficiency Systems are easier to express and understand Maximal leverage with libraries Source-code reuse with templates Error handling Programming in the large Strategies for transition Guidelines 1. Training 2. Low-risk project 3. Model from success 4. Use existing class libraries 5. Don’t rewrite existing code in C++ Management obstacles Startup costs Performance issues Common design errors Summary 2: Making & Using Objects The process of language translation Interpreters Compilers The compilation process Static type checking Tools for separate compilation Declarations vs. definitions Function declaration syntax A gotcha Function definitions Variable declaration syntax Including headers Standard C++ include format Linking Using libraries How the linker searches a library Secret additions Using plain C libraries Your first C++ program Using the iostreams class Namespaces Fundamentals of program structure "Hello, world!" Running the compiler More about iostreams Character array concatenation Reading input Calling other programs Introducing strings Reading and writing files Introducing vector Summary Exercises 3: The C in C++ Creating functions Function return values Using the C function library Creating your own libraries with the librarian Controlling execution True and false if-else while do-while for The break and continue keywords switch Using and misusing goto Recursion Introduction to operators Precedence Auto increment and decrement Introduction to data types Basic built-in types bool, true, & false Specifiers Introduction to pointers Modifying the outside object Introduction to C++ references Pointers and references as modifiers Scoping Defining variables on the fly Specifying storage allocation Global variables Local variables Register variables static extern Linkage Constants Constant values volatile Operators and their use Assignment Mathematical operators Introduction to preprocessor macros Relational operators Logical operators Bitwise operators Shift operators Unary operators The ternary operator The comma operator Common pitfalls when using operators Casting operators C++ explicit casts static_cast const_cast reinterpret_cast sizeof – an operator by itself The asm keyword Explicit operators Composite type creation Aliasing names with typedef Combining variables with struct Pointers and structs Clarifying programs with enum Type checking for enumerations Saving memory with union Arrays Pointers and arrays Exploring floating-point format Pointer arithmetic Debugging hints Debugging flags Preprocessor debugging flags Runtime debugging flags Turning variables and expressions into strings The C assert( ) macro Function addresses Defining a function pointer Complicated declarations & definitions Using a function pointer Arrays of pointers to functions Make: managing separate compilation Make activities Macros Suffix Rules Default targets Makefiles in this book An example makefile Summary Exercises 4: Data Abstraction A tiny C-like library Dynamic storage allocation Bad guesses What's wrong? The basic object What's an object? Abstract data typing Object details Header file etiquette Importance of header files The multiple-declaration problem The preprocessor directives #define, #ifdef, and #endif A standard for header files Namespaces in headers Using headers in projects Nested structures Global scope resolution Summary Exercises 5: Hiding the Implementation Setting limits C++ access control protected Friends Nested friends Is it pure? Object layout The class Modifying Stash to use access control Modifying Stack to use access control Handle classes Hiding the implementation Reducing recompilation Summary Exercises 6: Initialization & Cleanup Guaranteed initialization with the constructor Guaranteed cleanup with the destructor Elimination of the definition block for loops Storage allocation Stash with constructors and destructors Stack with constructors & destructors Aggregate initialization Default constructors Summary Exercises 7: Function Overloading & Default Arguments More name decoration Overloading on return values Type-safe linkage Overloading example unions Default arguments Placeholder arguments Choosing overloading vs. default arguments Summary Exercises 8: Constants Value substitution const in header files Safety consts Aggregates Differences with C Pointers Pointer to const const pointer Formatting Assignment and type checking Character array literals Function arguments & return values Passing by const value Returning by const value Temporaries Passing and returning addresses Standard argument passing Classes const in classes The constructor initializer list “Constructors” for built-in types Compile-time constants in classes The “enum hack” in old code const objects & member functions mutable: bitwise vs. logical const ROMability volatile Summary Exercises 9: Inline Functions Preprocessor pitfalls Macros and access Inline functions Inlines inside classes Access functions Accessors and mutators Stash & Stack with inlines Inlines & the compiler Limitations Forward references Hidden activities in constructors & destructors Reducing clutter More preprocessor features Token pasting Improved error checking Summary Exercises 10: Name Control Static elements from C static variables inside functions static class objects inside functions Static object destructors Controlling linkage Confusion Other storage class specifiers Namespaces Creating a namespace Unnamed namespaces Friends Using a namespace Scope resolution The using directive The using declaration The use of namespaces Static members in C++ Defining storage for static data members static array initialization Nested and local classes static member functions Static initialization dependency What to do Technique one Technique two Alternate linkage specifications Summary Exercises 11: References & the Copy-Constructor Pointers in C++ References in C++ References in functions const references Pointer references Argument-passing guidelines The copy-constructor Passing & returning by value Passing & returning large objects Function-call stack frame Re-entrancy Bitcopy versus initialization Copy-construction Temporary objects Default copy-constructor Alternatives to copy-construction Preventing pass-by-value Functions that modify outside objects Pointers to members Functions An example Summary Exercises 12: Operator Overloading Warning & reassurance Syntax Overloadable operators Unary operators Increment & decrement Binary operators Arguments & return values Return by value as const The return optimization Unusual operators Operator comma Operator-> A nested iterator Operator->* Operators you can’t overload Non-member operators Basic guidelines Overloading assignment Behavior of operator= Pointers in classes Reference Counting Automatic operator= creation Automatic type conversion Constructor conversion Preventing constructor conversion Operator conversion Reflexivity Type conversion example Pitfalls in automatic type conversion Hidden activities Summary Exercises 13: Dynamic Object Creation Object creation C’s approach to the heap operator new operator delete A simple example Memory manager overhead Early examples redesigned delete void* is probably a bug Cleanup responsibility with pointers Stash for pointers A test new & delete for arrays Making a pointer more like an array Running out of storage Overloading new & delete Overloading global new & delete Overloading new & delete for a class Overloading new & delete for arrays Constructor calls placement new & delete Summary Exercises 14: Inheritance & Composition Composition syntax Inheritance syntax The constructor initializer list Member object initialization Built-in types in the initializer list Combining composition & inheritance Automatic destructor calls Order of constructor & destructor calls Name hiding Functions that don’t automatically inherit Inheritance and static member functions Choosing composition vs. inheritance Subtyping private inheritance Publicizing privately inherited members protected protected inheritance Operator overloading & inheritance Multiple inheritance Incremental development Upcasting Why “upcasting?” Upcasting and the copy-constructor Composition vs. inheritance (revisited) Pointer & reference upcasting A crisis Summary Exercises 15: Polymorphism & Virtual Functions Evolution of C++ programmers Upcasting The problem Function call binding virtual functions Extensibility How C++ implements late binding Storing type information Picturing virtual functions Under the hood Installing the vpointer Objects are different Why virtual functions? Abstract base classes and pure virtual functions Pure virtual definitions Inheritance and the VTABLE Object slicing Overloading & overriding Variant return type virtual functions & constructors Order of constructor calls Behavior of virtual functions inside constructors Destructors and virtual destructors Pure virtual destructors Virtuals in destructors Creating an object-based hierarchy Operator overloading Downcasting Summary Exercises 16: Introduction to Templates Containers The need for containers Overview of templates The template solution Template syntax Non-inline function definitions Header files IntStack as a template Constants in templates Stack and Stash as templates Templatized pointer Stash Turning ownership on and off Holding objects by value Introducing iterators Stack with iterators PStash with iterators Why iterators? Function templates Summary Exercises A: Coding Style B: Programming Guidelines C: Recommended Reading C General C++ My own list of books Depth & dark corners Analysis & design Index