Tải bản đầy đủ (.pdf) (1,105 trang)

C++ How to Program, 7th Edition pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.41 MB, 1,105 trang )

Deitel® Ser
How To Program Series
Java How to Program, 8/E
Java How to Program, Late Objects Version, 8/E
C++ How to Program, 7/E
C How to Program, 6/E
Internet & World Wide Web How to Program, 4/E
Visual Basic
®
2008 How to Program
Visual C#
®
2008 How to Program, 3/E
Visual C++
®
2008 How to Program, 2/E
Small Java

How to Program, 6/E
Small C++ How to Program, 5/E
Simply Series
Simply C++: An Application-Driven
Tu t orial Approach
Simply Java

Programming: An
Application-Driven Tutorial
Approach
Simply C#: An Application-Driven


Tutorial Approach
Simply Visual Basic
®
2008, 3/E: An
Application-Driven Tutorial
Approach
CourseSmart Web Books
www.deitel.com/books/CourseSmart/
C++ How to Program, 5/E & 6/E
Java How to Program, 6/E, 7/E & 8/E
Simply C++: An Application-Driven
Tu t orial Ap p r o a c h
Simply Visual Basic 2008: An
Application-Driven Tutorial
Approach, 3/E
Small C++ How to Program, 5/E
Small Java How to Program, 6/E
Visual Basic 2008 How to Program
Visual C# 2008 How to Program, 3/E
ies Page
Deitel
®
Developer Series
AJAX, Rich Internet Applications and
Web Development for Programmers
C++ for Programmers
C# 2008 for Programmers, 3/E
iPhone for Programmers
Java for Programmers
Javascript for Programmers

LiveLessons Video Learning Products
www.deitel.com/books/LiveLessons/
Java Fundamentals Parts 1 and 2
C# Fundamentals Parts 1 and 2
C++ Fundamentals Parts 1 and 2
JavaScript Fundamentals Parts 1 and 2
To receive updates on Deitel publications, Resource Centers, training courses, partner
offers and more, please register for the free Deitel
®
Buzz Online e-mail newsletter at:
www.deitel.com/newsletter/subscribe.html
follow us on Twitter
®
@deitel
and become a Deitel & Associates fan on Facebook
®
www.deitel.com/deitelfan/
To communicate with the authors, send e-mail to:

For information on government and corporate Dive-Into
®
Series on-site seminars
offered by Deitel & Associates, Inc. worldwide, visit:
www.deitel.com/training/
or write to

For continuing updates on Prentice Hall/Deitel publications visit:
www.deitel.com
www.pearsonhighered.com/deitel


Check out our Resource Centers for valuable web resources that will help you master
Java, other important programming languages, software and Internet- and web-related
topics:
www.deitel.com/ResourceCenters.html
Library of Congress Cataloging-in-Publication Data
On file
Vice President and Editorial Director, ECS: Marcia J. Horton
Editor-in-Chief, Computer Science: Michael Hirsch
Associate Editor: Carole Snyder
Supervisor/Editorial Assistant: Dolores Mars
Director of Team-Based Project Management: Vince O’Brien
Senior Managing Editor: Scott Disanno
Managing Editor: Robert Engelhardt
A/V Production Editor: Greg Dulles
Art Director: Kristine Carney
Cover Design: Abbey S. Deitel, Harvey M. Deitel, Francesco Santalucia, Kristine Carney
Interior Design: Harvey M. Deitel, Kristine Carney
Manufacturing Manager: Alexis Heydt-Long
Manufacturing Buyer: Lisa McDowell
Director of Marketing: Margaret Waples
Marketing Manager: Erin Davis
© 2010 by Pearson Education, Inc.
Upper Saddle River, New Jersey 07458
The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the
development, research, and testing of the theories and programs to determine their effectiveness. The authors and pub-
lisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation
contained in this book. The authors and publisher shall not be liable in any event for incidental or consequential dam-
ages in connection with, or arising out of, the furnishing, performance, or use of these programs.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks
and registered trademarks. Where those designations appear in this book, and Prentice Hall and the authors were aware

of a trademark claim, the designations have been printed in initial caps or all caps. All product names mentioned
remain trademarks or registered trademarks of their respective owners.
All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in
writing from the publisher.
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
ISBN-10: 0-13-611726-0
ISBN-13: 978-0-13-611726-1
Pearson Education Ltd., London
Pearson Education Australia Pty. Ltd., Sydney
Pearson Education Singapore, Pte. Ltd.
Pearson Education North Asia Ltd., Hong Kong
Pearson Education Canada, Inc., To ron t o
Pearson Educación de Mexico, S.A. de C.V.
Pearson Education–Japan, To kyo
Pearson Education Malaysia, Pte. Ltd.
Pearson Education, Inc., Upper Saddle River, New Jersey
Trademarks
DEITEL, the double-thumbs-up bug and DIVE INTO are registered trademarks of Deitel and Associates,
Inc.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the
United States and other countries. Pearson Education is independent of Sun Microsystems, Inc.
Microsoft, Internet Explorer and the Windows logo are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
Pong
®
classic video game courtesy of Atari Interactive, Inc. ©2009 Atari Interactive, Inc. All rights
reserved. Used with permission.
UNIX is a registered trademark of The Open Group.

In memory of Joseph Weizenbaum
MIT Professor Emeritus of Computer Science:
For making us think.
Paul and Harvey Deitel
Deitel Resource Centers
Our Resource Centers focus on the vast amounts of free content available online. Find resources,
downloads, tutorials, documentation, books, e-books, journals, articles, blogs, RSS feeds and more
on many of today’s hottest programming and technology topics. For the most up-to-date list of our
Resource Centers, visit:
www.deitel.com/ResourceCenters.html
Let us know what other Resource Centers you’d like to see! Also, please register for the free Deitel
®
Buzz Online e-mail newsletter at:
www.deitel.com/newsletter/subscribe.html
Computer Science
Functional Programming
Regular Expressions
Programming
Apple iPhone
ASP.NET 3.5
Adobe Flex
Ajax
Apex
ASP.NET Ajax
ASP.NET
C
C++
C++ Boost Libraries
C++ Game Programming
C#

Code Search Engines and
Code Sites
Computer Game
Programming
CSS 2.1
Dojo
Facebook Developer Plat-
form
Flash 9
Functional Programming
Java
Java Certification and
Assessment Testing
Java Design Patterns
Java EE 5
Java SE 6
Java SE 7 (Dolphin)
Resource Center
JavaFX
JavaScript
JSON
Microsoft LINQ
Microsoft Popfly
.NET
.NET 3.0
.NET 3.5
OpenGL
Perl
PHP
Programming Projects

Python
Regular Expressions
Ruby
Ruby on Rails
Silverlight
UML
Visual Basic
Visual C++
Visual Studio Team System
Web 3D Technologies
Web Services
Windows Presentation
Foundation
XHTML
XML
Games and Game
Programming
Computer Game Pro-
gramming
Computer Games
Mobile Gaming
Sudoku
Internet Business
Affiliate Programs
Competitive Analysis
Facebook Social Ads
Google AdSense
Google Analytics
Google Services
Internet Advertising

Internet Business
Initiative
Internet Public Relations
Link Building
Location-Based Services
Online Lead Generation
Podcasting
Search Engine Optimiza-
tion
Selling Digital Content
Sitemaps
Web Analytics
Website Monetization
YouTube and AdSense
Java
Java
Java Certification and
Assessment Testing
Java Design Patterns
Java EE 5
Java SE 6
Java SE 7 (Dolphin)
Resource Center
JavaFX
Microsoft
ASP.NET
ASP.NET 3.5
ASP.NET Ajax
C#
DotNetNuke (DNN)

Internet Explorer 7 (IE7)
Microsoft LINQ
.NET
.NET 3.0
.NET 3.5
SharePoint
Silverlight
Visual Basic
Visual C++
Visual Studio Team
System
Windows Presentation
Foundation
Windows Vista
Microsoft Popfly
Open Source and
LAMP Stack
Apache
DotNetNuke (DNN)
Eclipse
Firefox
Linux
MySQL
Open Source
Perl
PHP
Python
Ruby
Software
Apache

DotNetNuke (DNN)
Eclipse
Firefox
Internet Explorer 7 (IE7)
Linux
MySQL
Open Source
Search Engines
SharePoint
Skype
Web Servers
Wikis
Windows Vista
Web 2.0
Alert Services
Attention Economy
Blogging
Building Web
Communities
Community Generated
Content
Facebook Developer
Platform
Facebook Social Ads
Google Base
Google Video
Google Web Toolkit
(GWT)
Internet Video
Joost

Location-Based Services
Mashups
Microformats
Recommender Systems
RSS
Social Graph
Social Media
Social Networking
Software as a Service
(SaaS)
Virtual Worlds
Web 2.0
Web 3.0
Widgets
Dive Into
®
Web 2.0
eBook
Web 2 eBook
Other Topics
Computer Games
Computing Jobs
Gadgets and Gizmos
Ring Tones
Sudoku
Chapters 23–27 and Appendices F–I are PDF documents posted online at the book’s
Companion Website (located at
www.pearsonhighered.com/deitel).
Preface xxiii
1 Introduction to Computers, the Internet and

the World Wide Web 1
1.1 Introduction 2
1.2 Computers: Hardware and Software 3
1.3 Computer Organization 4
1.4 Personal, Distributed and Client/Server Computing 5
1.5 The Internet and the World Wide Web 6
1.6 Web 2.0 6
1.7 Machine Languages, Assembly Languages
and High-Level Languages 7
1.8 History of C and C++ 8
1.9 C++ Standard Library 9
1.10 History of Java 10
1.11 Fortran, COBOL, Pascal and Ada 11
1.12 BASIC, Visual Basic, Visual C++, C# and .NET 11
1.13 Key Software Trend: Object Technology 12
1.14 Typical C++ Development Environment 13
1.15 Notes About C++ and C++ How to Program, 7/e 15
1.16 Test-Driving a C++ Application 16
1.17 Software Technologies 22
1.18 Future of C++: Open Source Boost Libraries, TR1 and C++0x 23
1.19 Software Engineering Case Study: Introduction to Object Technology
and the UML 24
1.20 Wrap-Up 28
1.21 Web Resources 29
2 Introduction to C++ Programming 39
2.1 Introduction 40
2.2 First Program in C++: Printing a Line of Text 40
2.3 Modifying Our First C++ Program 44
2.4 Another C++ Program: Adding Integers 45
Contents

x Contents
2.5 Memory Concepts 49
2.6 Arithmetic 50
2.7 Decision Making: Equality and Relational Operators 54
2.8 Wrap-Up 58
3 Introduction to Classes and Objects 68
3.1 Introduction 69
3.2 Classes, Objects, Member Functions and Data Members 69
3.3 Defining a Class with a Member Function 71
3.4 Defining a Member Function with a Parameter 74
3.5 Data Members, set Functions and get Functions 77
3.6 Initializing Objects with Constructors 84
3.7 Placing a Class in a Separate File for Reusability 87
3.8 Separating Interface from Implementation 91
3.9 Validating Data with set Functions 97
3.10 Wrap-Up 102
4 Control Statements: Part 1 109
4.1 Introduction 110
4.2 Algorithms 110
4.3 Pseudocode 111
4.4 Control Structures 112
4.5
if Selection Statement 115
4.6
if…else Double-Selection Statement 117
4.7
while Repetition Statement 122
4.8 Formulating Algorithms: Counter-Controlled Repetition 123
4.9 Formulating Algorithms: Sentinel-Controlled Repetition 129
4.10 Formulating Algorithms: Nested Control Statements 139

4.11 Assignment Operators 144
4.12 Increment and Decrement Operators 144
4.13 Wrap-Up 148
5 Control Statements: Part 2 163
5.1 Introduction 164
5.2 Essentials of Counter-Controlled Repetition 164
5.3
for Repetition Statement 166
5.4 Examples Using the
for Statement 170
5.5
do…while Repetition Statement 174
5.6
switch Multiple-Selection Statement 176
5.7
break and continue Statements 185
5.8 Logical Operators 187
5.9 Confusing the Equality (
==) and Assignment (=) Operators 191
5.10 Structured Programming Summary 192
5.11 Wrap-Up 197
Contents xi
6 Functions and an Introduction to Recursion 207
6.1 Introduction 208
6.2 Program Components in C++ 209
6.3 Math Library Functions 210
6.4 Function Definitions with Multiple Parameters 211
6.5 Function Prototypes and Argument Coercion 216
6.6 C++ Standard Library Header Files 218
6.7 Case Study: Random Number Generation 220

6.8 Case Study: Game of Chance; Introducing
enum 225
6.9 Storage Classes 229
6.10 Scope Rules 231
6.11 Function Call Stack and Activation Records 235
6.12 Functions with Empty Parameter Lists 238
6.13 Inline Functions 239
6.14 References and Reference Parameters 241
6.15 Default Arguments 245
6.16 Unary Scope Resolution Operator 247
6.17 Function Overloading 248
6.18 Function Templates 251
6.19 Recursion 253
6.20 Example Using Recursion: Fibonacci Series 256
6.21 Recursion vs. Iteration 259
6.22 Wrap-Up 262
7 Arrays and Vectors 282
7.1 Introduction 283
7.2 Arrays 284
7.3 Declaring Arrays 285
7.4 Examples Using Arrays 286
7.4.1 Declaring an Array and Using a Loop to Initialize the
Array’s Elements 286
7.4.2 Initializing an Array in a Declaration with an Initializer List 287
7.4.3 Specifying an Array’s Size with a Constant Variable and
Setting Array Elements with Calculations 288
7.4.4 Summing the Elements of an Array 291
7.4.5 Using Bar Charts to Display Array Data Graphically 291
7.4.6 Using the Elements of an Array as Counters 293
7.4.7 Using Arrays to Summarize Survey Results 294

7.4.8 Static Local Arrays and Automatic Local Arrays 297
7.5 Passing Arrays to Functions 299
7.6 Case Study: Class
GradeBook Using an Array to Store Grades 303
7.7 Searching Arrays with Linear Search 309
7.8 Sorting Arrays with Insertion Sort 311
7.9 Multidimensional Arrays 313
7.10 Case Study: Class
GradeBook Using a Two-Dimensional Array 316
xii Contents
7.11 Introduction to C++ Standard Library Class Template
vector 323
7.12 Wrap-Up 328
8 Pointers 345
8.1 Introduction 346
8.2 Pointer Variable Declarations and Initialization 346
8.3 Pointer Operators 348
8.4 Pass-by-Reference with Pointers 350
8.5 Using
const with Pointers 354
8.6 Selection Sort Using Pass-by-Reference 358
8.7 sizeof Operator 362
8.8 Pointer Expressions and Pointer Arithmetic 365
8.9 Relationship Between Pointers and Arrays 367
8.10 Pointer-Based String Processing 370
8.11 Arrays of Pointers 373
8.12 Function Pointers 374
8.13 Wrap-Up 377
9 Classes: A Deeper Look, Part 1 395
9.1 Introduction 396

9.2 Time Class Case Study 397
9.3 Class Scope and Accessing Class Members 403
9.4 Separating Interface from Implementation 405
9.5 Access Functions and Utility Functions 406
9.6
Time Class Case Study: Constructors with Default Arguments 409
9.7 Destructors 414
9.8 When Constructors and Destructors Are Called 415
9.9
Time Class Case Study: A Subtle Trap—Returning a Reference to a
private Data Member 418
9.10 Default Memberwise Assignment 421
9.11 Wrap-Up 423
10 Classes: A Deeper Look, Part 2 429
10.1 Introduction 430
10.2 const (Constant) Objects and const Member Functions 430
10.3 Composition: Objects as Members of Classes 439
10.4
friend Functions and friend Classes 445
10.5 Using the this Pointer 448
10.6
static Class Members 453
10.7 Data Abstraction and Information Hiding 458
10.8 Wrap-Up 460
11 Operator Overloading 466
11.1 Introduction 467
Contents xiii
11.2 Fundamentals of Operator Overloading 468
11.3 Restrictions on Operator Overloading 469
11.4 Operator Functions as Class Members vs. Global Functions 470

11.5 Overloading Stream Insertion and Stream Extraction Operators 472
11.6 Overloading Unary Operators 475
11.7 Overloading Binary Operators 476
11.8 Dynamic Memory Management 476
11.9 Case Study:
Array Class 478
11.10 Converting between Types 490
11.11 Building a
String Class 491
11.12 Overloading
++ and 492
11.13 Case Study: A
Date Class 494
11.14 Standard Library Class
string 498
11.15
explicit Constructors 502
11.16 Proxy Classes 505
11.17 Wrap-Up 509
12 Object-Oriented Programming: Inheritance 521
12.1 Introduction 522
12.2 Base Classes and Derived Classes 523
12.3
protected Members 526
12.4 Relationship between Base Classes and Derived Classes 526
12.4.1 Creating and Using a
CommissionEmployee Class 527
12.4.2 Creating a
BasePlusCommissionEmployee Class Without
Using Inheritance 532

12.4.3 Creating a
CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy 537
12.4.4 CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy Using
protected Data 542
12.4.5
CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy Using
private Data 549
12.5 Constructors and Destructors in Derived Classes 556
12.6
public, protected and private Inheritance 564
12.7 Software Engineering with Inheritance 565
12.8 Wrap-Up 566
13 Object-Oriented Programming: Polymorphism 572
13.1 Introduction 573
13.2 Polymorphism Examples 574
13.3 Relationships Among Objects in an Inheritance Hierarchy 575
13.3.1 Invoking Base-Class Functions from Derived-Class Objects 576
13.3.2 Aiming Derived-Class Pointers at Base-Class Objects 583
13.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 584
13.3.4 Virtual Functions 586
xiv Contents
13.3.5 Summary of the Allowed Assignments Between Base-Class
and Derived-Class Objects and Pointers 592
13.4 Type Fields and
switch Statements 593
13.5 Abstract Classes and Pure
virtual Functions 593

13.6 Case Study: Payroll System Using Polymorphism 595
13.6.1 Creating Abstract Base Class
Employee 597
13.6.2 Creating Concrete Derived Class
SalariedEmployee 600
13.6.3 Creating Concrete Derived Class
HourlyEmployee 602
13.6.4 Creating Concrete Derived Class
CommissionEmployee 605
13.6.5 Creating Indirect Concrete Derived Class
BasePlusCommissionEmployee 607
13.6.6 Demonstrating Polymorphic Processing 608
13.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding
“Under the Hood” 612
13.8 Case Study: Payroll System Using Polymorphism and Runtime Type
Information with Downcasting,
dynamic_cast, typeid and type_info 616
13.9 Virtual Destructors 620
13.10 Wrap-Up 620
14 Templates 626
14.1 Introduction 627
14.2 Function Templates 628
14.3 Overloading Function Templates 631
14.4 Class Templates 631
14.5 Nontype Parameters and Default Types for Class Templates 638
14.6 Notes on Templates and Inheritance 639
14.7 Notes on Templates and Friends 639
14.8 Notes on Templates and
static Members 640
14.9 Wrap-Up 640

15 Stream Input/Output 645
15.1 Introduction 646
15.2 Streams 647
15.2.1 Classic Streams vs. Standard Streams 647
15.2.2
iostream Library Header Files 648
15.2.3 Stream Input/Output Classes and Objects 648
15.3 Stream Output 651
15.3.1 Output of
char * Variables 651
15.3.2 Character Output Using Member Function
put 651
15.4 Stream Input 652
15.4.1
get and getline Member Functions 652
15.4.2
istream Member Functions peek, putback and ignore 655
15.4.3 Type-Safe I/O 655
15.5 Unformatted I/O Using
read, write and gcount 655
15.6 Introduction to Stream Manipulators 656
Contents xv
15.6.1 Integral Stream Base:
dec, oct, hex and setbase 657
15.6.2 Floating-Point Precision (
precision, setprecision) 658
15.6.3 Field Width (
width, setw) 659
15.6.4 User-Defined Output Stream Manipulators 660
15.7 Stream Format States and Stream Manipulators 662

15.7.1 Trailing Zeros and Decimal Points (
showpoint) 662
15.7.2 Justification (
left, right and internal) 663
15.7.3 Padding (
fill, setfill) 665
15.7.4 Integral Stream Base (
dec, oct, hex, showbase) 666
15.7.5 Floating-Point Numbers; Scientific and Fixed Notation
(
scientific, fixed) 667
15.7.6 Uppercase/Lowercase Control (
uppercase) 668
15.7.7 Specifying Boolean Format (
boolalpha) 668
15.7.8 Setting and Resetting the Format State via Member
Function
flags 669
15.8 Stream Error States 671
15.9 Tying an Output Stream to an Input Stream 673
15.10 Wrap-Up 673
16 Exception Handling 683
16.1 Introduction 684
16.2 Exception-Handling Overview 685
16.3 Example: Handling an Attempt to Divide by Zero 685
16.4 When to Use Exception Handling 691
16.5 Rethrowing an Exception 692
16.6 Exception Specifications 694
16.7 Processing Unexpected Exceptions 695
16.8 Stack Unwinding 695

16.9 Constructors, Destructors and Exception Handling 697
16.10 Exceptions and Inheritance 698
16.11 Processing
new Failures 698
16.12 Class
auto_ptr and Dynamic Memory Allocation 701
16.13 Standard Library Exception Hierarchy 703
16.14 Other Error-Handling Techniques 705
16.15 Wrap-Up 706
17 File Processing 713
17.1 Introduction 714
17.2 Data Hierarchy 714
17.3 Files and Streams 716
17.4 Creating a Sequential File 717
17.5 Reading Data from a Sequential File 721
17.6 Updating Sequential Files 726
17.7 Random-Access Files 727
17.8 Creating a Random-Access File 728
xvi Contents
17.9 Writing Data Randomly to a Random-Access File 733
17.10 Reading from a Random-Access File Sequentially 735
17.11 Case Study: A Transaction-Processing Program 737
17.12 Overview of Object Serialization 743
17.13 Wrap-Up 744
18 Class string and String Stream Processing 755
18.1 Introduction 756
18.2 string Assignment and Concatenation 757
18.3 Comparing strings 759
18.4 Substrings 762
18.5 Swapping

strings 762
18.6 string Characteristics 763
18.7 Finding Substrings and Characters in a string 766
18.8 Replacing Characters in a string 768
18.9 Inserting Characters into a string 769
18.10 Conversion to C-Style Pointer-Based
char * Strings 770
18.11 Iterators 772
18.12 String Stream Processing 773
18.13 Wrap-Up 776
19 Searching and Sorting 784
19.1 Introduction 785
19.2 Searching Algorithms 786
19.2.1 Efficiency of Linear Search 786
19.2.2 Binary Search 788
19.3 Sorting Algorithms 793
19.3.1 Efficiency of Selection Sort 793
19.3.2 Efficiency of Insertion Sort 793
19.3.3 Merge Sort (A Recursive Implementation) 794
19.4 Wrap-Up 801
20 Data Structures 806
20.1 Introduction 807
20.2 Self-Referential Classes 808
20.3 Dynamic Memory Allocation and Data Structures 809
20.4 Linked Lists 809
20.5 Stacks 824
20.6 Queues 829
20.7 Trees 832
20.8 Wrap-Up 841
21 Bits, Characters, C Strings and structs 852

21.1 Introduction 853
Contents xvii
21.2 Structure Definitions 853
21.3 Initializing Structures 856
21.4 Using Structures with Functions 856
21.5
typedef 856
21.6 Example: Card Shuffling and Dealing Simulation 857
21.7 Bitwise Operators 860
21.8 Bit Fields 869
21.9 Character-Handling Library 873
21.10 Pointer-Based String Manipulation Functions 878
21.11 Pointer-Based String-Conversion Functions 885
21.12 Search Functions of the Pointer-Based String-Handling Library 890
21.13 Memory Functions of the Pointer-Based String-Handling Library 895
21.14 Wrap-Up 899
22 Standard Template Library (STL) 916
22.1 Introduction to the Standard Template Library (STL) 917
22.1.1 Introduction to Containers 919
22.1.2 Introduction to Iterators 923
22.1.3 Introduction to Algorithms 928
22.2 Sequence Containers 930
22.2.1
vector Sequence Container 930
22.2.2
list Sequence Container 938
22.2.3
deque Sequence Container 942
22.3 Associative Containers 944
22.3.1

multiset Associative Container 944
22.3.2
set Associative Container 947
22.3.3
multimap Associative Container 948
22.3.4
map Associative Container 950
22.4 Container Adapters 952
22.4.1
stack Adapter 952
22.4.2
queue Adapter 954
22.4.3
priority_queue Adapter 955
22.5 Algorithms 957
22.5.1
fill, fill_n, generate and generate_n 958
22.5.2
equal, mismatch and lexicographical_compare 959
22.5.3
remove, remove_if, remove_copy and remove_copy_if 962
22.5.4
replace, replace_if, replace_copy and replace_copy_if 964
22.5.5 Mathematical Algorithms 967
22.5.6 Basic Searching and Sorting Algorithms 970
22.5.7
swap, iter_swap and swap_ranges 972
22.5.8
copy_backward, merge, unique and reverse 973
22.5.9

inplace_merge, unique_copy and reverse_copy 976
22.5.10 Set Operations 977
22.5.11
lower_bound, upper_bound and equal_range 980
22.5.12 Heapsort 982
xviii Contents
22.5.13
min and max 985
22.5.14 STL Algorithms Not Covered in This Chapter 986
22.6 Class
bitset 987
22.7 Function Objects 991
22.8 Wrap-Up 994
22.9 STL Web Resources 995
Chapters on the Web 1005
Chapters 23–27 are PDF documents posted online at the book’s Companion Website
(located at
www.pearsonhighered.com/deitel).
23 Boost Libraries, Technical Report 1 and C++0x I
23.1 Introduction II
23.2 Deitel Online C++ and Related Resource Centers II
23.3 Boost Libraries II
23.4 Boost Libraries Overview III
23.5 Regular Expressions with the
Boost.Regex Library VI
23.5.1 Regular Expression Example VI
23.5.2 Validating User Input with Regular Expressions IX
23.5.3 Replacing and Splitting Strings XII
23.6 Smart Pointers with
Boost.Smart_ptr XIV

23.6.1 Reference Counted
shared_ptr XIV
23.6.2
weak_ptr: shared_ptr Observer XIX
23.7 Technical Report 1 XXIV
23.8 C++0x XXVI
23.9 Core Language Changes XXVI
23.10 Wrap-Up XXXI
24 Other Topics XL
24.1 Introduction XLI
24.2
const_cast Operator XLI
24.3
mutable Class Members XLIII
24.4
namespaces XLV
24.5 Operator Keywords XLVIII
24.6 Pointers to Class Members (
.* and ->*) L
24.7 Multiple Inheritance LII
24.8 Multiple Inheritance and
virtual Base Classes LVII
24.9 Wrap-Up LXII
25 ATM Case Study, Part 1: Object-Oriented
Design with the UML LXVII
25.1 Introduction LXVIII
25.2 Examining the ATM Requirements Document LXVIII
Contents xix
25.3 Identifying the Classes in the ATM Requirements Document LXXVI
25.4 Identifying Class Attributes LXXXIII

25.5 Identifying Objects’ States and Activities LXXXVII
25.6 Identifying Class Operations XCI
25.7 Indicating Collaboration Among Objects XCVIII
25.8 Wrap-Up CV
26 ATM Case Study, Part 2: Implementing
an Object-Oriented Design CIX
26.1 Introduction CX
26.2 Starting to Program the Classes of the ATM System CX
26.3 Incorporating Inheritance into the ATM System CXVII
26.4 ATM Case Study Implementation CXXIV
26.4.1 Class
ATM CXXIV
26.4.2 Class Screen CXXXII
26.4.3 Class Keypad CXXXIII
26.4.4 Class
CashDispenser CXXXIV
26.4.5 Class DepositSlot CXXXVI
26.4.6 Class
Account CXXXVII
26.4.7 Class BankDatabase CXXXIX
26.4.8 Class Transaction CXLIII
26.4.9 Class
BalanceInquiry CXLV
26.4.10 Class Withdrawal CXLVII
26.4.11 Class Deposit CLII
26.4.12 Test Program
ATMCaseStudy.cpp CLV
26.5 Wrap-Up CLV
27 Game Programming with Ogre CLVIII
27.1 Introduction CLIX

27.2 Installing Ogre, OgreAL and OpenAL CLIX
27.3 Basics of Game Programming CLIX
27.4 The Game of Pong: Code Walkthrough CLXII
27.4.1 Ogre Initialization CLXIII
27.4.2 Creating a Scene CLXXII
27.4.3 Adding to the Scene CLXXIII
27.4.4 Animation and Timers CLXXXV
27.4.5 User Input CLXXXVI
27.4.6 Collision Detection CLXXXVIII
27.4.7 Sound CXCII
27.4.8 Resources CXCIII
27.4.9 Pong Driver CXCIV
27.5 Wrap-Up CXCV
27.6 Ogre Web Resources CXCV
xx Contents
A Operator Precedence and Associativity 1006
B ASCII Character Set 1008
C Fundamental Types 1009
D Number Systems 1011
D.1 Introduction 1012
D.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers 1015
D.3 Converting Octal and Hexadecimal Numbers to Binary Numbers 1016
D.4 Converting from Binary, Octal or Hexadecimal to Decimal 1016
D.5 Converting from Decimal to Binary, Octal or Hexadecimal 1017
D.6 Negative Binary Numbers: Two’s Complement Notation 1019
E Preprocessor 1024
E.1 Introduction 1025
E.2
#include Preprocessor Directive 1025
E.3

#define Preprocessor Directive: Symbolic Constants 1026
E.4
#define Preprocessor Directive: Macros 1026
E.5 Conditional Compilation 1028
E.6
#error and #pragma Preprocessor Directives 1029
E.7 Operators # and ## 1030
E.8 Predefined Symbolic Constants 1030
E.9 Assertions 1031
E.10 Wrap-Up 1031
Appendices on the Web 1036
Appendices F–I are PDF documents posted online at the book’s Companion Website
(located at
www.pearsonhighered.com/deitel).
F C Legacy Code Topics CCV
F.1 Introduction CCVI
F.2 Redirecting Input/Output on UNIX/Linux/Mac OS X and
Windows Systems CCVI
F.3 Variable-Length Argument Lists CCVII
F.4 Using Command-Line Arguments CCIX
F.5 Notes on Compiling Multiple-Source-File Programs CCXI
F.6 Program Termination with
exit and atexit CCXIII
F.7 Type Qualifier
volatile CCXIV
F.8 Suffixes for Integer and Floating-Point Constants CCXIV
F.9 Signal Handling CCXV
F.10 Dynamic Memory Allocation with
calloc and realloc CCXVII
Contents xxi

F.11 Unconditional Branch:
goto CCXVIII
F.12 Unions CCXIX
F.13 Linkage Specifications CCXXII
F.14 Wrap-Up CCXXIII
G UML 2: Additional Diagram Types CCXXIX
G.1 Introduction CCXXIX
G.2 Additional Diagram Types CCXXIX
H Using the Visual Studio Debugger CCXXXI
H.1 Introduction CCXXXII
H.2 Breakpoints and the
Continue Command CCXXXII
H.3
Locals and Watch Windows CCXXXVIII
H.4 Controlling Execution Using the
Step Into, Step Over, Step Out
and
Continue Commands CCXLI
H.5
Autos Window CCXLIII
H.6 Wrap-Up CCXLIV
I Using the GNU C++ Debugger CCXLVII
I.1 Introduction CCXLVIII
I.2 Breakpoints and the
run, stop, continue and print Commands CCXLVIII
I.3
print and set Commands CCLIV
I.4 Controlling Execution Using the
step, finish and next Commands CCLVI
I.5

watch Command CCLIX
I.6 Wrap-Up CCLXI
Index 1037
This page intentionally left blank
“The chief merit of language is clearness …”
—Galen
Welcome to the world of C++ programming and C++ How to Program, Seventh Edition!
This book presents leading-edge computing technologies for students, instructors and
software development professionals.
At the heart of the book is the Deitel signature “live-code approach.” Concepts are
presented in the context of complete working C++ programs, rather than in code snippets.
Each code example is immediately followed by one or more sample executions. All the
source code is available at
www.deitel.com/books/cpphtp7/.
New and Updated Features
Here are the updates we’ve made for C++ How to Program, 7/e:
• “Making a Difference” Exercise Sets. We encourage you to use computers and the
Internet to research and solve problems that really matter. These new exercises are
meant to increase awareness of important issues the world is facing. We hope
you’ll approach them with your own values, politics and beliefs.
• Prefer
string Objects to C Strings. C++ offers two types of strings—string class
objects (which we use starting in Chapter 3) and C-style, pointer-based strings.
We continue to include some early discussions of C strings to give you practice
with pointer manipulations, to illustrate dynamic memory allocation with
new
and delete and to prepare you for working with C strings in the “legacy code”
that you’ll encounter in industry. In new development, you should favor
string
class objects. We’ve replaced most occurrences of C strings with instances of C++

class
string to make programs more robust and eliminate many of the security
problems that can be caused by manipulating C strings.
• Prefer vectors to C Arrays. Similarly, C++ offers two types of arrays—vector
class objects (which we use starting in Chapter 7) and C-style, pointer-based ar-
rays. As appropriate, we use class template
vector instead of C arrays throughout
the book. However, we begin by discussing C arrays in Chapter 7 to prepare you
for working with legacy code and to use as a basis for building your own custom-
ized
Array class in Chapter 11, Operator Overloading.
• New Companion Website (
www.pearsonhighered.com/deitel/). This edition’s
Companion Website includes a wealth of material to help you with your study of
C++ programming. We provide an extensive number of VideoNotes that walk
you through the code examples in 14 of the key chapters, solutions to many of
the book’s exercises, bonus chapters, and more (see the Companion Website sec-
tion later in this Preface).
Preface
xxiv Preface
• Dynamic Memory Allocation. We moved dynamic memory allocation later in the
book to Chapter 11, where it’s first needed. The “proxy class” discussion (which
uses dynamic memory) has also been moved to Chapter 11.
• Titled Programming Exercises. We’ve titled all the programming exercises. This
and the previous two features help instructors tune assignments for their classes.
• Eliminated “Magic” Numbers. We eliminated all uses of truly “magic” numbers
and replaced them with named constants or
enums as appropriate. In a few cases in
which the context is absolutely clear, we don’t consider numbers to be “magic.”
• Enhanced Use of

const. We increased our use of const bookwide to encourage
better software engineering.
• Eliminated “
return 0;”. According to the C++ standard, any main function that
does not contain “
return 0;” as its last statement is assumed to return 0. For this
reason, we’ve eliminated “
return 0;” from all but the first program in the book.
• Use
“using namespace std;”. Previously, we specified a using declaration for
every individual item that we referenced from a C++ Standard Library header file.
Since these items are well known and unlikely to have name collisions with other
C++ libraries, we now use
“using namespace std;” for all C++ Standard Library
components from Chapter 3 forward. This simplifies the programs and saves
many lines of code.
• New Design. The book has a new interior design that graphically serves to orga-
nize, clarify and highlight the information, and enhances the book’s pedagogy.
• Reorganized Optional OOD Case Study. We tuned the Object-Oriented Design/
UML automated teller machine (ATM) case study and reorganized it into two
optional chapters (25 and 26) that present the ATM’s design and complete code
implementation. This is a nice business example that most students can relate to.
Working through these two chapters as a unit will help you tie together many of
the object-oriented programming (OOP) concepts you learn in Chapters 1–13.
A key concept in OOP is the interactions among objects. In most textbooks, the
code examples create and use only one or two objects. The ATM case study gives
you the opportunity to examine the interactions among many objects that pro-
vide the functionality of a substantial system. For instructors who wish to cover
the case study in a distributed manner, we’ve indicated where each section in
Chapters 25 and 26 can be covered inline with earlier chapters in the book.

• Function Pointer Exercises. We added several real-world function-pointers exer-
cises. These are available at the Companion Website and at
www.deitel.com/
books/cpphtp7/
.
• Improved Terminology Sections. We’ve added page numbers for the defining
occurrences of all terms in the terminology lists for easy reference.
New Features in the Next C++ Standard
We discuss four new language features that will be part of the next C++ standard and are
already implemented by some of today’s C++ compilers. These include:
• Initializer Lists for User-Defined Types. These enable objects of your own types
to be initialized using the same syntax as built-in arrays.

×