HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
TOTAL LEARNING PACKAGE
MyProgrammingLab for C++ How to Program is a total learning package. MyProgrammingLab is an online homework, tutorial, and assessment program that truly engages students in learning. It helps students better prepare for class, quizzes, and exams–resulting in better performance in the course–and provides educators a dynamic set of tools for gauging individual and class progress. And, MyProgrammingLab comes from Pearson, your partner in providing the best digital learning experience.
Through the power of practice and immediate personalized feedback, MyProgrammingLab helps students fully grasp the logic, semantics, and syntax of programming. A self-study and homework tool, a MyProgrammingLab course consists of hundreds of small practice problems organized around the structure of this textbook. For students, the system automatically detects errors in the logic and syntax of their code submissions and offers targeted hints that enable them to figure out what went wrong–and why. For instructors, a comprehensive roster tracks correct and incorrect answers and stores the code inputted by students for review.
Note: If you are purchasing the standalone text or electronic version, MyProgrammingLab does not come automatically packaged with the text. To purchase MyProgrammingLab, please visit: myprogramminglab.com or you can purchase a package of the physical text + MyProgrammingLab by searching the Pearson Higher Education web site. MyProgrammingLab is not a self-paced technology and should only be purchased when required by an instructor.
Object-Oriented Programming
Pedagogic Features
Other Features
Teaching Approach
Online Content
An access code to the Companion Website is available free when packaged with a new text. Companion Website access can also be purchased separately online. The book’s Companion Website, which is accessible at www.pearsonhighered.com/deitel contains the following chapters and appendices in searchable PDF format:
The Companion Website also includes:
NOTE: You are purchasing a standalone product; MyProgrammingLab does not come packaged with this content. If you would like to purchase both the physical text and MyProgrammingLab search for ISBN-10: 0133450732/ISBN-13: 9780133450736 . That package includes ISBN-10: 0133146146/ISBN-13: 9780133146141 and ISBN-10: 0133378713/ISBN-13: 9780133378719.
MyProgrammingLab should only be purchased when required by an instructor
For Introduction to Programming (CS1) and other more intermediate courses covering programming in C++. Also appropriate as a supplement for upper-level courses where the instructor uses a book as a reference for the C++ language.
This best-selling comprehensive text is aimed at readers with little or no programming experience. It teaches programming by presenting the concepts in the context of full working programs and takes an early-objects approach. The authors emphasize achieving program clarity through structured and object-oriented programming, software reuse and component-oriented software construction. The Ninth Edition encourages students to connect computers to the community, using the Internet to solve problems and make a difference in our world. All content has been carefully fine-tuned in response to a team of distinguished academic and industry reviewers.
Chapters 24—26 and Appendices F—K are PDF documents posted online at the book’s Companion Website, which is accessible from www.pearsonhighered.com/deitel
Preface xxi
1 Introduction to Computers and C++ 1
1.1 Introduction 2
1.2 Computers and the Internet in Industry and Research 2
1.3 Hardware and Software 5
1.3.1 Moore’s Law 6
1.3.2 Computer Organization 6
1.4 Data Hierarchy 7
1.5 Machine Languages, Assembly Languages and High-Level Languages 9
1.6 C++ 10
1.7 Programming Languages 11
1.8 Introduction to Object Technology 14
1.9 Typical C++ Development Environment 17
1.10 Test-Driving a C++ Application 19
1.11 Operating Systems 25
1.11.1 Windows–A Proprietary Operating System 25
1.11.2 Linux–An Open-Source Operating System 26
1.11.3 Apple’s OS X; Apple’s iOS for iPhone®, iPad® and iPod Touch® Devices 26
1.11.4 Google’s Android 27
1.12 The Internet and World Wide Web 27
1.13 Some Key Software Development Terminology 29
1.14 C++11 and the Open Source Boost Libraries 31
1.15 Keeping Up to Date with Information Technologies 32
1.16 Web Resources 33
2 Introduction to C++ Programming; Input/Output and Operators 38
2.1 Introduction 39
2.2 First Program in C++: Printing a Line of Text 39
2.3 Modifying Our First C++ Program 43
2.4 Another C++ Program: Adding Integers 44
2.5 Memory Concepts 48
2.6 Arithmetic 49
2.7 Decision Making: Equality and Relational Operators 53
2.8 Wrap-Up 57
3 Introduction to Classes, Objects and Strings 66
3.1 Introduction 67
3.2 Defining a Class with a Member Function 67
3.3 Defining a Member Function with a Parameter 70
3.4 Data Members, set Member Functions and get Member Functions 74
3.5 Initializing Objects with Constructors 79
3.6 Placing a Class in a Separate File for Reusability 83
3.7 Separating Interface from Implementation 87
3.8 Validating Data with set Functions 92
3.9 Wrap-Up 97
4 Control Statements: Part 1; Assignment, ++ and -- Operators 104
4.1 Introduction 105
4.2 Algorithms 105
4.3 Pseudocode 106
4.4 Control Structures 107
4.5 if Selection Statement 110
4.6 if…else Double-Selection Statement 112
4.7 while Repetition Statement 116
4.8 Formulating Algorithms: Counter-Controlled Repetition 118
4.9 Formulating Algorithms: Sentinel-Controlled Repetition 124
4.10 Formulating Algorithms: Nested Control Statements 134
4.11 Assignment Operators 139
4.12 Increment and Decrement Operators 140
4.13 Wrap-Up 143
5 Control Statements: Part 2; Logical Operators 157
5.1 Introduction 158
5.2 Essentials of Counter-Controlled Repetition 158
5.3 for Repetition Statement 159
5.4 Examples Using the for Statement 163
5.5 do…while Repetition Statement 168
5.6 switch Multiple-Selection Statement 169
5.7 break and continue Statements 178
5.8 Logical Operators 180
5.9 Confusing the Equality (==) and Assignment (=) Operators 185
5.10 Structured Programming Summary 186
5.11 Wrap-Up 191
6 Functions and an Introduction to Recursion 201
6.1 Introduction 202
6.2 Program Components in C++ 203
6.3 Math Library Functions 204
6.4 Function Definitions with Multiple Parameters 205
6.5 Function Prototypes and Argument Coercion 210
6.6 C++ Standard Library Headers 212
6.7 Case Study: Random Number Generation 214
6.8 Case Study: Game of Chance; Introducing enum 219
6.9 C++11 Random Numbers 224
6.10 Storage Classes and Storage Duration 225
6.11 Scope Rules 228
6.12 Function Call Stack and Activation Records 231
6.13 Functions with Empty Parameter Lists 235
6.14 Inline Functions 236
6.15 References and Reference Parameters 237
6.16 Default Arguments 240
6.17 Unary Scope Resolution Operator 242
6.18 Function Overloading 243
6.19 Function Templates 246
6.20Recursion 248
6.21 Example Using Recursion: Fibonacci Series 252
6.22 Recursion vs. Iteration 255
6.23 Wrap-Up 258
7 Class Templates array and vector; Catching Exceptions 278
7.1 Introduction 279
7.2 arrays 279
7.3 Declaring arrays 281
7.4 Examples Using arrays 281
7.4.1 Declaring an array and Using a Loop to Initialize the array’s Elements 281
7.4.2 Initializing an array in a Declaration with an Initializer List 282
7.4.3 Specifying an array’s Size with a Constant Variable and Setting array Elements with Calculations 283
7.4.4 Summing the Elements of an array 286
7.4.5 Using Bar Charts to Display array Data Graphically 286
7.4.6 Using the Elements of an array as Counters 288
7.4.7 Using arrays to Summarize Survey Results 289
7.4.8 Static Local arrays and Automatic Local arrays 291
7.5 Range-Based for Statement 293
7.6 Case Study: Class GradeBook Using an array to Store Grades 295
7.7 Sorting and Searching arrays 302
7.8 Multidimensional arrays 304
7.9 Case Study: Class GradeBook Using a Two-Dimensional array 307
7.10 Introduction to C++ Standard Library Class Template vector 314
7.11 Wrap-Up 320
8 Pointers 334
8.1 Introduction 335
8.2 Pointer Variable Declarations and Initialization 335
8.3 Pointer Operators 337
8.4 Pass-by-Reference with Pointers 339
8.5 Built-In Arrays 344
8.6 Using const with Pointers 346
8.6.1 Nonconstant Pointer to Nonconstant Data 347
8.6.2 Nonconstant Pointer to Constant Data 347
8.6.3 Constant Pointer to Nonconstant Data 348
8.6.4 Constant Pointer to Constant Data 349
8.7 sizeof Operator 350
8.8 Pointer Expressions and Pointer Arithmetic 353
8.9 Relationship Between Pointers and Built-In Arrays 355
8.10 Pointer-Based Strings 358
8.11 Wrap-Up 361
9 Classes: A Deeper Look; Throwing Exceptions 377
9.1 Introduction 378
9.2 Time Class Case Study 379
9.3 Class Scope and Accessing Class Members 385
9.4 Access Functions and Utility Functions 386
9.5 Time Class Case Study: Constructors with Default Arguments 387
9.6 Destructors 393
9.7 When Constructors and Destructors Are Called 393
9.8 Time Class Case Study: A Subtle Trap–Returning a Reference or a Pointer to a private Data Member 397
9.9 Default Memberwise Assignment 400
9.10 const Objects and const Member Functions 402
9.11 Composition: Objects as Members of Classes 404
9.12 friend Functions and friend Classes 410
9.13 Using the this Pointer 412
9.14 static Class Members 418
9.15 Wrap-Up 423
10 Operator Overloading; Class string 433
10.1 Introduction 434
10.2 Using the Overloaded Operators of Standard Library Class string 435
10.3 Fundamentals of Operator Overloading 438
10.4 Overloading Binary Operators 439
10.5 Overloading the Binary Stream Insertion and Stream Extraction Operators 440
10.6 Overloading Unary Operators 444
10.7 Overloading the Unary Prefix and Postfix ++ and -- Operators 445
10.8 Case Study: A Date Class 446
10.9 Dynamic Memory Management 451
10.10 Case Study: Array Class 453
10.10.1 Using the Array Class 454
10.10.2 Array Class Definition 458
10.11 Operators as Member vs. Non-Member Functions 466
10.12 Converting Between Types 466
10.13 explicit Constructors and Conversion Operators 468
10.14 Overloading the Function Call Operator () 470
10.15 Wrap-Up 471
11 Object-Oriented Programming: Inheritance 482
11.1 Introduction 483
11.2 Base Classes and Derived Classes 483
11.3 Relationship between Base and Derived Classes 486
11.3.1 Creating and Using a CommissionEmployee Class 486
11.3.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance 491
11.3.3 Creating a CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy 497
11.3.4 CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data 501
11.3.5 CommissionEmployee—BasePlusCommissionEmployee
Inheritance Hierarchy Using private Data 504
11.4 Constructors and Destructors in Derived Classes 509
11.5 public, protected and private Inheritance 511
11.6 Software Engineering with Inheritance 512
11.7 Wrap-Up 512
12 Object-Oriented Programming: Polymorphism 517
12.1 Introduction 518
12.2 Introduction to Polymorphism: Polymorphic Video Game 519
12.3 Relationships Among Objects in an Inheritance Hierarchy 519
12.3.1 Invoking Base-Class Functions from Derived-Class Objects 520
12.3.2 Aiming Derived-Class Pointers at Base-Class Objects 523
12.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 524
12.3.4 Virtual Functions and Virtual Destructors 526
12.4 Type Fields and switch Statements 533
12.5 Abstract Classes and Pure virtual Functions 533
12.6 Case Study: Payroll System Using Polymorphism 535
12.6.1 Creating Abstract Base Class Employee 536
12.6.2 Creating Concrete Derived Class SalariedEmployee 540
12.6.3 Creating Concrete Derived Class CommissionEmployee 542
12.6.4 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 544
12.6.5 Demonstrating Polymorphic Processing 546
12.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood” 550
12.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info 553
12.9 Wrap-Up 557
13 Stream Input/Output: A Deeper Look 562
13.1 Introduction 563
13.2 Streams 564
13.2.1 Classic Streams vs. Standard Streams 564
13.2.2 iostream Library Headers 565
13.2.3 Stream Input/Output Classes and Objects 565
13.3 Stream Output 567
13.3.1 Output of char * Variables 568
13.3.2 Character Output Using Member Function put 568
13.4 Stream Input 569
13.4.1 get and getline Member Functions 569
13.4.2 istream Member Functions peek, putback and ignore 572
13.4.3 Type-Safe I/O 572
13.5 Unformatted I/O Using read, write and gcount 572
13.6 Introduction to Stream Manipulators 573
13.6.1 Integral Stream Base: dec, oct, hex and setbase 574
13.6.2 Floating-Point Precision (precision, setprecision) 574
13.6.3 Field Width (width, setw) 576
13.6.4 User-Defined Output Stream Manipulators 577
13.7 Stream Format States and Stream Manipulators 578
13.7.1 Trailing Zeros and Decimal Points (showpoint) 579
13.7.2 Justification (left, right and internal) 580
13.7.3 Padding (fill, setfill) 582
13.7.4 Integral Stream Base (dec, oct, hex, showbase) 583
13.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) 584
13.7.6 Uppercase/Lowercase Control (uppercase) 585
13.7.7 Specifying Boolean Format (boolalpha) 585
13.7.8 Setting and Resetting the Format State via Member Function flags 586
13.8 Stream Error States 587
13.9 Tying an Output Stream to an Input Stream 590
13.10 Wrap-Up 590
14 File Processing 599
14.1 Introduction 600
14.2 Files and Streams 600
14.3 Creating a Sequential File 601
14.4 Reading Data from a Sequential File 605
14.5 Updating Sequential Files 611
14.6 Random-Access Files 611
14.7 Creating a Random-Access File 612
14.8 Writing Data Randomly to a Random-Access File 617
14.9 Reading from a Random-Access File Sequentially 619
14.10 Case Study: A Transaction-Processing Program 621
14.11 Object Serialization 628
14.12 Wrap-Up 628
15 Standard Library Containers and Iterators 638
15.1 Introduction 639
15.2 Introduction to Containers 640
15.3 Introduction to Iterators 644
15.4 Introduction to Algorithms 649
15.5 Sequence Containers 649
15.5.1 vector Sequence Container 650
15.5.2 list Sequence Container 658
15.5.3 deque Sequence Container 662
15.6 Associative Containers 664
15.6.1 multiset Associative Container 665
15.6.2 set Associative Container 668
15.6.3 multimap Associative Container 669
15.6.4 map Associative Container 671
15.7 Container Adapters 673
15.7.1 stack Adapter 673
15.7.2 queue Adapter 675
15.7.3 priority_queue Adapter 676
15.8 Class bitset 677
15.9 Wrap-Up 679
16 Standard Library Algorithms 690
16.1 Introduction 691
16.2 Minimum Iterator Requirements 691
16.3 Algorithms 693
16.3.1 fill, fill_n, generate and generate_n 693
16.3.2 equal, mismatch and lexicographical_compare 695
16.3.3 remove, remove_if, remove_copy and remove_copy_if 697
16.3.4 replace, replace_if, replace_copy and replace_copy_if 700
16.3.5 Mathematical Algorithms 702
16.3.6 Basic Searching and Sorting Algorithms 706
16.3.7 swap, iter_swap and swap_ranges 710
16.3.8 copy_backward, merge, unique and reverse 711
16.3.9 inplace_merge, unique_copy and reverse_copy 714
16.3.10 Set Operations 716
16.3.11 lower_bound, upper_bound and equal_range 719
16.3.12Heapsort 721
16.3.13 min, max, minmax and minmax_element 724
16.4 Function Objects 726
16.5 Lambda Expressions 729
16.6 Standard Library Algorithm Summary 730
16.7 Wrap-Up 732
17 Exception Handling: A Deeper Look 740
17.1 Introduction 741
17.2 Example: Handling an Attempt to Divide by Zero 741
17.3 Rethrowing an Exception 747
17.4 Stack Unwinding 748
17.5 When to Use Exception Handling 750
17.6 Constructors, Destructors and Exception Handling 751
17.7 Exceptions and Inheritance 752
17.8 Processing new Failures 752
17.9 Class unique_ptr and Dynamic Memory Allocation 755
17.10 Standard Library Exception Hierarchy 758
17.11 Wrap-Up 759
18 Introduction to Custom Templates 765
18.1 Introduction 766
18.2 Class Templates 766
18.3 Function Template to Manipulate a Class-Template Specialization Object 771
18.4 Nontype Parameters 773
18.5 Default Arguments for Template Type Parameters 773
18.6 Overloading Function Templates 774
18.7 Wrap-Up 774
19 Custom Templatized Data Structures 777
19.1 Introduction 778
19.2 Self-Referential Classes 779
19.3 Linked Lists 780
19.4Stacks 794
19.5Queues 799
19.6Trees 803
19.7 Wrap-Up 811
20 Searching and Sorting 822
20.1 Introduction 823
20.2 Searching Algorithms 824
20.2.1 Linear Search 824
20.2.2 Binary Search 827
20.3 Sorting Algorithms 831
20.3.1 Insertion Sort 832
20.3.2 Selection Sort 834
20.3.3 Merge Sort (A Recursive Implementation) 837
20.4 Wrap-Up 843
21 Class string and String Stream Processing: A Deeper Look 849
21.1 Introduction 850
21.2 string Assignment and Concatenation 851
21.3 Comparing strings 853
21.4 Substrings 856
21.5 Swapping strings 856
21.6 string Characteristics 857
21.7 Finding Substrings and Characters in a string 859
21.8 Replacing Characters in a string 861
21.9 Inserting Characters into a string 863
21.10 Conversion to Pointer-Based char * Strings 864
21.11 Iterators 865
21.12 String Stream Processing 867
21.13 C++11 Numeric Conversion Functions 870
21.14 Wrap-Up 871
22 Bits, Characters, C Strings and structs 879
22.1 Introduction 880
22.2 Structure Definitions 880
22.3 typedef 882
22.4 Example: Card Shuffling and Dealing Simulation 882
22.5 Bitwise Operators 885
22.6 Bit Fields 894
22.7 Character-Handling Library 897
22.8 C String-Manipulation Functions 903
22.9 C String-Conversion Functions 910
22.10 Search Functions of the C String-Handling Library 915
22.11 Memory Functions of the C String-Handling Library 919
22.12 Wrap-Up 923
23 Other Topics 938
23.1 Introduction 939
23.2 const_cast Operator 939
23.3 mutable Class Members 941
23.4 namespaces 943
23.5 Operator Keywords 946
23.6 Pointers to Class Members (.* and ->*) 948
23.7 Multiple Inheritance 950
23.8 Multiple Inheritance and virtual Base Classes 955
23.9 Wrap-Up 959
List of Chapters on the Web 965
A Operator Precedence and Associativity 967
B ASCII Character Set 969
C Fundamental Types 970
D Number Systems 972
D.1 Introduction 973
D.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers 976
D.3 Converting Octal and Hexadecimal Numbers to Binary Numbers 977
D.4 Converting from Binary, Octal or Hexadecimal to Decimal 977
D.5 Converting from Decimal to Binary, Octal or Hexadecimal 978
D.6 Negative Binary Numbers: Two’s Complement Notation 980
E Preprocessor 985
E.1 Introduction 986
E.2 #include Preprocessing Directive 986
E.3 #define Preprocessing Directive: Symbolic Constants 987
E.4 #define Preprocessing Directive: Macros 987
E.5 Conditional Compilation 989
E.6 #error and #pragma Preprocessing Directives 990
E.7 Operators # and ## 991
E.8 Predefined Symbolic Constants 991
E.9 Assertions 992
E.10Wrap-Up 992
List of Appendices on the Web 997
Index 999
Online Chapters and Appendices
Chapters 24—26 and Appendices F—K are PDF documents posted online at the book’s Companion Website, which is accessible from www.pearsonhighered.com/deitel
24 C++11 Additional Features 24-1
25 ATM Case Study, Part 1: Object-Oriented Design with the UML 25-1
25.1 Introduction 25-2
25.2 Introduction to Object-Oriented Analysis and Design 25-2
25.3 Examining the ATM Requirements Document 25-3
25.4 Identifying the Classes in the ATM Requirements Document 25-10
25.5 Identifying Class Attributes 25-17
25.6 Identifying Objects’ States and Activities 25-21
25.7 Identifying Class Operations 25-25
25.8 Indicating Collaboration Among Objects 25-32
25.9 Wrap-Up 25-39
26 ATM Case Study, Part 2: Implementing an Object-Oriented Design 26-1
26.1 Introduction 26-2
26.2 Starting to Program the Classes of the ATM System 26-2
26.3 Incorporating Inheritance into the ATM System 26-8
26.4 ATM Case Study Implementation 26-15
26.4.1 Class ATM 26-16
26.4.2 Class Screen 26-23
26.4.3 Class Keypad 26-25
26.4.4 Class CashDispenser 26-26
26.4.5 Class DepositSlot 26-28
26.4.6 Class Account 26-29
26.4.7 Class BankDatabase 26-31
26.4.8 Class Transaction 26-35
26.4.9 Class BalanceInquiry 26-37
26.4.10 Class Withdrawal 26-39
26.4.11 Class Deposit 26-44
26.4.12 Test Program ATMCaseStudy.cpp 26-47
26.5 Wrap-Up 26-47
F C Legacy Code Topics F-1
F.1 Introduction F-2
F.2 Redirecting Input/Output on UNIX/Linux/Mac OS X and Windows Systems F-2
F.3 Variable-Length Argument Lists F-3
F.4 Using Command-Line Arguments F-5
F.5 Notes on Compiling Multiple-Source-File Programs F-7
F.6 Program Termination with exit and atexit F-9
F.7 Type Qualifier volatile F-10
F.8 Suffixes for Integer and Floating-Point Constants F-10
F.9 Signal Handling F-11
F.10 Dynamic Memory Allocation with calloc and realloc F-13
F.11 Unconditional Branch: goto F-14
F.12 Unions F-15
F.13 Linkage Specifications F-18
F.14 Wrap-Up F-19
G UML 2: Additional Diagram Types G-1
G.1 Introduction G-1
G.2 Additional Diagram Types G-2
H Using the Visual Studio Debugger H-1
H.1 Introduction H-2
H.2 Breakpoints and the Continue Command H-2
H.3 Locals and Watch Windows H-8
H.4 Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands H-11
H.5 Autos Window H-13
H.6 Wrap-Up H-14
I Using the GNU C++ Debugger I-1
I.1 Introduction I-2
I.2 Breakpoints and the run, stop, continue and print Commands I-2
I.3 print and set Commands I-8
I.4 Controlling Execution Using the step, finish and next Commands I-10
I.5 watch Command I-13
I.6 Wrap-Up I-15
J Using the Xcode Debugger J-1
K Test Driving a C++ Program on Mac OS X K-1 [Note: The test drives for Windows and Linux are in Chapter 1.]