C++ How to Program

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Paul Deitel, Harvey Deitel
Edition: 10
Publisher: Deitel
Year: 2017

Language: English
Pages: 3015

C++ HOW TO PROGRAM Introducing the New C++14 Standard......Page 2
How To Program Series......Page 3
Deitel® Developer Series......Page 4
Simply Series......Page 5
VitalSource Web Books......Page 6
LiveLessons Video Learning Products......Page 7
C++ How to Program Introducing the New C++14 Standard......Page 9
Trademarks......Page 14
Contents......Page 16
Preface......Page 50
Contacting the Authors......Page 52
Join the Deitel & Associates, Inc. Social Media Communities......Page 53
The C++11 and C++14 Standards......Page 54
Key Features of C++ How to Program, 10/e......Page 59
New in This Edition......Page 61
Object-Oriented Programming......Page 63
Hundreds of Code Examples......Page 66
Exercises......Page 69
Illustrations and Figures......Page 72
Dependency Chart......Page 75
Teaching Approach......Page 76
Secure C++ Programming......Page 81
Online Chapters, Appendices and Other Content......Page 83
Obtaining the Software Used in C++ How to Program, 10/e......Page 85
Instructor Supplements......Page 87
Online Practice and Assessment with MyProgrammingLab™......Page 89
Reviewers......Page 91
About the Authors......Page 94
Before You Begin......Page 98
1 Introduction to Computers and C++......Page 104
Objectives......Page 105
Outline......Page 106
1.1 Introduction......Page 108
1.2 Computers and the Internet in Industry and Research......Page 110
1.3 Hardware and Software......Page 114
1.4 Data Hierarchy......Page 119
1.5 Machine Languages, Assembly Languages and High-Level Languages......Page 125
1.6 C and C++......Page 128
1.7 Programming Languages......Page 132
1.8 Introduction to Object Technology......Page 136
1.9 Typical C++ Development Environment......Page 143
1.10 Test-Driving a C++ Application......Page 149
1.10.1 Compiling and Running an Application in Visual Studio 2015 for Windows......Page 151
1.10.2 Compiling and Running Using GNU C++ on Linux......Page 160
1.10.3 Compiling and Running with Xcode on Mac OS X......Page 165
1.11 Operating Systems......Page 174
1.12 The Internet and the World Wide Web......Page 180
1.13 Some Key Software Development Terminology......Page 185
1.14 C++11 and C++14: The Latest C++ Versions......Page 189
1.15 Boost C++ Libraries......Page 191
1.16 Keeping Up to Date with Information Technologies......Page 192
Self-Review Exercises......Page 194
Exercises......Page 196
Making a Difference......Page 198
Making a Difference Resources......Page 202
2 Introduction to C++ Programming, Input/Output and Operators......Page 205
Objectives......Page 206
Outline......Page 207
2.1 Introduction......Page 208
2.2 First Program in C++: Printing a Line of Text......Page 209
2.3 Modifying Our First C++ Program......Page 219
2.4 Another C++ Program: Adding Integers......Page 221
2.5 Memory Concepts......Page 233
2.6 Arithmetic......Page 236
2.7 Decision Making: Equality and Relational Operators......Page 245
2.8 Wrap-Up......Page 256
Summary......Page 257
Self-Review Exercises......Page 265
Exercises......Page 269
Making a Difference......Page 277
3 Introduction to Classes, Objects, Member Functions and Strings......Page 283
Objectives......Page 284
Outline......Page 285
3.1 Introduction 1......Page 287
3.2 Test-Driving an Account Object......Page 289
3.2.1 Instantiating an Object......Page 291
3.2.2 Headers and Source-Code Files......Page 292
3.2.3 Calling Class Account’s getName Member Function......Page 294
3.2.4 Inputting a string with getline......Page 296
3.2.5 Calling Class Account’s setName Member Function......Page 299
3.3 Account Class with a Data Member and Set and Get Member Functions......Page 301
3.3.1 Account Class Definition......Page 302
3.3.2 Keyword class and the Class Body......Page 303
3.3.3 Data Member name of Type string......Page 305
3.3.4 setName Member Function......Page 307
3.3.5 getName Member Function......Page 312
3.3.6 Access Specifiers private and public......Page 315
3.3.7 Account UML Class Diagram......Page 318
3.4 Account Class: Initializing Objects with Constructors......Page 321
3.4.1 Defining an Account Constructor for Custom Object Initialization......Page 323
3.4.2 Initializing Account Objects When They’re Created......Page 327
3.4.3 Account UML Class Diagram with a Constructor......Page 331
3.5 Software Engineering with Set and Get Member Functions......Page 332
3.6 Account Class with a Balance; Data Validation......Page 336
3.6.1 Data Member balance......Page 337
3.6.2 Two-Parameter Constructor with Validation......Page 340
3.6.3 deposit Member Function with Validation......Page 341
3.6.4 getBalance Member Function......Page 343
3.6.5 Manipulating Account Objects with Balances......Page 344
3.6.6 Account UML Class Diagram with a Balance and Member Functions deposit and getBalance......Page 349
3.7 Wrap-Up......Page 351
Summary......Page 352
Self-Review Exercises......Page 377
Exercises......Page 379
Making a Difference......Page 383
4 Algorithm Development and Control Statements: Part 1......Page 388
Objectives......Page 389
Outline......Page 390
4.1 Introduction......Page 393
4.2 Algorithms......Page 394
4.3 Pseudocode......Page 395
4.4 Control Structures......Page 397
4.5 if Single-Selection Statement......Page 405
4.6 if…else Double-Selection Statement......Page 409
4.7 Student Class: Nested if…else Statements......Page 420
4.8 while Iteration Statement......Page 425
4.9 Formulating Algorithms: Counter-Controlled Iteration......Page 429
4.9.1 Pseudocode Algorithm with Counter-Controlled Iteration......Page 430
4.9.2 Implementing Counter-Controlled Iteration......Page 432
4.9.3 Notes on Integer Division and Truncation......Page 437
4.9.4 Arithmetic Overflow......Page 438
4.9.5 Input Validation......Page 440
4.10 Formulating Algorithms: Sentinel-Controlled Iteration......Page 441
4.10.1 Top-Down, Stepwise Refinement: The Top and First Refinement......Page 443
4.10.2 Proceeding to the Second Refinement......Page 445
4.10.3 Implementing Sentinel-Controlled Iteration......Page 450
4.10.4 Converting Between Fundamental Types Explicitly and Implicitly......Page 455
4.10.5 Formatting Floating-Point Numbers......Page 458
4.10.6 Unsigned Integers and User Input......Page 461
4.11 Formulating Algorithms: Nested Control Statements......Page 462
4.11.1 Problem Statement......Page 463
4.11.2 Top-Down, Stepwise Refinement: Pseudocode Representation of the Top......Page 465
4.11.3 Top-Down, Stepwise Refinement: First Refinement......Page 466
4.11.4 Top-Down, Stepwise Refinement: Second Refinement......Page 467
4.11.5 Complete Second Refinement of the Pseudocode......Page 470
4.11.6 Program That Implements the Pseudocode Algorithm......Page 472
4.11.7 Preventing Narrowing Conversions with List Initialization......Page 476
4.12 Compound Assignment Operators......Page 479
4.13 Increment and Decrement Operators......Page 481
4.14 Fundamental Types Are Not Portable......Page 489
4.15 Wrap-Up......Page 490
Summary......Page 491
Self-Review Exercises......Page 524
Exercises......Page 533
Making a Difference......Page 552
5 Control Statements: Part 2; Logical Operators......Page 554
Objectives......Page 555
Outline......Page 556
5.1 Introduction......Page 558
5.2 Essentials of Counter-Controlled Iteration......Page 559
5.3 for Iteration Statement......Page 562
5.4 Examples Using the for Statement......Page 572
5.5 Application: Summing Even Integers......Page 575
5.6 Application: Compound-Interest Calculations......Page 578
5.7 Case Study: Integer-Based Monetary Calculations with Class DollarAmount......Page 587
5.7.1 Demonstrating Class DollarAmount......Page 592
5.7.2 Class DollarAmount......Page 597
C++11 Type int64_t......Page 599
DollarAmount Constructor......Page 601
DollarAmount Member Functions add and subtract......Page 602
DollarAmount Member Function addInterest......Page 603
Member Function toString......Page 606
Banker’s Rounding......Page 607
Even int64_t Is Limited......Page 608
A Note About Arithmetic Operators and Modifying Operands......Page 609
5.8 do…while Iteration Statement......Page 610
5.9 switch Multiple-Selection Statement......Page 613
5.10 break and continue Statements......Page 626
5.11 Logical Operators......Page 630
5.11.1 Logical AND (&&) Operator......Page 631
5.11.2 Logical OR (||) Operator......Page 633
5.11.3 Short-Circuit Evaluation......Page 635
5.11.4 Logical Negation (!) Operator......Page 637
5.11.5 Logical Operators Example......Page 639
5.12 Confusing the Equality (==) and Assignment (=) Operators......Page 643
5.13 Structured-Programming Summary......Page 647
5.14 Wrap-Up......Page 656
Summary......Page 657
Self-Review Exercises......Page 673
Exercises......Page 681
Making a Difference......Page 694
6 Functions and an Introduction to Recursion......Page 696
Objectives......Page 697
Outline......Page 698
6.1 Introduction......Page 700
6.2 Program Components in C++......Page 702
6.3 Math Library Functions......Page 705
6.4 Function Prototypes......Page 709
6.5 Function-Prototype and Argument-Coercion Notes......Page 717
6.6 C++ Standard Library Headers......Page 724
6.7 Case Study: Random-Number Generation2......Page 728
6.7.1 Rolling a Six-Sided Die......Page 730
6.7.2 Rolling a Six-Sided Die 60,000,000 Times......Page 732
6.7.3 Randomizing the Random-Number Generator with srand......Page 735
6.7.4 Seeding the Random-Number Generator with the Current Time......Page 738
6.7.5 Scaling and Shifting Random Numbers......Page 739
6.8 Case Study: Game of Chance; Introducing Scoped enums......Page 741
6.9 C++11 Random Numbers......Page 750
6.10 Scope Rules......Page 753
6.11 Function-Call Stack and Activation Records......Page 761
6.12 Inline Functions......Page 768
6.13 References and Reference Parameters......Page 771
6.14 Default Arguments......Page 780
6.15 Unary Scope Resolution Operator......Page 784
6.16 Function Overloading......Page 788
6.17 Function Templates......Page 794
6.18 Recursion......Page 799
6.19 Example Using Recursion: Fibonacci Series......Page 807
6.20 Recursion vs. Iteration......Page 815
6.21 Wrap-Up......Page 822
Summary......Page 824
Self-Review Exercises......Page 845
Exercises......Page 858
Making a Difference......Page 879
7 Class Templates array and vector; Catching Exceptions......Page 883
Objectives......Page 884
Outline......Page 885
7.1 Introduction......Page 887
7.2 arrays......Page 888
7.3 Declaring arrays......Page 893
7.4 Examples Using arrays The following examples demonstrate how to declare, initialize and manipulate arrays.......Page 895
7.4.1 Declaring an array and Using a Loop to Initialize the array’s Elements......Page 896
7.4.2 Initializing an array in a Declaration with an Initializer List......Page 899
7.4.3 Specifying an array’s Size with a Constant Variable and Setting array Elements with Calculations......Page 902
7.4.4 Summing the Elements of an array......Page 906
7.4.5 Using a Bar Chart to Display array Data Graphically......Page 908
7.4.6 Using the Elements of an array as Counters......Page 911
7.4.7 Using arrays to Summarize Survey Results......Page 914
7.4.8 Static Local arrays and Automatic Local arrays......Page 920
7.5 Range-Based for Statement......Page 925
7.6 Case Study: Class GradeBook Using an array to Store Grades......Page 929
7.7 Sorting and Searching arrays......Page 941
7.8 Multidimensional arrays......Page 945
7.9 Case Study: Class GradeBook Using a Two-Dimensional array......Page 953
7.10 Introduction to C++ Standard Library Class Template vector......Page 965
7.11 Wrap-Up......Page 978
Summary......Page 980
Self-Review Exercises......Page 992
Exercises......Page 996
Recursion Exercises......Page 1014
Making a Difference......Page 1017
8 Pointers......Page 1022
Objectives......Page 1023
Outline......Page 1024
8.1 Introduction......Page 1027
8.2 Pointer Variable Declarations and Initialization......Page 1029
8.3 Pointer Operators......Page 1033
8.4 Pass-by-Reference with Pointers......Page 1040
8.5 Built-In Arrays......Page 1048
8.6 Using const with Pointers......Page 1056
8.7 sizeof Operator......Page 1065
8.8 Pointer Expressions and Pointer Arithmetic......Page 1070
8.9 Relationship Between Pointers and Built-In Arrays......Page 1078
8.10 Pointer-Based Strings (Optional)......Page 1085
8.11 Note About Smart Pointers......Page 1093
8.12 Wrap-Up......Page 1094
Summary......Page 1096
Self-Review Exercises......Page 1109
Exercises......Page 1114
Special Section: Building Your Own Computer......Page 1123
9 Classes: A Deeper Look......Page 1144
Objectives......Page 1145
Outline......Page 1146
9.1 Introduction......Page 1149
9.2 Time Class Case Study: Separating Interface from Implementation......Page 1152
9.2.1 Interface of a Class......Page 1154
9.2.2 Separating the Interface from the Implementation......Page 1155
9.2.3 Time Class Definition......Page 1156
9.2.4 Time Class Member Functions......Page 1160
9.2.5 Scope Resolution Operator (::)......Page 1162
9.2.6 Including the Class Header in the Source-Code File......Page 1164
9.2.7 Time Class Member Function setTime and Throwing Exceptions......Page 1165
9.2.8 Time Class Member Function toUniversalString and String Stream Processing......Page 1167
9.2.9 Time Class Member Function toStandardString......Page 1169
9.2.10 Implicitly Inlining Member Functions......Page 1170
9.2.11 Member Functions vs. Global Functions......Page 1172
9.2.12 Using Class Time......Page 1174
9.2.13 Object Size......Page 1177
9.3 Compilation and Linking Process......Page 1178
9.4 Class Scope and Accessing Class Members......Page 1183
9.5 Access Functions and Utility Functions......Page 1186
9.6 Time Class Case Study: Constructors with Default Arguments The program of Figs. 9.5–9.7 enhances class Time to demonstrate how arguments can be passed to a constructor implicitly.......Page 1188
9.6.1 Constructors with Default Arguments......Page 1189
9.6.2 Overloaded Constructors and C++11 Delegating Constructors......Page 1199
9.7 Destructors......Page 1201
9.8 When Constructors and Destructors Are Called......Page 1203
9.8.1 Constructors and Destructors for Objects in Global Scope......Page 1204
9.8.2 Constructors and Destructors for Non-static Local Objects......Page 1205
9.8.3 Constructors and Destructors for static Local Objects......Page 1206
9.8.4 Demonstrating When Constructors and Destructors Are Called......Page 1207
9.9 Time Class Case Study: A Subtle Trap — Returning a Reference or a Pointer to a private Data Member......Page 1212
9.10 Default Memberwise Assignment......Page 1217
9.11 const Objects and const Member Functions......Page 1220
9.12 Composition: Objects as Members of Classes......Page 1225
9.13 friend Functions and friend Classes......Page 1237
9.14 Using the this Pointer......Page 1242
9.14.1 Implicitly and Explicitly Using the this Pointer to Access an Object’s Data Members......Page 1245
9.14.2 Using the this Pointer to Enable Cascaded Function Calls......Page 1248
9.15 static Class Members......Page 1254
9.15.1 Motivating Classwide Data......Page 1255
9.15.2 Scope and Initialization of static Data Members......Page 1257
9.15.3 Accessing static Data Members......Page 1258
9.15.4 Demonstrating static Data Members......Page 1260
9.16 Wrap-Up......Page 1266
Summary......Page 1269
Self-Review Exercises......Page 1297
Exercises......Page 1300
Making a Difference......Page 1314
10 Operator Overloading; Class string......Page 1318
Objectives......Page 1319
Outline......Page 1320
10.1 Introduction......Page 1322
10.2 Using the Overloaded Operators of Standard Library Class string......Page 1325
10.3 Fundamentals of Operator Overloading......Page 1334
10.4 Overloading Binary Operators......Page 1339
10.5 Overloading the Binary Stream Insertion and Stream Extraction Operators......Page 1341
10.6 Overloading Unary Operators......Page 1351
10.7 Overloading the Increment and Decrement Operators......Page 1353
10.8 Case Study: A Date Class......Page 1358
10.9 Dynamic Memory Management......Page 1367
10.10 Case Study: Array Class......Page 1374
10.10.1 Using the Array Class......Page 1376
10.10.2 Array Class Definition......Page 1384
Overloading the Stream Insertion and Stream Extraction Operators as friends......Page 1389
Range-Based for Does Not Work with Dynamically Allocated Built-In Arrays......Page 1391
Array Default Constructor......Page 1392
Array Copy Constructor......Page 1393
Array Destructor......Page 1395
getSize Member Function......Page 1396
Overloaded Assignment Operator......Page 1397
C++11: Move Constructor and Move Assignment Operator......Page 1399
C++11: Deleting Unwanted Member Functions from Your Class......Page 1400
Overloaded Equality and Inequality Operators......Page 1401
Overloaded Subscript Operators......Page 1403
C++11: Managing Dynamically Allocated Memory with unique_ptr......Page 1405
C++11: Passing a List Initializer to a Constructor......Page 1406
10.11 Operators as Member vs. Non-Member Functions......Page 1408
10.12 Converting Between Types......Page 1410
10.13 explicit Constructors and Conversion Operators......Page 1414
10.14 Overloading the Function Call Operator ()......Page 1420
10.15 Wrap-Up......Page 1422
Summary......Page 1424
Self-Review Exercises......Page 1441
Exercises......Page 1443
11 Object-Oriented Programming: Inheritance......Page 1455
Objectives......Page 1456
Outline......Page 1457
11.1 Introduction......Page 1459
11.2 Base Classes and Derived Classes......Page 1461
11.3 Relationship between Base and Derived Classes......Page 1467
11.3.1 Creating and Using a CommissionEmployee Class......Page 1468
11.3.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance......Page 1475
11.3.3 Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy......Page 1485
11.3.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data......Page 1494
Defining Base-Class CommissionEmployee with protected Data......Page 1496
Class BasePlusCommissionEmployee......Page 1498
Testing the Modified BasePlusCommissionEmployee Class......Page 1499
Notes on Using protected Data......Page 1501
11.3.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data......Page 1504
Changes to Class CommissionEmployee’s Member-Function Definitions......Page 1505
Changes to Class BasePlusCommissionEmployee’s Member-Function Definitions......Page 1509
BasePlusCommissionEmployee Member Function earnings......Page 1511
BasePlusCommissionEmployee Member Function toString......Page 1513
Testing the Modified Class Hierarchy......Page 1514
Summary of the CommissionEmployee–BasePlusCommissionEmployee Examples......Page 1515
11.4 Constructors and Destructors in Derived Classes......Page 1516
11.5 public, protected and private Inheritance......Page 1521
11.6 Wrap-Up......Page 1524
Summary......Page 1525
Self-Review Exercises......Page 1531
Exercises......Page 1533
12 Object-Oriented Programming: Polymorphism......Page 1540
Objectives......Page 1541
Outline......Page 1542
12.1 Introduction......Page 1544
12.2 Introduction to Polymorphism: Polymorphic Video Game......Page 1546
12.3 Relationships Among Objects in an Inheritance Hierarchy......Page 1549
12.3.1 Invoking Base-Class Functions from Derived-Class Objects......Page 1551
12.3.2 Aiming Derived-Class Pointers at Base-Class Objects......Page 1557
12.3.3 Derived-Class Member-Function Calls via Base-Class Pointers......Page 1559
12.4 Virtual Functions and Virtual Destructors......Page 1563
12.4.1 Why virtual Functions Are Useful......Page 1564
12.4.2 Declaring virtual Functions......Page 1565
12.4.3 Invoking a virtual Function Through a Base-Class Pointer or Reference......Page 1568
12.4.4 Invoking a virtual Function Through an Object’s Name......Page 1569
12.4.5 virtual Functions in the CommissionEmployee Hierarchy......Page 1570
12.4.6 virtual Destructors......Page 1578
12.4.7 C++11: final Member Functions and Classes......Page 1581
12.5 Type Fields and switch Statements......Page 1583
12.6 Abstract Classes and Pure virtual Functions......Page 1585
12.7 Case Study: Payroll System Using Polymorphism......Page 1590
12.7.1 Creating Abstract Base Class Employee......Page 1593
12.7.2 Creating Concrete Derived Class SalariedEmployee......Page 1600
12.7.3 Creating Concrete Derived Class CommissionEmployee......Page 1605
12.7.4 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee......Page 1609
12.7.5 Demonstrating Polymorphic Processing......Page 1613
12.8 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”......Page 1619
12.9 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info......Page 1628
12.10 Wrap-Up......Page 1636
Summary......Page 1638
Section 12.1 Introduction......Page 1639
Section 12.2 Introduction to Polymorphism: Polymorphic Video Game......Page 1640
Section 12.3 Relationships Among Objects in an Inheritance Hierarchy......Page 1641
Section 12.4.2 Declaring virtual Functions Polymorphism is implemented via virtual functions (p. 540) and dynamic binding (p. 541).......Page 1642
Section 12.4.3 Invoking a virtual Function Through a Base-Class Pointer or Reference......Page 1643
Section 12.4.4 Invoking a virtual Function Through an Object’s Name......Page 1644
Section 12.4.5 virtual Functions in the CommissionEmployee Hierarchy......Page 1645
Section 12.4.6 virtual Destructors......Page 1646
Section 12.4.7 C++11: final Member Functions and Classes......Page 1647
Section 12.5 Type Fields and switch Statements......Page 1648
Section 12.6 Abstract Classes and Pure virtual Functions......Page 1649
Section 12.6.1 Pure Virtual Functions......Page 1650
Section 12.8 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”......Page 1651
Section 12.9 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info......Page 1653
Self-Review Exercises......Page 1655
Exercises......Page 1657
Making a Difference......Page 1661
13 Stream Input/Output: A Deeper Look......Page 1664
Objectives......Page 1665
Outline......Page 1666
13.1 Introduction......Page 1668
13.2 Streams......Page 1670
13.3 Stream Output......Page 1677
13.4 Stream Input......Page 1680
13.4.1 get and getline Member Functions......Page 1681
13.4.2 istream Member Functions peek, putback and ignore......Page 1686
13.4.3 Type-Safe I/O......Page 1687
13.5 Unformatted I/O Using read, write and gcount......Page 1688
13.6 Stream Manipulators: A Deeper Look......Page 1690
13.6.1 Integral Stream Base: dec, oct, hex and setbase......Page 1691
13.6.2 Floating-Point Precision (precision, setprecision)......Page 1693
13.6.3 Field Width (width, setw)......Page 1696
13.6.4 User-Defined Output Stream Manipulators......Page 1699
13.7 Stream Format States and Stream Manipulators......Page 1701
13.7.1 Trailing Zeros and Decimal Points (showpoint)......Page 1703
13.7.2 Justification (left, right and internal)......Page 1705
13.7.3 Padding (fill, setfill)......Page 1708
13.7.4 Integral Stream Base (dec, oct, hex, showbase)......Page 1711
13.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)......Page 1713
13.7.6 Uppercase/Lowercase Control (uppercase)......Page 1715
13.7.7 Specifying Boolean Format (boolalpha)......Page 1716
13.7.8 Setting and Resetting the Format State via Member Function flags......Page 1718
13.8 Stream Error States......Page 1720
13.9 Tying an Output Stream to an Input Stream......Page 1727
13.10 Wrap-Up......Page 1729
Summary......Page 1731
Section 13.1 Introduction......Page 1732
Section 13.2 Streams......Page 1733
Section 13.2.2 iostream Library Headers......Page 1734
Section 13.2.3 Stream Input/Output Classes and Objects......Page 1735
Section 13.3 Stream Output......Page 1736
Section 13.4 Stream Input......Page 1737
Section 13.5 Unformatted I/O Using read, write and gcount......Page 1739
Section 13.6 Stream Manipulators: A Deeper Look......Page 1740
Section 13.7 Stream Format States and Stream Manipulators......Page 1742
Section 13.8 Stream Error States......Page 1744
Section 13.9 Tying an Output Stream to an Input Stream......Page 1745
Self-Review Exercises......Page 1746
Exercises......Page 1752
14 File Processing......Page 1763
Objectives......Page 1764
Outline......Page 1765
14.1 Introduction......Page 1767
14.2 Files and Streams......Page 1768
14.3 Creating a Sequential File......Page 1770
14.4 Reading Data from a Sequential File......Page 1779
14.4.1 Opening a File for Input......Page 1781
14.4.2 Reading from the File......Page 1782
14.4.3 File-Position Pointers......Page 1783
14.4.4 Case Study: Credit Inquiry Program......Page 1785
14.5 C++14: Reading and Writing Quoted Text......Page 1790
14.6 Updating Sequential Files......Page 1793
14.7 Random-Access Files......Page 1795
14.8 Creating a Random-Access File......Page 1797
14.8.1 Writing Bytes with ostream Member Function write......Page 1798
14.8.2 Converting Between Pointer Types with the reinterpret_cast Operator......Page 1800
14.8.3 Credit-Processing Program......Page 1803
14.8.4 Opening a File for Output in Binary Mode......Page 1807
14.9 Writing Data Randomly to a Random-Access File......Page 1809
14.10 Reading from a Random-Access File Sequentially......Page 1813
14.11 Case Study: A Transaction-Processing Program......Page 1817
14.12 Object Serialization......Page 1828
14.13 Wrap-Up......Page 1830
Summary......Page 1831
Section 14.1 Introduction......Page 1832
Section 14.2 Files and Streams......Page 1833
Section 14.3 Creating a Sequential File......Page 1835
Section 14.4 Reading Data from a Sequential File......Page 1837
Section 14.5 C++14: Reading and Writing Quoted Text......Page 1839
Section 14.6 Updating Sequential Files......Page 1840
Self-Review Exercises......Page 1847
Exercises......Page 1853
Making a Difference......Page 1862
15 Standard Library Containers and Iterators......Page 1864
Objectives......Page 1865
Outline......Page 1866
15.1 Introduction......Page 1868
15.2 Introduction to Containers2......Page 1873
15.3 Introduction to Iterators......Page 1883
15.4 Introduction to Algorithms......Page 1894
15.5 Sequence Containers......Page 1895
15.5.1 vector Sequence Container......Page 1898
Using vectors and Iterators......Page 1900
Creating a vector......Page 1902
vector Member Functions size and capacity......Page 1903
vector Member Function push_back......Page 1904
Updated size and capacity After Modifying a vector......Page 1905
vector Growth......Page 1906
Outputting Built-in Array Contents with Pointers......Page 1907
Outputting vector Contents with Iterators......Page 1909
Displaying the vector’s Contents in Reverse with const_reverse_iterators......Page 1911
C++11: shrink_to_fit......Page 1912
vector Element-Manipulation Functions......Page 1913
ostream_iterator......Page 1916
copy Algorithm......Page 1917
vector Member Functions front and back......Page 1918
Accessing vector Elements......Page 1919
vector Member Function insert......Page 1920
vector Member Function erase......Page 1921
vector Member Function insert with Three Arguments (Range insert)......Page 1922
vector Member Function clear......Page 1923
15.5.2 list Sequence Container......Page 1924
C++11: forward_list Container......Page 1925
list Member Functions......Page 1926
Creating list Objects......Page 1930
list Member Function sort......Page 1931
list Member Function splice......Page 1932
list Member Function merge......Page 1933
list Member Function pop_front......Page 1934
list Member Function unique......Page 1935
list Member Function swap......Page 1936
list Member Functions assign and remove......Page 1937
15.5.3 deque Sequence Container......Page 1938
15.6 Associative Containers......Page 1942
15.6.1 multiset Associative Container......Page 1944
15.6.2 set Associative Container......Page 1952
15.6.3 multimap Associative Container......Page 1955
15.6.4 map Associative Container......Page 1961
15.7 Container Adapters......Page 1964
15.7.1 stack Adapter......Page 1965
15.7.2 queue Adapter......Page 1969
15.7.3 priority_queue Adapter......Page 1971
15.8 Class bitset......Page 1974
15.9 Wrap-Up......Page 1981
Summary......Page 1982
Section 15.1 Introduction......Page 1983
Section 15.2 Introduction to Containers......Page 1984
Section 15.3 Introduction to Iterators......Page 1986
Section 15.4 Introduction to Algorithms......Page 1987
Section 15.5 Sequence Containers......Page 1988
Section 15.5.1 vector Sequence Container......Page 1989
Section 15.5.2 list Sequence Container......Page 1992
Section 15.5.3 deque Sequence Container......Page 1993
Section 15.6 Associative Containers......Page 1994
Section 15.6.1 multiset Associative Container......Page 1995
Section 15.6.2 set Associative Container......Page 1997
Section 15.6.3 multimap Associative Container......Page 1998
Section 15.6.4 map Associative Container......Page 1999
Section 15.7 Container Adapters......Page 2000
Section 15.7.1 stack Adapter......Page 2001
Section 15.7.2 queue Adapter......Page 2002
Section 15.7.3 priority_queue Adapter......Page 2003
Section 15.8 Class bitset......Page 2004
Self-Review Exercises......Page 2005
Exercises......Page 2010
Recommended Reading......Page 2017
16 Standard Library Algorithms......Page 2021
Objectives......Page 2022
Outline......Page 2023
16.1 Introduction......Page 2025
16.2 Minimum Iterator Requirements......Page 2027
16.3 Lambda Expressions......Page 2031
16.4 Algorithms Sections 16.4.1–16.4.12 demonstrate many of the Standard Library algorithms.......Page 2038
16.4.1 fill, fill_n, generate and generate_n......Page 2039
16.4.2 equal, mismatch and lexicographical_compare......Page 2045
16.4.3 remove, remove_if, remove_copy and remove_copy_if......Page 2052
16.4.4 replace, replace_if, replace_copy and replace_copy_if......Page 2057
16.4.5 Mathematical Algorithms......Page 2061
16.4.6 Basic Searching and Sorting Algorithms......Page 2069
16.4.7 swap, iter_swap and swap_ranges......Page 2078
16.4.8 copy_backward, merge, unique and reverse......Page 2081
16.4.9 inplace_merge, unique_copy and reverse_copy......Page 2088
16.4.10 Set Operations......Page 2091
16.4.11 lower_bound, upper_bound and equal_range......Page 2097
16.4.12 min, max, minmax and minmax_element......Page 2101
16.5 Function Objects......Page 2105
Advantages of Function Objects over Function Pointers......Page 2107
Predefined Function Objects of the Standard Template Library......Page 2108
Using the accumulate Algorithm......Page 2110
16.6 Standard Library Algorithm Summary......Page 2114
16.7 Wrap-Up......Page 2118
Summary......Page 2119
Section 16.1 Introduction......Page 2120
Section 16.3 Lambda Expressions......Page 2121
Section 16.3.1 Algorithm for_each......Page 2122
Section 16.3.2 Lambda with an Empty Introducer......Page 2123
Section 16.3.3 Lambda with a Nonempty Introducer—Capturing Local Variables......Page 2124
Section 16.3.4 Lambda Return Types......Page 2125
Section 16.4.1 fill, fill_n, generate and generate_n......Page 2126
Section 16.4.2 equal, mismatch and lexicographical_compare......Page 2127
Section 16.4.3 remove, remove_if, remove_copy and remove_copy_if......Page 2128
Section 16.4.4 replace, replace_if, replace_copy and replace_copy_if......Page 2129
Section 16.4.5 Mathematical Algorithms......Page 2130
Section 16.4.6 Basic Searching and Sorting Algorithms......Page 2131
Section 16.4.7 swap, iter_swap and swap_ranges......Page 2132
Section 16.4.8 copy_backward, merge, unique and reverse......Page 2133
Section 16.4.9 inplace_merge, unique_copy and reverse_copy......Page 2134
Section 16.4.10 Set Operations......Page 2135
Section 16.4.11 lower_bound, upper_bound and equal_range......Page 2136
Section 16.4.12 min, max, minmax and minmax_element......Page 2137
Section 16.5 Function Objects......Page 2138
Self-Review Exercises......Page 2139
Exercises......Page 2143
17 Exception Handling: A Deeper Look......Page 2150
Objectives......Page 2151
Outline......Page 2152
17.1 Introduction......Page 2154
17.2 Exception-Handling Flow of Control; Defining an Exception Class......Page 2157
17.2.4 Defining a catch Handler to Process a DivideByZeroException......Page 2164
17.3 Rethrowing an Exception......Page 2172
17.4 Stack Unwinding......Page 2175
17.5 When to Use Exception Handling......Page 2178
17.6 noexcept: Declaring Functions That Do Not Throw Exceptions......Page 2180
17.7 Constructors, Destructors and Exception Handling......Page 2181
17.8 Processing new Failures......Page 2185
17.9 Class unique_ptr and Dynamic Memory Allocation......Page 2191
17.10 Standard Library Exception Hierarchy......Page 2198
17.11 Wrap-Up......Page 2203
Summary......Page 2204
Section 17.1 Introduction......Page 2205
Section 17.2.1 Defining an Exception Class to Represent the Type of Problem That Might Occur......Page 2206
Section 17.2.5 Termination Model of Exception Handling......Page 2207
Section 17.2.7 Flow of Program Control When the User Enters a Denominator of Zero......Page 2208
Section 17.3 Rethrowing an Exception......Page 2210
Section 17.4 Stack Unwinding......Page 2211
Section 17.5 When to Use Exception Handling......Page 2212
Section 17.6 noexcept: Declaring Functions That Do Not Throw Exceptions......Page 2213
Section 17.7.1 Destructors Called Due to Exceptions......Page 2214
Section 17.7.2 Initializing Local Objects to Acquire Resources......Page 2215
Section 17.8 Processing new Failures......Page 2216
Section 17.9 Class unique_ptr and Dynamic Memory Allocation......Page 2217
Section 17.10 Standard Library Exception Hierarchy......Page 2218
Self-Review Exercises......Page 2219
Exercises......Page 2221
18 Introduction to Custom Templates......Page 2227
Objectives......Page 2228
Outline......Page 2229
18.1 Introduction......Page 2230
18.2 Class Templates......Page 2233
18.2.1 Creating Class Template Stack......Page 2235
18.2.2 Class Template Stack’s Data Representation......Page 2238
18.2.3 Class Template Stack’s Member Functions......Page 2239
18.2.4 Declaring a Class Template’s Member Functions Outside the Class Template Definition......Page 2240
18.2.5 Testing Class Template Stack......Page 2242
18.3 Function Template to Manipulate a Class-Template Specialization Object......Page 2246
18.4 Nontype Parameters......Page 2250
18.5 Default Arguments for Template Type Parameters......Page 2252
18.6 Overloading Function Templates......Page 2254
18.7 Wrap-Up......Page 2256
Summary......Page 2257
Self-Review Exercises......Page 2264
Exercises......Page 2266
19 Custom Templatized Data Structures......Page 2269
Objectives......Page 2270
Outline......Page 2271
19.1 Introduction......Page 2274
19.2 Self-Referential Classes......Page 2277
19.3 Linked Lists......Page 2280
19.3.1 Testing Our Linked List Implementation......Page 2284
19.3.2 Class Template ListNode......Page 2289
19.3.3 Class Template List......Page 2291
19.3.4 Member Function insertAtFront......Page 2295
19.3.5 Member Function insertAtBack......Page 2297
19.3.6 Member Function removeFromFront......Page 2299
19.3.7 Member Function removeFromBack......Page 2301
19.3.8 Member Function print......Page 2304
19.3.9 Circular Linked Lists and Double Linked Lists......Page 2306
19.4 Stacks......Page 2309
19.4.1 Taking Advantage of the Relationship Between Stack and List......Page 2311
19.4.2 Implementing a Class Template Stack Class Based By Inheriting from List......Page 2312
19.4.3 Dependent Names in Class Templates......Page 2314
19.4.4 Testing the Stack Class Template......Page 2315
19.4.5 Implementing a Class Template Stack Class With Composition of a List Object......Page 2318
19.5 Queues......Page 2320
19.6 Trees......Page 2327
19.6.1 Basic Terminology......Page 2328
19.6.2 Binary Search Trees......Page 2329
19.6.3 Testing the Tree Class Template......Page 2330
19.6.4 Class Template TreeNode......Page 2333
19.6.5 Class Template Tree......Page 2335
19.6.6 Tree Member Function insertNodeHelper......Page 2338
19.6.7 Tree Traversal Functions......Page 2339
19.6.8 Duplicate Elimination......Page 2342
19.6.9 Overview of the Binary Tree Exercises......Page 2343
19.7 Wrap-Up......Page 2344
Summary......Page 2345
Self-Review Exercises......Page 2354
Exercises......Page 2357
Special Section: Building Your Own Compiler......Page 2371
20 Searching and Sorting......Page 2375
Objectives......Page 2376
Outline......Page 2377
20.1 Introduction......Page 2378
20.2 Searching Algorithms......Page 2380
20.2.1 Linear Search......Page 2381
Function Template linearSearch......Page 2382
Big O: Constant Runtime......Page 2385
Big O: Linear Runtime......Page 2386
Big O: Quadratic Runtime......Page 2387
O(n2) Performance......Page 2388
Linear Search’s Runtime......Page 2389
20.2.2 Binary Search......Page 2390
Binary Search of 15 Integer Values......Page 2391
Binary Search Example......Page 2392
Function Template binarySearch......Page 2397
Function main......Page 2398
Efficiency of Binary Search......Page 2399
20.3 Sorting Algorithms......Page 2400
20.3.1 Insertion Sort......Page 2401
20.3.2 Selection Sort......Page 2406
20.3.3 Merge Sort (A Recursive Implementation)......Page 2411
Sample Merge......Page 2412
Recursive Implementation......Page 2413
Demonstrating Merge Sort......Page 2414
Function mergeSort......Page 2421
Function merge......Page 2422
Efficiency of Merge Sort......Page 2423
Summary of Searching and Sorting Algorithm Efficiencies......Page 2424
20.4 Wrap-Up......Page 2426
Summary......Page 2427
Self-Review Exercises......Page 2436
Exercises......Page 2437
21 Class string and String Stream Processing: A Deeper......Page 2445
Objectives......Page 2446
Outline......Page 2447
21.1 Introduction1......Page 2448
21.2 string Assignment and Concatenation......Page 2453
21.3 Comparing strings......Page 2457
21.4 Substrings......Page 2462
21.5 Swapping strings......Page 2463
21.6 string Characteristics......Page 2465
21.7 Finding Substrings and Characters in a string......Page 2470
21.8 Replacing Characters in a string......Page 2474
21.9 Inserting Characters into a string......Page 2478
21.10 Conversion to Pointer-Based char* Strings......Page 2481
21.11 Iterators......Page 2485
21.12 String Stream Processing......Page 2488
21.13 C++11 Numeric Conversion Functions......Page 2496
21.14 Wrap-Up......Page 2501
Summary......Page 2502
Self-Review Exercises......Page 2517
Exercises......Page 2519
Making a Difference......Page 2526
22 Bits, Characters, C Strings and structs......Page 2532
Objectives......Page 2533
Outline......Page 2534
22.1 Introduction......Page 2535
22.2 Structure Definitions......Page 2537
22.3 typedef and using......Page 2541
22.4 Example: Card Shuffling and Dealing Simulation......Page 2543
22.5 Bitwise Operators......Page 2547
22.6 Bit Fields......Page 2565
22.7 Character-Handling Library......Page 2573
22.8 C String-Manipulation Functions......Page 2582
22.9 C String-Conversion Functions......Page 2596
22.10 Search Functions of the C String-Handling Library......Page 2604
22.11 Memory Functions of the C String-Handling Library......Page 2612
22.12 Wrap-Up......Page 2619
Summary......Page 2620
Self-Review Exercises......Page 2635
Exercises......Page 2641
Special Section: Advanced String-Manipulation Exercises......Page 2654
Challenging String-Manipulation Projects......Page 2664
Chapters on the Web......Page 2667
A Operator Precedence and Associativity......Page 2668
B ASCII Character Set......Page 2672
C Fundamental Types......Page 2673
D Number Systems......Page 2676
Objectives......Page 2677
Outline......Page 2678
D.1 Introduction......Page 2679
D.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers......Page 2686
D.3 Converting Octal and Hexadecimal Numbers to Binary Numbers......Page 2690
D.4 Converting from Binary, Octal or Hexadecimal to Decimal......Page 2691
D.5 Converting from Decimal to Binary, Octal or Hexadecimal......Page 2693
D.6 Negative Binary Numbers: Two’s Complement Notation......Page 2696
Summary......Page 2700
Self-Review Exercises......Page 2702
Answers to Self-Review Exercises......Page 2704
Exercises......Page 2707
E Preprocessor......Page 2709
Objectives......Page 2710
Outline......Page 2711
E.1 Introduction......Page 2712
E.2 #include Preprocessing Directive......Page 2714
E.3 #define Preprocessing Directive: Symbolic Constants......Page 2716
E.4 #define Preprocessing Directive: Macros......Page 2719
E.5 Conditional Compilation......Page 2726
E.6 #error and #pragma Preprocessing Directives......Page 2729
E.7 Operators # and ##......Page 2731
E.8 Predefined Symbolic Constants......Page 2733
E.9 Assertions......Page 2735
E.10 Wrap-Up......Page 2737
Summary......Page 2738
Self-Review Exercises......Page 2747
Answers to Self-Review Exercises......Page 2749
Exercises......Page 2752
Appendices on the Web......Page 2754
Index......Page 2755
Symbols......Page 2756
Numerics......Page 2761
A......Page 2762
B......Page 2778
C......Page 2790
D......Page 2813
E......Page 2826
F......Page 2837
G......Page 2849
H......Page 2854
I......Page 2859
J......Page 2875
K......Page 2876
L......Page 2878
M......Page 2888
N......Page 2899
O......Page 2906
P......Page 2919
Q......Page 2937
R......Page 2939
S......Page 2954
T......Page 2988
U......Page 2999
V......Page 3005
W......Page 3009
X......Page 3013
Y......Page 3014
Z......Page 3015