Part I GETTING STARTED 1
Chapter 1 A Journey of 1000 Miles 5
1.1: Problem Solving 5
1.2: The C++ Program 6
1.3: Preprocessor Directives 13
1.4: A Word About Comments 17
1.5: A First Look at Input/Output 19
Chapter 2 A Tour of C++ 23
2.1: The Built-In Array Data Type 23
2.2: Dynamic Memory Allocation and Pointers 26
2.3: An Object-Based Design 30
2.4: An Object-Oriented Design 41
2.5: A Generic Design 51
2.6: An Exception-Based Design 58
2.7: An Array by Any Other Name 62
2.8: The Standard Array Is a Vector 68
Part II THE BASIC LANGUAGE 73
Chapter 3 Data Types 75
3.1: Literal Constant 75
3.2: Variables 79
3.3: Pointer Types 88
3.4: String Types 93
3.5: const Qualifier 102
3.6: Reference Types 105
3.7: The bool Type 110
3.8: Enumeration Types 111
3.9: Array Types 114
3.10: The vector Container Type 121
3.11: complex Number Types 125
3.12: Typedef Names 126
3.13: volatile Qualifier 127
3.14: The pair Type 128
3.15: Class Types 129
Chapter 4 Expression 141
4.1: What Is an Expression? 141
4.2: Arithmetic Operators 143
4.3: Equality, Relational, and Logical Operators 146
4.4: Assignment Operators 149
4.5: Increment and Decrement Operators 154
4.6: Complex Number Operations 155
4.7: The Conditional Operator 159
4.8: The sizeof Operator 160
4.9: The new and delete Expressions 162
4.10: Comma Operator 164
4.11: The Bitwise Operators 164
4.12: bitset Operations 168
4.13: Precedence 172
4.14: Type Conversions 175
4.15: A Stack Class Example 185
Chapter 5 Statements 189
5.1: Simple and Compound Statements 189
5.2: Declaration Statement 191
5.3: The if Statement 194
5.4: The switch Statement 202
5.5: The for Loop Statement 210
5.6: The while Statement 214
5.7: The do while Statement 216
5.8: The break Statement 218
5.9: The continue Statement 220
5.10: The goto Statement 220
5.11: A Linked List Example 222
Chapter 6 Abstract Container Types 249
6.1: Our Text Query System 250
6.2: A vector or a list? 254
6.3: How a vector Grows Itself 256
6.4: Defining a Sequence Container 260
6.5: Iterators 265
6.6: Sequence Container Operations 269
6.7: Storing Lines of Text 273
6.8: Finding a Substring 276
6.9: Handling Punctuation 282
6.10: A String by Any Other Format 285
6.11: Additional String Operations 288
6.12: Building a Text Location Map 294
6.13: Building a Word Exclusion Set 305
6.14: The Complete Program 308
6.15: Multimap and Multiset 318
6.16: Stack 321
6.17: Queue and Priority Queue 323
6.18: Revisiting Our iStack Class Iterators 324
Part III PROCEDURAL BASED PROGRAMMING 329
Chapter 7 Functions 331
7.1: Overview 331
7.2: Function Prototype 334
7.3: Argument Passing 338
7.4: Returning a Value 356
7.5: Recursion 361
7.6: Inline Functions 363
7.7: Linkage Directives: extern “C” 364
7.8: main(): Handling Command Line Options 367
7.9: Pointers to Functions 368
Chapter 8 Scope and Lifetime 389
8.1: Scope 389
8.2: Global Objects and Functions 395
8.3: Local Objects 402
8.4: Dynamically Allocated Objects 405
8.5: Namespace Definitions 420
8.6: Using Namespace Members 434
Chapter 9 Overloaded Functions 443
9.1: Overloaded Function Declarations 443
9.2: The Three Steps of Overload Resolution 456
9.3: Argument Type Conversions 458
9.4: Details of Function Overload Resolution 474
Chapter 10 Function Templates 489
10.1: Function Template Definition 489
10.2: Function Template Instantiation 497
10.3: Template Argument Deduction 500
10.4: Explicit Template Arguments 505
10.5: Template Compilation Models 509
10.6: Template Explicit Specialization 514
10.7: Overloading Function Templates 520
10.8: Overload Resolution with Instantiations 522
10.9: Name Resolution in Template Definitions 531
10.10: Namespaces and Function Templates 538
10.11: Function Template Example 542
Chapter 11 Exception Handling 547
11.1: Throwing an Exception 547
11.2: The Try Block 551
11.3: Catching an Exception 555
11.4: Exception Specifications 564
11.5: Exceptions and Design Issues 568
Chapter 12 Generic Algorithms 571
12.1: Overview 571
12.2: Using the Generic Algorithms 575
12.3: Function Objects 586
12.4: Revisiting Iterators 594
12.5: The Generic Algorithms 603
12.6: When Not to Use the Generic Algorithms 606
Part IV OBJECT-BASED PROGRAMMING 611
Chapter 13 Classes 613
13.1: Class Definition 614
13.2: Class Objects 621
13.3: Class Member Functions 624
13.4: The Implicit this Pointer 636
13.5: Static Class Members 641
13.6: Pointer to Class Member 649
13.7: Union: A Space-Saving Class 658
13.8: Bit-field: A Space-Saving Member 663
13.9: Class Scope 665
13.10: Nested Classes 672
13.11: Classes as Namespace Members 683
13.12: Local Classes 687
Chapter 14 Initialization, Assignment, and Destruction 689
14.1: Class Initialization 689
14.2: The Class Constructor 691
14.3: The Class Destructor 703
14.4: Class Object Arrays and Vectors 709
14.5: The Member Initialization List 716
14.6: Memberwise Initialization 723
14.7: Memberwise Assignment 729
14.8: Efficiency Considerations 732
Chapter 15 Function And Operator Overloading 737
15.1: Operator Overloading 737
15.2: Friends 748
15.3: Operator = 751
15.4: Operator [ ] 754
15.5: Operator ( ) 755
15.6: Operator -> 756
15.7: Operators ++ and -- 759
15.8: Operators new and delete 763
15.9: User-Defined Conversions 773
15.10: Selecting a Conversion 782
15.11: Overload Resolution and Member Functions 795
15.12: Overload Resolution and Operators 801
Chapter 16 Class Templates 811
16.1: Class Template Definition 812
16.2: Class Template Instantiation 820
16.3: Member Functions of Class Templates 829
16.4: Friend Declarations in Class Templates 833
16.5: Static Data Members of Class Templates 839
16.6: Nested Types of Class Templates 841
16.7: Member Templates 844
16.8: Class Templates and Compilation Model 849
16.9: Class Template Specializations 856
16.10: Class Template Partial Specializations 860
16.11: Name Resolution in Class Templates 862
16.12: Namespaces and Class Templates 865
16.13: A Template Array Class 867
Part V OBJECT-ORIENTED PROGRAMMING 877
Chapter 17 Class Inheritance and Subtyping 879
17.1: Defining a Class Hierarchy 882
17.2: Identifying the Members of the Hierarchy 890
17.3: Base Class Member Access 900
17.4: Base and Derived Class Construction 908
17.5: Base and Derived Class Virtual Functions 919
17.6: Memberwise Initialization and Assignment 943
17.7: A UserQuery Manager Class 948
17.8: Putting It Together 958
Chapter 18 Multiple and Virtual Inheritance 965
18.1: Setting the Stage 965
18.2: Multiple Inheritance 970
18.3: Public, Private, and Protected Inheritance 977
18.4: Class Scope under Inheritance 9985
18.5: Virtual Inheritance 993
18.6: A Multiple, Virtual Inheritance Example 1005
Chapter 19 Uses of Class Inheritance in C++ 1021
19.1: Run-Time Type Identification 1021
19.2: Exceptions and Inheritance 1033
19.3: Overload Resolution and Inheritance 1051
Chapter 20 The iostream Library 1063
20.1: The Output Operator> 1095
20.6: File Input and Output 1097
20.7: Condition States 1107
20.8: String Streams 1109
20.9: Format State 1112
20.10: A Strongly Typed Library 1121
Appendix: The IOStream Library 1123
accumulate() 1125
adjacent_difference() 1126
adjacent_find() 1127
binary_search() 1128
copy() 1129
copy_backward() 1130
count() 1131
count_if() 1133
equal() 1134
equal_range() 1136
fill() 1138
fill_n() 1139
find() 1140
find_if() 1141
find_end() 1143
find_first_of() 1144
for_each() 1145
generate() 1146
generate_n() 1147
includes() 1148
inner_product() 1149
inplace_merge() 1150
iter_swap () 1152
lexicographical_compare() 1153
lower_bound() 1154
max() 1156
max_element() 1156
min() 1156
min_element() 1157
merge() 1158
mismatch() 1159
next_permutation() 1161
nth_element() 1162
partial_sort() 1163
partial_sort_copy() 1164
partial_sum() 1165
partition() 1167
prev_permutation() 1168
random_shuffle() 1169
remove() 1170
remove_copy() 1170
remove_if() 1171
remove_copy_if() 1172
replace() 1173
replace_copy() 1173
replace_if() 1174
replace_copy_if() 1174
reverse() 1175
reverse_copy() 1176
rotate() 1177
rotate_copy() 1177
search() 1178
search_n() 1180
set_difference() 1181
set_intersection() 1181
set_symmetric_difference() 1182
set_union() 1182
sort() 1184
stable_partition() 1185
stable_sort() 1186
swap() 1187
swap_range() 1188
transform() 1189
unique() 1190
unique_copy() 1191
upper_bound() 1193
Heap Algorithms 1194
make_heap() 1194
pop_heap() 1195
push_heap() 1195
sort_heap() 1195
Index 1199