453 C-String Input and Output 457
C-String-to-Number Conversions and Robust Input 460
8.2 THE STANDARD string CLASS 465
Introduction to the Standard Class string 465
I/O with the Class string 468
Programming Tip: More Versions of getline 472
Pitfall: Mixing cin >> variable; and getline 472
String Processing with the Class string 474
Programming Example: Palindrome Testing 476
Converting between string Objects and C Strings 481
8.3 VECTORS 482
Vector Basics 482
Pitfall: Using Square Brackets Beyond the Vector Size 484
Programming Tip: Vector Assignment Is Well Behaved 486
Efficiency Issues 487
Chapter Summary 488
Answers to Self-Test Exercises 489
Programming Projects 491
Chapter 9 Pointers and Dynamic Arrays 499
9.1 POINTERS 500
Pointer Variables 501
Basic Memory Management 508
Pitfall: Dangling Pointers 509
Static Variables and Automatic Variables 510
Programming Tip: Define Pointer Types 510
9.2 DYNAMIC ARRAYS 513
Array Variables and Pointer Variables 513
Creating and Using Dynamic Arrays 513
Pointer Arithmetic (Optional) 519
Multidimensional Dynamic Arrays (Optional) 521
Chapter Summary 523
Answers to Self-Test Exercises 523
Programming Projects 524
Chapter 10 Defining Classes 529
10.1 STRUCTURES 530
Structures for Diverse Data 530
Pitfall: Forgetting a Semicolon in a Structure Definition 535
Structures as Function Arguments 536
Programming Tip: Use Hierarchical Structures 537
Initializing Structures 539
10.2 CLASSES 542
Defining Classes and Member Functions 542
Public and Private Members 547
Programming Tip: Make All Member Variables Private 555
Programming Tip: Define Accessor and Mutator Functions 555
Programming Tip: Use the Assignment Operator with Objects 557
Programming Example: BankAccount Class–Version 1 557
Summary of Some Properties of Classes 562
Constructors for Initialization 564
Programming Tip: Always Include a Default Constructor 572
Pitfall: Constructors with No Arguments 573
10.3 ABSTRACT DATA TYPES 575
Classes to Produce Abstract Data Types 576
Programming Example: Alternative Implementation of a Class 580
10.4 INTRODUCTION TO INHERITANCE 584
Inheritance Among Stream Classes 585
Programming Example: Another new_line Function 588
Default Arguments for Functions (Optional) 589
Defining Derived Classes 591
Chapter Summary 594
Answers to Self-Test Exercises 595
Programming Projects 603
Chapter 11 Friends, Overloaded Operators, and
Arrays in Classes 609
11.1 FRIEND FUNCTIONS 610
Programming Example: An Equality Function 610
Friend Functions 614
Programming Tip: Define Both Accessor Functions and Friend
Functions 616
Programming Tip: Use Both Member and Nonmember Functions 618
Programming Example: Money Class (Version 1) 618
Implementation of digit_to_int (Optional) 625
Pitfall: Leading Zeros in Number Constants 626
The const Parameter Modifier 628
Pitfall: Inconsistent Use of const 630
11.2 OVERLOADING OPERATORS 633
Overloading Operators 634
Constructors for Automatic Type Conversion 638
Overloading Unary Operators 640
Overloading >> and < 640="">
11.3 ARRAYS AND CLASSES 651
Arrays of Classes 651
Arrays as Class Members 655
Programming Example: A Class for a Partially Filled Array 656
11.4 CLASSES AND DYNAMIC ARRAYS 659
Programming Example: A String Variable Class 659
Destructors 663
Pitfall: Pointers as Call-by-Value Parameters 665
Copy Constructors 667
Overloading the Assignment Operator 672
Chapter Summary 675
Answers to Self-Test Exercises 676
Programming Projects 686
Chapter 12 Separate Compilation and Namespaces 695
12.1 SEPARATE COMPILATION 696
ADTs Reviewed 697
Case Study: DigitalTime–A Class Compiled Separately 698
Using #ifndef 707
Programming Tip: Defining Other Libraries 710
12.2 NAMESPACES 712
Namespaces and using Directives 712
Creating a Namespace 714
Qualifying Names 717
A Subtle Point About Namespaces (Optional) 718
Unnamed Namespaces 719
Programming Tip: Choosing a Name for a Namespace 722
Pitfall: Confusing the Global Namespace and the Unnamed
Namespace 724
Chapter Summary 727
Answers to Self-Test Exercises 727
Programming Projects 729
Chapter 13 Pointers and Linked Lists 733
13.1 NODES AND LINKED LISTS 734
Nodes 734
Linked Lists 740
Inserting a Node at the Head of a List 741
Pitfall: Losing Nodes 744
Searching a Linked List 745
Pointers as Iterators 749
Inserting and Removing Nodes Inside a List 749
Pitfall: Using the Assignment Operator with Dynamic
Data Structures 752
Variations on Linked Lists 754
Linked Lists of Classes 756
13.2 STACKS AND QUEUES 760
Stacks 760
Programming Example: A Stack Class 761
Queues 766
Programming Example: A Queue Class 767
Chapter Summary 771
Answers to Self-Test Exercises 772
Programming Projects 775
Chapter 14 Recursion 781
14.1 RECURSIVE FUNCTIONS FOR TASKS 783
Case Study: Vertical Numbers 783
A Closer Look at Recursion 790
Pitfall: Infinite Recursion 791
Stacks for Recursion 793
Pitfall: Stack Overflow 794
Recursion Versus Iteration 795
14.2 RECURSIVE FUNCTIONS FOR VALUES 796
General Form for a Recursive Function That Returns a Value 796
Programming Example: Another Powers Function 797
14.3 THINKING RECURSIVELY 801
Recursive Design Techniques 801
Case Study: Binary Search–An Example of Recursive Thinking 803
Programming Example: A Recursive Member Function 810
Chapter Summary 815
Answers to Self-Test Exercises 815
Programming Projects 820
Chapter 15 Inheritance 825
15.1 INHERITANCE BASICS 826
Derived Classes 827
Constructors in Derived Classes 835
Pitfall: Use of Private Member Variables from the Base Class 838
Pitfall: Private Member Functions Are Effectively Not Inherited 840
The protected Qualifier 840
Redefinition of Member Functions 843
Redefining Versus Overloading 847
Access to a Redefined Base Function 848
15.2 INHERITANCE DETAILS 849
Functions That Are Not Inherited 850
Assignment Operators and Copy Constructors in Derived Classes 850
Destructors in Derived Classes 851
15.3 POLYMORPHISM 853
Late Binding 853
Virtual Functions in C++ 854
Virtual Functions and Extended Type Compatibility 860
Pitfall: The Slicing Problem 864
Pitfall: Not Using Virtual Member Functions 864
Pitfall: Attempting to Compile Class Definitions Without Definitions for
Every Virtual Member Function 865
Programming Tip: Make Destructors Virtual 866
Chapter Summary 867
Answers to Self-Test Exercises 868
Programming Projects 872
Chapter 16 Exception Handling 881
16.1 EXCEPTION-HANDLING BASICS 883
A Toy Example of Exception Handling 883
Defining Your Own Exception Classes 892
Multiple Throws and Catches 892
Pitfall: Catch the More Specific Exception First 896
Programming Tip: Exception Classes Can Be Trivial 897
Throwing an Exception in a Function 898
Exception Specification 898
Pitfall: Exception Specification in Derived Classes 902
16.2 PROGRAMMING TECHNIQUES FOR
EXCEPTION HANDLING 903
When to Throw an Exception 903
Pitfall: Uncaught Exceptions 905
Pitfall: Nested try-catch Blocks 905
Pitfall: Overuse of Exceptions 905
Exception Class Hierarchies 906
Testing for Available Memory 906
Rethrowing an Exception 907
Chapter Summary 907
Answers to Self-Test Exercises 907
Programming Projects 909
Chapter 17 Templates 913
17.1 TEMPLATES FOR ALGORITHM ABSTRACTION 914
Templates for Functions 915
Pitfall: Compiler Complications 919
Programming Example: A Generic Sorting Function 921
Programming Tip: How to Define Templates 925
Pitfall: Using a Template with an Inappropriate Type 926
17.2 TEMPLATES FOR DATA ABSTRACTION 927
Syntax for Class Templates 927
Programming Example: An Array Class 930
Chapter Summary 936
Answers to Self-Test Exercises 936
Programming Projects 939
Chapter 18 Standard Template Library 943
18.1 ITERATORS 945
Using Declarations 945
Iterator Basics 946
Pitfall: Compiler Problems 951
Kinds of Iterators 952
Constant and Mutable Iterators 956
Reverse Iterators 957
Other Kinds of Iterators 959
18.2 CONTAINERS 960
Sequential Containers 960
Pitfall: Iterators and Removing Elements 965
Programming Tip: Type Definitions in Containers 965
Container Adapters stack and queue 966
Associative Containers set and map 970
Efficiency 976
18.3 GENERIC ALGORITHMS 977
Running Times and Big-O Notation 978
Container Access Running Times 982
Nonmodifying Sequence Algorithms 983
Container Modifying Algorithms 989
Set Algorithms 989
Sorting Algorithms 991
Chapter Summary 991
Answers to Self-Test Exercises 992
Programming Projects 994
APPENDICES
1 C++ Keywords 999
2 Precedence of Operators 1000
3 The ASCII Character Set 1002
4 Some Library Functions 1003
5 Inline Functions 1011
6 Overloading the Array Index Square Brackets 1012
7 The this Pointer 1014
8 Overloading Operators as Member Operators 1017
INDEX 1019