Starting Out with C++ From Control Structures Through Objects....3
Starting Out with C++ From Control Structures Through Objects....4
Contents at a Glance....9
Contents....12
Preface....32
Changes in the Ninth Edition....33
Organization of the Text....36
Brief Overview of Each Chapter....39
Features of the Text....63
Supplements....64
Which Gaddis C++ book is right for you?....68
About the Author....82
Chapter 1 Introduction to Computers and Programming....84
Topics....84
1.1 Why Program?....85
1.2 Computer Systems: Hardware and Software....89
Hardware....90
The CPU....92
Main Memory....96
Secondary Storage....98
Input Devices....99
Output Devices....99
Software....100
System Software....101
Application Software....102
Checkpoint....103
1.3 Programs and Programming Languages....104
What Is a Program?....105
Programming Languages....110
Source Code, Object Code, and Executable Code....113
Checkpoint....118
1.4 What Is a Program Made of?....119
Language Elements....120
Key Words (Reserved Words)....123
Programmer-Defined Identifiers....124
Operators....125
Punctuation....125
Lines and Statements....126
Variables....127
Variable Definitions....129
1.5 Input, Processing, and Output....131
Checkpoint....133
1.6 The Programming Process....134
Designing and Creating a Program....135
What Is Software Engineering?....143
1.7 Procedural and Object-Oriented Programming....145
Checkpoint....148
Review Questions and Exercises....149
Short Answer....149
Fill-in-the-Blank....150
Algorithm Workbench....152
Predict the Result....155
Find the Error....157
Chapter 2 Introduction to C++....158
Topics....159
2.1 The Parts of a C++ Program....160
Checkpoint....169
2.2 The cout Object....170
2.3 The #include Directive....185
Checkpoint....188
2.4 Variables, Literals, and Assignment Statements....190
Sometimes a Number Isn’t a Number....196
Literals....197
Checkpoint....200
2.5 Identifiers....202
Legal Identifiers....205
2.6 Integer Data Types....206
Integer and Long Integer Literals....216
If You Plan to Continue in Computer Science: Hexadecimal and Octal Literals....219
Checkpoint....221
2.7 The char Data Type....222
The Difference between String Literals and Character Literals....228
2.8 The C++ string Class....234
Using the string Class....235
Checkpoint....239
2.9 Floating-Point Data Types....241
Floating-Point Literals....244
Assigning Floating-Point Values to Integer Variables....249
2.10 The bool Data Type....251
2.11 Determining the Size of a Data Type....254
Checkpoint....257
2.12 More about Variable Assignments and Initialization....258
Declaring Variables with the auto Key Word....262
Alternative Forms of Variable Initialization....264
2.13 Scope....266
2.14 Arithmetic Operators....268
Integer Division....277
Checkpoint....296
2.15 Comments....298
Single-Line Comments....300
Multi-Line Comments....302
2.16 Named Constants....305
Checkpoint....310
2.17 Programming Style....311
Review Questions and Exercises....316
Short Answer....317
Multiple Choice....322
True or False....325
Algorithm Workbench....326
Find the Error....327
Programming Challenges....328
Chapter 3 Expressions and Interactivity....335
Topics....335
3.1 The cin Object....336
Entering Multiple Values....344
Checkpoint....352
3.2 Mathematical Expressions....355
Operator Precedence....361
Associativity....363
Grouping with Parentheses....364
Converting Algebraic Expressions to Programming Statements....365
No Exponents Please!....367
Checkpoint....378
3.3 When You Mix Apples and Oranges: Type Conversion....381
Integer Division....385
3.4 Overflow and Underflow....386
3.5 Type Casting....393
Checkpoint....401
3.6 Multiple Assignment and Combined Assignment....404
Combined Assignment Operators....405
Checkpoint....412
3.7 Formatting Output....414
The setprecision Manipulator....426
The fixed Manipulator....434
The showpoint Manipulator....440
The left and right Manipulators....442
Checkpoint....444
3.8 Working with Characters and string Objects....447
Inputting a Character....452
Using cin.get....455
Mixing cin >> and cin.get....459
Using cin.ignore....463
string Member Functions and Operators....467
3.9 More Mathematical Library Functions....469
Random Numbers....475
Checkpoint....486
3.10 Focus on Debugging: Hand Tracing a Program....487
3.11 Focus on Problem Solving: A Case Study....492
Variables....492
Program Design....493
Calculations....496
The Program....497
Review Questions and Exercises....503
Short Answer....504
Fill-in-the-Blank....508
Algorithm Workbench....509
Find the Errors....510
Predict the Output....515
Programming Challenges....519
Chapter 4 Making Decisions....532
Topics....533
4.1 Relational Operators....534
The Value of a Relationship....537
What Is Truth?....541
Checkpoint....545
4.2 The if Statement....547
Be Careful with Semicolons....556
Programming Style and the if Statement....559
Comparing Floating-Point Numbers....561
And Now Back to Truth....564
Don’t Confuse == with =....566
Checkpoint....570
4.3 Expanding the if Statement....571
Don’t Forget the Braces!....577
Checkpoint....581
4.4 The if/else Statement....582
Checkpoint....592
4.5 Nested if Statements....593
Programming Style and Nested Decision Structures....605
Testing a Series of Conditions....608
Checkpoint....615
4.6 The if/else if Statement....616
Using the Trailing else to Catch Errors....624
The if/else if Statement Compared to a Nested Decision Structure....628
Checkpoint....629
4.7 Flags....631
Integer Flags....633
4.8 Logical Operators....634
The && Operator....635
The || Operator....643
The ! Operator....650
Precedence and Associativity of Logical Operators....654
4.9 Checking Numeric Ranges with Logical Operators....656
Checkpoint....659
4.10 Menus....660
4.11 Focus on Software Engineering: Validating User Input....668
4.12 Comparing Characters and Strings....674
Comparing Characters....675
Comparing string Objects....680
Checkpoint....686
4.13 The Conditional Operator....687
Using the Value of a Conditional Expression....690
Checkpoint....697
4.14 The switch Statement....699
Using switch in Menu Systems....719
Checkpoint....725
4.15 More about Blocks and Variable Scope....729
Variables with the Same Name....738
Review Questions and Exercises....743
Short Answer....743
Fill-in-the-Blank....744
Algorithm Workbench....746
True or False....749
Find the Errors....751
Programming Challenges....757
Chapter 5 Loops and Files....774
Topics....775
5.1 The Increment and Decrement Operators....776
The Difference between Postfix and Prefix Modes....783
Using ++ and −− in Mathematical Expressions....790
Using ++ and −− in Relational Expressions....792
Checkpoint....793
5.2 Introduction to Loops: The while Loop....794
The while Loop....796
The while Loop Is a Pretest Loop....803
Infinite Loops....804
Don’t Forget the Braces with a Block of Statements....806
Programming Style and the while Loop....808
5.3 Using the while Loop for Input Validation....815
Checkpoint....822
5.4 Counters....823
5.5 The do-while Loop....828
Using do-while with Menus....835
Checkpoint....841
5.6 The for Loop....842
Using the for Loop instead of while or do-while....851
The for Loop Is a Pretest Loop....853
Avoid Modifying the Counter Variable in the Body of the for Loop....854
Other Forms of the Update Expression....855
Defining a Variable in the for Loop’s Initialization Expression....856
Creating a User-Controlled for Loop....858
Using Multiple Statements in the Initialization and Update Expressions....863
Omitting the for Loop’s Expressions....866
Checkpoint....874
5.7 Keeping a Running Total....875
5.8 Sentinels....883
Checkpoint....888
5.9 Focus on Software Engineering: Deciding Which Loop to Use....889
5.10 Nested Loops....891
5.11 Using Files for Data Storage....899
Types of Files....903
File Access Methods....904
Filenames and File Stream Objects....905
Setting Up a Program for File Input/Output....907
Creating a File Object and Opening a File....909
Closing a File....912
Writing Data to a File....913
Reading Data from a File....927
The Read Position....931
Reading Numeric Data from a Text File....934
Using Loops to Process Files....938
Detecting the End of the File....942
Testing for File Open Errors....948
Letting the User Specify a Filename....953
Using the c_str Member Function in Older Versions of C++....957
Checkpoint....958
5.12 Optional Topics: Breaking and Continuing a Loop....959
Using break in a Nested Loop....965
The continue Statement....967
Review Questions and Exercises....973
Short Answer....974
Fill-in-the-Blank....976
Algorithm Workbench....978
True or False....981
Find the Errors....983
Programming Challenges....989
Chapter 6 Functions....1003
Topics....1004
6.1 Focus on Software Engineering: Modular Programming....1005
6.2 Defining and Calling Functions....1009
void Functions....1011
Calling a Function....1012
Checkpoint....1031
6.3 Function Prototypes....1035
6.4 Sending Data into a Function....1042
6.5 Passing Data by Value....1060
6.6 Focus on Software Engineering: Using Functions in a Menu-Driven Program....1066
Checkpoint....1074
6.7 The return Statement....1078
6.8 Returning a Value from a Function....1083
Defining a Value-Returning Function....1086
Calling a Value-Returning Function....1089
6.9 Returning a Boolean Value....1108
Checkpoint....1114
6.10 Local and Global Variables....1115
Local Variables....1116
Local Variable Lifetime....1120
Initializing Local Variables with Parameter Values....1121
Global Variables....1123
Global Constants....1131
Local and Global Variables with the Same Name....1138
6.11 Static Local Variables....1142
Checkpoint....1154
6.12 Default Arguments....1156
6.13 Using Reference Variables as Parameters....1167
Checkpoint....1180
6.14 Overloading Functions....1184
6.15 The exit() Function....1198
Checkpoint....1204
6.16 Stubs and Drivers....1206
Review Questions and Exercises....1214
Short Answer....1214
Fill-in-the-Blank....1215
Algorithm Workbench....1217
True or False....1219
Find the Errors....1221
Programming Challenges....1223
Group Project....1239
Chapter 7 Arrays and Vectors....1242
Topics....1242
7.1 Arrays Hold Multiple Values....1243
Memory Requirements of Arrays....1246
7.2 Accessing Array Elements....1247
Inputting and Outputting Array Contents....1252
Array Initialization....1264
Partial Array Initialization....1274
Implicit Array Sizing....1277
Reading Data from a File into an Array....1278
Writing the Contents of an Array to a File....1283
7.3 No Bounds Checking in C++....1288
Watch for Off-by-One Errors....1293
Checkpoint....1294
7.4 The Range-Based for Loop....1297
Modifying an Array with a Range-Based for Loop....1305
The Range-Based for Loop versus the Regular for Loop....1310
7.5 Processing Array Contents....1311
Thou Shall Not Assign....1319
Printing the Contents of an Array....1322
Summing the Values in a Numeric Array....1324
Getting the Average of the Values in a Numeric Array....1326
Finding the Highest and Lowest Values in a Numeric Array....1328
Partially Filled Arrays....1330
Comparing Arrays....1337
7.6 Focus on Software Engineering: Using Parallel Arrays....1340
Checkpoint....1346
7.7 Arrays as Function Arguments....1348
Using const Array Parameters....1367
Some Useful Array Functions....1368
Checkpoint....1386
7.8 Two-Dimensional Arrays....1390
Passing Two-Dimensional Arrays to Functions....1400
Summing All the Elements of a Two-Dimensional Array....1406
Summing the Rows of a Two-Dimensional Array....1407
Summing the Columns of a Two-Dimensional Array....1410
7.9 Arrays with Three or More Dimensions....1412
Checkpoint....1415
7.10 Focus on Problem Solving and Program Design: A Case Study....1417
7.11 Introduction to the STL vector....1424
Defining a vector....1426
Using an Initialization List with a vector in C++ 11....1429
Storing and Retrieving Values in a vector....1430
Using the Range-Based for Loop with a vector in C++ 11....1437
Using the push_back Member Function....1444
Determining the Size of a vector....1450
Removing Elements from a vector....1456
Clearing a vector....1460
Detecting an Empty vector....1463
Summary of vector Member Functions....1469
Checkpoint....1471
Review Questions and Exercises....1472
Short Answer....1473
Fill-in-the-Blank....1476
Algorithm Workbench....1478
True or False....1481
Find the Errors....1484
Programming Challenges....1487
Group Project....1502
Chapter 8 Searching and Sorting Arrays....1505
Topics....1505
8.1 Focus on Software Engineering: Introduction to Search Algorithms....1506
The Linear Search....1507
Inefficiency of the Linear Search....1514
The Binary Search....1515
The Efficiency of the Binary Search....1526
8.2 Focus on Problem Solving and Program Design: A Case Study....1527
Variables....1527
Modules....1528
Function main....1529
The getProdNum Function....1531
The binarySearch Function....1532
The displayProd Function....1533
The Entire Program....1534
Checkpoint....1544
8.3 Focus on Software Engineering: Introduction to Sorting Algorithms....1545
Sorting Algorithms....1546
The Bubble Sort....1547
Swapping Array Elements....1554
The Selection Sort Algorithm....1562
8.4 Focus on Problem Solving and Program Design: A Case Study....1573
Variables....1574
Modules....1574
Function main....1575
The calcSales Function....1577
The dualSort Function....1578
The Overloaded swap Functions....1582
The showOrder Function....1584
The showTotals Function....1586
The Entire Program....1588
8.5 Sorting and Searching vectors (Continued from Section 7.11)....1599
Review Questions and Exercises....1609
Short Answer....1609
Fill-in-the-Blank....1610
True or False....1611
Programming Challenges....1612
Chapter 9 Pointers....1619
Topics....1619
9.1 Getting the Address of a Variable....1620
9.2 Pointer Variables....1625
Creating and Using Pointer Variables....1632
9.3 The Relationship between Arrays and Pointers....1648
9.4 Pointer Arithmetic....1664
9.5 Initializing Pointers....1669
Checkpoint....1672
9.6 Comparing Pointers....1674
9.7 Pointers as Function Parameters....1680
Pointers to Constants....1695
Passing a Nonconstant Argument into a Pointer to a Constant....1698
Constant Pointers....1703
Constant Pointers to Constants....1706
9.8 Dynamic Memory Allocation....1708
9.9 Returning Pointers from Functions....1720
Checkpoint....1736
9.10 Using Smart Pointers to Avoid Memory Leaks....1740
9.11 Focus on Problem Solving and Program Design: A Case Study....1750
Variables....1751
Programming Strategy....1752
Functions....1754
Function main....1754
The arrSelectSort Function....1755
The showArrPtr Function....1756
The showArray Function....1756
The Entire Program....1757
Review Questions and Exercises....1765
Short Answer....1766
Fill-in-the-Blank....1768
Algorithm Workbench....1769
True or False....1771
Find the Error....1773
Programming Challenges....1777
Chapter 10 Characters, C-Strings, and More about the string Class....1783
Topics....1783
10.1 Character Testing....1784
10.2 Character Case Conversion....1796
Checkpoint....1804
10.3 C-Strings....1806
More about String Literals....1808
C-Strings Stored in Arrays....1811
10.4 Library Functions for Working with C-Strings....1818
The strlen Function....1819
The strcat Function....1821
The strcpy Function....1824
The strncat and strncpy Functions....1826
The strstr Function....1830
The strcmp Function....1837
Using ! with strcmp....1845
Sorting Strings....1846
Checkpoint....1850
10.5 String/Numeric Conversion Functions....1852
The string to Number Functions....1854
The to_string Function....1856
Checkpoint....1868
10.6 Focus on Software Engineering: Writing Your Own C-String-Handling Functions....1869
Using Pointers to Pass C-String Arguments....1881
Checkpoint....1888
10.7 More about the C++ string Class....1889
Using the string Class....1890
Reading a Line of Input into a string Object....1895
Comparing and Sorting string Objects....1896
Other Ways to Define string Objects....1905
Using string Class Member Functions....1911
10.8 Focus on Problem Solving and Program Design: A Case Study....1926
Review Questions and Exercises....1933
Short Answer....1934
Fill-in-the-Blank....1935
Algorithm Workbench....1937
True or False....1938
Find the Errors....1939
Programming Challenges....1940
Chapter 11 Structured Data....1950
Topics....1950
11.1 Abstract Data Types....1951
Abstraction....1952
Data Types....1953
Abstract Data Types....1955
11.2 Structures....1956
11.3 Accessing Structure Members....1965
Comparing Structure Variables....1976
11.4 Initializing a Structure....1977
Checkpoint....1984
11.5 Arrays of Structures....1988
Initializing a Structure Array....1995
11.6 Focus on Software Engineering: Nested Structures....1996
Checkpoint....2005
11.7 Structures as Function Arguments....2007
Constant Reference Parameters....2017
11.8 Returning a Structure from a Function....2019
11.9 Pointers to Structures....2027
Dynamically Allocating a Structure....2035
11.10 Focus on Software Engineering: When to Use ., When to Use −>, and When to Use *....2037
Checkpoint....2040
11.11 Enumerated Data Types....2042
Assigning an Integer to an enum Variable....2046
Assigning an Enumerator to an int Variable....2047
Comparing Enumerator Values....2048
Anonymous Enumerated Types....2053
Using Math Operators to Change the Value of an enum Variable....2054
Using an enum Variable to Step through an Array’s Elements....2055
Using Enumerators to Output Values....2061
Specifying Integer Values for Enumerators....2068
Enumerators Must Be Unique within the Same Scope....2073
Declaring the Type and Defining the Variables in One Statement....2074
Using Strongly Typed enums in C++ 11....2075
Checkpoint....2078
Review Questions and Exercises....2080
Short Answer....2081
Fill-in-the-Blank....2085
Algorithm Workbench....2086
True or False....2089
Find the Errors....2091
Programming Challenges....2095
Chapter 12 Advanced File Operations....2105
Topics....2105
12.1 File Operations....2106
Using the fstream Data Type....2108
File Open Modes with ifstream and ofstream Objects....2119
Checking for a File’s Existence Before Opening It....2120
Opening a File with the File Stream Object Definition Statement....2121
Checkpoint....2122
12.2 File Output Formatting....2123
12.3 Passing File Stream Objects to Functions....2131
12.4 More Detailed Error Testing....2138
12.5 Member Functions for Reading and Writing Files....2146
The getline Function....2151
The get Member Function....2161
The put Member Function....2165
Checkpoint....2169
12.6 Focus on Software Engineering: Working with Multiple Files....2172
12.7 Binary Files....2178
The write and read Member Functions....2181
Writing Data other than char to Binary Files....2188
12.8 Creating Records with Structures....2193
12.9 Random-Access Files....2207
The seekp and seekg Member Functions....2209
The tellp and tellg Member Functions....2222
Rewinding a Sequential-Access File with seekg....2229
12.10 Opening a File for Both Input and Output....2231
Checkpoint....2246
Review Questions and Exercises....2248
Short Answer....2249
Fill-in-the-Blank....2251
Algorithm Workbench....2253
True or False....2255
Find the Error....2257
Programming Challenges....2260
Group Project....2268
Chapter 13 Introduction to Classes....2270
Topics....2271
13.1 Procedural and Object-Oriented Programming....2273
Object Reusability....2279
Classes and Objects....2280
Using a Class You Already Know....2285
13.2 Introduction to Classes....2288
Access Specifiers....2291
Public Member Functions....2292
Using const with Member Functions....2294
Placement of public and private Members....2295
Defining Member Functions....2298
Accessors and Mutators....2302
Using const with Accessors....2303
The Importance of Data Hiding....2304
13.3 Defining an Instance of a Class....2305
Accessing an Object’s Members....2307
A Class Demonstration Program....2309
Avoiding Stale Data....2326
Pointers to Objects....2327
Using Smart Pointers to Allocate Objects....2336
Checkpoint....2343
13.4 Why Have Private Members?....2345
13.5 Focus on Software Engineering: Separating Class Specification from Implementation....2349
13.6 Inline Member Functions....2366
Inline Functions and Performance....2373
Checkpoint....2374
13.7 Constructors....2375
Using Member Initialization Lists....2391
In-Place Member Initialization....2393
The Default Constructor....2394
Default Constructors and Dynamically Allocated Objects....2395
13.8 Passing Arguments to Constructors....2396
Using Default Arguments with Constructors....2416
More about the Default Constructor....2423
Classes with No Default Constructor....2424
13.9 Destructors....2425
Destructors and Dynamically Allocated Class Objects....2435
Checkpoint....2437
13.10 Overloading Constructors....2439
Constructor Delegation....2449
Only One Default Constructor and One Destructor....2452
Other Overloaded Member Functions....2453
13.11 Private Member Functions....2454
13.12 Arrays of Objects....2459
Accessing Members of Objects in an Array....2463
Checkpoint....2467
13.13 Focus on Problem Solving and Program Design: An OOP Case Study....2470
Private Member Variables....2470
Public Member Functions....2471
The Class Declaration....2472
The withdraw Member Function....2476
The Class’s Interface....2478
Implementing the Class....2479
13.14 Focus on Object-Oriented Programming: Simulating Dice with Objects....2491
13.15 Focus on Object-Oriented Design: The Unified Modeling Language (UML)....2502
Showing Access Specification in UML Diagrams....2505
Data Type and Parameter Notation in UML Diagrams....2506
Showing Constructors and Destructors in a UML Diagram....2508
13.16 Focus on Object-Oriented Design: Finding the Classes and Their Responsibilities....2509
Finding the Classes....2510
Write a Description of the Problem Domain....2511
Identify All of the Nouns....2513
Refine the List of Nouns....2515
Identifying a Class’s Responsibilities....2521
The Customer class....2522
The Car Class....2524
The ServiceQuote Class....2526
This Is Only the Beginning....2528
Checkpoint....2529
Review Questions and Exercises....2531
Short Answer....2532
Fill-in-the-Blank....2534
Algorithm Workbench....2536
True or False....2538
Find the Errors....2540
Programming Challenges....2544
Group Project....2561
Chapter 14 More about Classes....2563
Topics....2563
14.1 Instance and Static Members....2564
Instance Variables....2565
Static Members....2567
Static Member Variables....2568
Static Member Functions....2581
14.2 Friends of Classes....2593
Checkpoint....2612
14.3 Memberwise Assignment....2613
14.4 Copy Constructors....2618
Using const Parameters in Copy Constructors....2628
Copy Constructors and Function Parameters....2633
The Default Copy Constructor....2634
Checkpoint....2634
14.5 Operator Overloading....2635
The this Pointer....2638
Overloading the = Operator....2639
Checking for Self-Assignment....2643
The = Operator’s Return Value....2647
Copy Assignment....2649
Some General Issues of Operator Overloading....2658
Overloading Math Operators....2661
Overloading the Prefix ++ Operator....2677
Overloading the Postfix ++ Operator....2679
Checkpoint....2686
Overloading Relational Operators....2688
Overloading the << and >> Operators....2696
Overloading the [ ] Operator....2710
Checkpoint....2731
14.6 Object Conversion....2732
Checkpoint....2739
14.7 Aggregation....2740
Using Member Initialization Lists with Aggregate Classes....2754
Aggregation in UML Diagrams....2756
14.8 Focus on Object-Oriented Design: Class Collaborations....2758
Determining Class Collaborations with CRC Cards....2769
Checkpoint....2771
14.9 Focus on Object-Oriented Programming: Simulating the Game of Cho-Han....2772
14.10 Rvalue References and Move Semantics....2804
Lvalues and Rvalues....2805
Rvalue References....2810
Move Semantics....2813
When to Implement Move Semantics in a Class....2827
Default Operations Provided by the Compiler....2828
Review Questions and Exercises....2830
Short Answer....2831
Fill-in-the-Blank....2835
Algorithm Workbench....2836
True or False....2837
Find the Errors....2839
Programming Challenges....2844
Chapter 15 Inheritance, Polymorphism, and Virtual Functions....2857
Topics....2857
15.1 What Is Inheritance?....2858
Generalization and Specialization....2859
Inheritance and the “Is a” Relationship....2860
Checkpoint....2886
15.2 Protected Members and Class Access....2889
More about Base Class Access Specification....2905
Checkpoint....2908
15.3 Constructors and Destructors in Base and Derived Classes....2911
Passing Arguments to Base Class Constructors....2916
Constructor Inheritance....2950
Checkpoint....2954
15.4 Redefining Base Class Functions....2958
15.5 Class Hierarchies....2973
15.6 Polymorphism and Virtual Member Functions....2993
Polymorphism Requires References or Pointers....3017
Base Class Pointers....3024
Base Class Pointers and References Know Only about Base Class Members....3031
The “Is-a” Relationship Does Not Work in Reverse....3033
Redefining versus Overriding....3035
Virtual Destructors....3036
C++ 11’s override and final Key Words....3045
Preventing a Member Function from Being Overridden....3053
15.7 Abstract Base Classes and Pure Virtual Functions....3054
Checkpoint....3070
15.8 Multiple Inheritance....3079
Checkpoint....3097
Review Questions and Exercises....3100
Short Answer....3101
Fill-in-the-Blank....3103
Algorithm Workbench....3105
True or False....3107
Find the Errors....3109
Programming Challenges....3112
Group Project....3124
Chapter 16 Exceptions and Templates....3126
Topics....3126
16.1 Exceptions....3127
Throwing an Exception....3129
Handling an Exception....3131
What if an Exception Is Not Caught?....3139
Object-Oriented Exception Handling with Classes....3140
Multiple Exceptions....3152
Using Exception Handlers to Recover from Errors....3165
Extracting Data from the Exception Class....3171
Unwinding the Stack....3185
Rethrowing an Exception....3186
Handling the bad_alloc Exception....3189
Checkpoint....3192
16.2 Function Templates....3193
Introduction....3194
Using Operators in Function Templates....3207
Function Templates with Multiple Types....3208
Overloading with Function Templates....3212
16.3 Focus on Software Engineering: Where to Start When Defining Templates....3216
Checkpoint....3217
16.4 Class Templates....3218
Defining Objects of the Class Template....3229
Class Templates and Inheritance....3235
Specialized Templates....3245
Checkpoint....3246
Review Questions and Exercises....3248
Short Answer....3248
Fill-in-the-Blank....3249
Algorithm Workbench....3250
True or False....3251
Find the Error....3252
Programming Challenges....3254
Chapter 17 The Standard Template Library....3258
Topics....3258
17.1 Introduction to the Standard Template Library....3259
17.2 STL Container and Iterator Fundamentals....3261
Containers....3262
Introduction to the array Class....3265
Iterators....3271
Defining an Iterator....3274
Getting an Iterator from a Container Object....3275
Using auto to Define an Iterator....3281
Mutable Iterators and const_iterators....3285
Reverse Iterators....3287
Checkpoint....3289
17.3 The vector Class....3290
Review of Basic vector Operations....3293
Using an Iterator with a vector....3299
Inserting New Elements into a vector....3301
Storing Objects of Your Own Classes as Values in a vector....3308
Inserting Elements with the emplace()and emplace_back()Member Functions....3318
The capacity(), max_size(), shrink_to_fit(), and reserve() Member Functions....3326
Checkpoint....3328
17.4 The map, multimap, and unordered_map Classes....3329
The map Class....3331
Initializing a Map....3334
Adding Elements to an Existing Map....3336
Adding Elements with the insert() Member Function....3338
Adding Elements with the emplace() Member Function....3340
Retrieving Values from a Map....3341
Deleting Elements....3343
Iterating Over a Map with the Range-Based for Loop....3344
Using an Iterator with a Map....3346
Storing vectors as Values in a map....3353
Storing Objects of Your Own Classes as Values in a map....3362
Using an Object of Your Own Class as a Key....3378
The unordered_map Class....3388
The multimap Class....3390
Adding Elements to a multimap....3396
Getting the Number of Elements with a Specified Key....3398
Retrieving the Elements with a Specified Key....3401
Deleting Elements from a multimap....3407
The unordered_multimap Class....3408
Checkpoint....3410
17.5 The set, multiset, and unordered_set Classes....3411
Adding Elements to an Existing set....3415
Iterating Over a set with the Range-Based for Loop....3417
Using an Iterator with a set....3418
Determining Whether a Value Exists in a set....3420
Storing Objects of Your Own Classes in a set....3422
The multiset Class....3427
The unordered_set and unordered_multiset Classes....3428
Checkpoint....3429
17.6 Algorithms....3430
Sorting and Searching Algorithms....3433
Detecting Permutations....3443
Plugging Your Own Functions into an Algorithm....3450
Using the STL to Perform Set Operations....3460
Finding the Union of Sets with the set_union Function....3462
Finding the Intersection of Sets with the set_intersection Function....3468
Finding the Difference of Sets with the set_difference Function....3473
Finding the Symmetric Difference of Sets with the set_symmetric_difference Function....3478
Finding Subsets....3483
17.7 Introduction to Function Objects and Lambda Expressions....3502
Constructing an Anonymous Function Object....3511
Predicate Terminology....3513
Lambda Expressions....3514
Functional Classes in the STL....3521
Checkpoint....3526
Review Questions and Exercises....3527
Short Answer....3528
Fill-in-the-Blank....3530
True or False....3532
Algorithm Workbench....3534
Find the Errors....3537
Programming Challenges....3539
Chapter 18 Linked Lists....3546
Topics....3546
18.1 Introduction to the Linked List ADT....3547
Advantages of Linked Lists over Arrays and vectors....3548
The Composition of a Linked List....3549
Declarations....3551
Checkpoint....3552
18.2 Linked List Operations....3553
Appending a Node to the List....3557
Traversing a Linked List....3571
Inserting a Node....3575
Checkpoint....3585
Deleting a Node....3586
Destroying the List....3595
Checkpoint....3597
18.3 A Linked List Template....3598
Using a Class Node Type....3615
18.4 Variations of the Linked List....3631
18.5 The STL list and forward_list Containers....3633
The list Container....3635
The forward_list Container....3641
Review Questions and Exercises....3642
Short Answer....3642
Fill-in-the-Blank....3643
Algorithm Workbench....3644
True or False....3645
Find the Errors....3646
Programming Challenges....3649
Chapter 19 Stacks and Queues....3654
Topics....3654
19.1 Introduction to the Stack ADT....3655
Definition....3656
Applications of Stacks....3657
Static and Dynamic Stacks....3657
Stack Operations....3658
A Static Stack Class....3660
Implementing Other Stack Operations....3679
A Static Stack Template....3689
19.2 Dynamic Stacks....3709
A Dynamic Stack Template....3725
19.3 The STL stack Container....3741
Checkpoint....3748
19.4 Introduction to the Queue ADT....3749
Definition....3749
Application of Queues....3750
Static and Dynamic Queues....3750
Queue Operations....3751
Detecting Full and Empty Queues with Circular Arrays....3757
A Static Queue Class....3758
A Static Queue Template....3773
19.5 Dynamic Queues....3786
A Dynamic Queue Template....3800
19.6 The STL deque and queue Containers....3811
The deque Container....3812
The queue Container Adapter....3817
Review Questions and Exercises....3821
Short Answer....3821
Fill-in-the-Blank....3822
Algorithm Workbench....3823
True or False....3826
Programming Challenges....3827
Chapter 20 Recursion....3832
Topics....3833
20.1 Introduction to Recursion....3834
20.2 Solving Problems with Recursion....3847
Example: Using Recursion to Calculate the Factorial of a Number....3850
Example: Using Recursion to Count Characters....3859
Direct and Indirect Recursion....3868
Checkpoint....3869
20.3 Focus on Problem Solving and Program Design: The Recursive gcd Function....3870
20.4 Focus on Problem Solving and Program Design: Solving Recursively Defined Problems....3874
20.5 Focus on Problem Solving and Program Design: Recursive Linked List Operations....3879
Counting the Nodes in the List....3883
Displaying List Nodes in Reverse Order....3887
20.6 Focus on Problem Solving and Program Design: A Recursive Binary Search Function....3892
20.7 The Towers of Hanoi....3900
20.8 Focus on Problem Solving and Program Design: The QuickSort Algorithm....3910
20.9 Exhaustive Algorithms....3922
20.10 Focus on Software Engineering: Recursion versus Iteration....3932
Review Questions and Exercises....3934
Short Answer....3934
Fill-in-the-Blank....3935
Algorithm Workbench....3935
Predict the Output....3936
Programming Challenges....3939
Chapter 21 Binary Trees....3943
Topics....3943
21.1 Definition and Applications of Binary Trees....3944
Applications of Binary Trees....3947
Checkpoint....3951
21.2 Binary Search Tree Operations....3952
Creating a Binary Trees....3953
Inserting a Node....3958
Traversing the Tree....3966
Searching the Tree....3975
Deleting a Node....3979
Checkpoint....4003
21.3 Template Considerations for Binary Search Trees....4004
Review Questions and Exercises....4021
Short Answer....4021
Fill-in-the-Blank....4022
Algorithm Workbench....4023
True or False....4024
Programming Challenges....4025
Appendix A The ASCII Character Set....4029
Appendix B Operator Precedence and Associativity....4030
C++ Quick Reference....4031
Index....4034
Symbols....4035
A....4038
B....4043
C....4046
D....4056
E....4061
F....4066
G....4073
H....4074
I....4076
J....4081
K....4082
L....4083
M....4088
N....4092
O....4094
P....4099
Q....4104
R....4105
S....4109
T....4118
U....4121
V....4123
W....4126
Z....4127
Credits....4128
For two-semester courses in the C++ programming sequence, or an accelerated one-semester course.
A clear and student-friendly way to teach the fundamentals of C++
Starting Out with C++: From Control Structures through Objectscovers control structures, functions, arrays, and pointers before objects and classes in Tony Gaddis’s hallmark accessible, step-by-step presentation. His books help beginning students understand the important details necessary to become skilled programmers at an introductory level. Gaddis motivates the study of both programming skills and the C++ programming language by presenting all the details needed to understand the “how” and the “why”–but never losing sight of the fact that most beginners struggle with this material. His approach is gradual and highly accessible, ensuring that students understand the logic behind developing high-quality programs. As with all Gaddis texts, clear and easy-to-read code listings, concise and practical real-world examples, and an abundance of exercises appear in every chapter. Updates to the 9th Edition include revised, improved problems throughout and a new chapter featuring completely rewritten and expanded material on the Standard Template Library (STL).
Also Available with MyLab Programming.
MyLabTM Programming is an online learning system designed to engage students and improve results. MyLab Programming consists of programming exercises correlated to the concepts and objectives in this book. Through practice exercises and immediate, personalized feedback, MyLab Programming improves the programming competence of beginning students who often struggle with the basic concepts of programming languages.