458 Pitfall: Copying past the end of a C-string using strcpy 461
C-String Input and Output 464
C-String-to-Number Conversions and Robust Input 466
8.2 The Standard string Class 472
Introduction to the Standard Class string 472
I/O with the Class string 475
Programming Tip: More Versions of getline 478
Pitfall: Mixing cin >> variable; and getline 479
String Processing with the Class string 480
Programming Example: Palindrome Testing 484
Converting Between string Objects and C Strings 487
Converting Between Strings and Numbers 488
8.3 Vectors 489
Vector Basics 489
Pitfall: Using Square Brackets Beyond the Vector Size 492
Programming Tip: Vector Assignment Is Well Behaved 493
Efficiency Issues 493
Chapter Summary 495
Answers to Self-Test Exercises 495
Practice Programs 497
Programming Projects 498
Chapter 9 Pointers and Dynamic Arrays 507
9.1 Pointers 508
Pointer Variables 509
Basic Memory Management 516
Pitfall: Dangling Pointers 517
Static Variables and Automatic Variables 518
Programming Tip: Define Pointer Types 518
9.2 Dynamic Arrays 521
Array Variables and Pointer Variables 521
Creating and Using Dynamic Arrays 522
Pointer Arithmetic (Optional) 528
Multidimensional Dynamic Arrays (Optional) 530
Chapter Summary 532
Answers to Self-Test Exercises 532
Practice Programs 533
Programming Projects 534
Chapter 10 Defining Classes 541
10.1 Structures 542
Structures for Diverse Data 542
Pitfall: Forgetting a Semicolon in a Structure Definition 547
Structures as Function Arguments 548
Programming Tip: Use Hierarchical Structures 549
Initializing Structures 551
10.2 Classes 554
Defining Classes and Member Functions 554
Public and Private Members 559
Programming Tip: Make All Member Variables Private 567
Programming Tip: Define Accessor and Mutator Functions 567
Programming Tip: Use the Assignment Operator with Objects 569
Programming Example: BankAccount Class–Version 1 570
Summary of Some Properties of Classes 574
Constructors for Initialization 576
Programming Tip: Always Include a Default Constructor 584
Pitfall: Constructors with No Arguments 585
Member Initializers and Constructor Delegation in C++11 587
10.3 Abstract Data Types 588
Classes to Produce Abstract Data Types 589
Programming Example: Alternative Implementation of a Class 593
10.4 Introduction to Inheritance 598
Derived Classes 599
Defining Derived Classes 600
Chapter Summary 604
Answers to Self-Test Exercises 605
Practice Programs 611
Programming Projects 612
Chapter 11 Friends, Overloaded Operators, and Arrays in Classes 619
11.1 Friend Functions 620
Programming Example: An Equality Function 620
Friend Functions 624
Programming Tip: Define Both Accessor Functions and Friend Functions 626
Programming Tip: Use Both Member and Nonmember Functions 628
Programming Example: Money Class (Version 1) 628
Implementation of digit_to_int (Optional) 635
Pitfall: Leading Zeros in Number Constants 636
The const Parameter Modifier 638
Pitfall: Inconsistent Use of const 639
11.2 Overloading Operators 643
Overloading Operators 644
Constructors for Automatic Type Conversion 647
Overloading Unary Operators 649
Overloading >> and < 650="">
11.3 Arrays and Classes 660
Arrays of Classes 660
Arrays as Class Members 664
Programming Example: A Class for a Partially Filled Array 665
11.4 Classes and Dynamic Arrays 667
Programming Example: A String Variable Class 668
Destructors 671
Pitfall: Pointers as Call-by-Value Parameters 674
Copy Constructors 675
Overloading the Assignment Operator 680
Chapter Summary 683
Answers to Self-Test Exercises 683
Practice Programs 693
Programming Projects 694
Chapter 12 Separate Compilation and Namespaces 703
12.1 Separate Compilation 704
ADTs Reviewed 705
Case Study: DigitalTime –A Class Compiled Separately 706
Using #ifndef 715
Programming Tip: Defining Other Libraries 718
12.2 Namespaces 719
Namespaces and using Directives 719
Creating a Namespace 721
Qualifying Names 724
A Subtle Point About Namespaces (Optional) 725
Unnamed Namespaces 726
Programming Tip: Choosing a Name for a Namespace 731
Pitfall: Confusing the Global Namespace and the Unnamed Namespace 732
Chapter Summary 733
Answers to Self-Test Exercises 734
Practice Programs 736
Programming Projects 738
Chapter 13 Pointers and Linked Lists 739
13.1 Nodes and Linked Lists 740
Nodes 740
nullptr 745
Linked Lists 746
Inserting a Node at the Head of a List 747
Pitfall: Losing Nodes 750
Searching a Linked List 751
Pointers as Iterators 755
Inserting and Removing Nodes Inside a List 755
Pitfall: Using the Assignment Operator with Dynamic Data Structures 757
Variations on Linked Lists 760
Linked Lists of Classes 762
13.2 Stacks and Queues 765
Stacks 765
Programming Example: A Stack Class 766
Queues 771
Programming Example: A Queue Class 772
Chapter Summary 776
Answers to Self-Test Exercises 776
Practice Programs 779
Programming Projects 780
Chapter 14 Recursion 789
14.1 Recursive Functions for Tasks 791
Case Study: Vertical Numbers 791
A Closer Look at Recursion 797
Pitfall: Infinite Recursion 799
Stacks for Recursion 800
Pitfall: Stack Overflow 802
Recursion Versus Iteration 802
14.2 Recursive Functions for Values 804
General Form for a Recursive Function That Returns a Value 804
Programming Example: Another Powers Function 804
14.3 Thinking Recursively 809
Recursive Design Techniques 809
Case Study: Binary Search–An Example of Recursive Thinking 810
Programming Example: A Recursive Member Function 818
Chapter Summary 822
Answers to Self-Test Exercises 822
Practice Programs 827
Programming Projects 827
Chapter 15 Inheritance 833
15.1 Inheritance Basics 834
Derived Classes 837
Constructors in Derived Classes 845
Pitfall: Use of Private Member Variables from the Base Class 848
Pitfall: Private Member Functions Are Effectively Not Inherited 850
The protected Qualifier 850
Redefinition of Member Functions 853
Redefining Versus Overloading 856
Access to a Redefined Base Function 858
15.2 INHERITANCE DETAILS 859
Functions That Are Not Inherited 859
Assignment Operators and Copy Constructors in Derived Classes 860
Destructors in Derived Classes 861
15.3 Polymorphism 862
Late Binding 863
Virtual Functions in C++ 864
Virtual Functions and Extended Type Compatibility 869
Pitfall: The Slicing Problem 873
Pitfall: Not Using Virtual Member Functions 874
Pitfall: Attempting to Compile Class Definitions Without Definitions for Every Virtual Member Function 875
Programming Tip: Make Destructors Virtual 875
Chapter Summary 877
Answers to Self-Test Exercises 877
Practice Programs 881
Programming Projects 884
Chapter 16 Exception Handling 893
16.1 Exception-Handling Basics 895
A Toy Example of Exception Handling 895
Defining Your Own Exception Classes 904
Multiple Throws and Catches 904
Pitfall: Catch the More Specific Exception First 908
Programming Tip: Exception Classes Can Be Trivial 909
Throwing an Exception in a Function 909
Exception Specification 911
Pitfall: Exception Specification in Derived Classes 913
16.2 Programming Techniques for
Exception Handling 914
When to Throw an Exception 914
Pitfall: Uncaught Exceptions 916
Pitfall: Nested try-catch Blocks 916
Pitfall: Overuse of Exceptions 916
Exception Class Hierarchies 917
Testing for Available Memory 917
Rethrowing an Exception 918
Chapter Summary 918
Answers to Self-Test Exercises 918
Practice Programs 920
Programming Projects 921
Chapter 17 Templates 925
17.1 Templates for Algorithm Abstraction 926
Templates for Functions 927
Pitfall: Compiler Complications 931
Programming Example: A Generic Sorting Function 933
Programming Tip: How to Define Templates 937
Pitfall: Using a Template with an Inappropriate Type 938
17.2 Templates for Data Abstraction 939
Syntax for Class Templates 939
Programming Example: An Array Class 942
Chapter Summary 949
Answers to Self-Test Exercises 949
Practice Programs 953
Programming Projects 953
Chapter 18 Standard Template Library 957
18.1 Iterators 959
using Declarations 959
Iterator Basics 960
Programming Tip: Use auto to Simplify Variable Declarations 964
Pitfall: Compiler Problems 964
Kinds of Iterators 966
Constant and Mutable Iterators 970
Reverse Iterators 971
Other Kinds of Iterators 972
18.2 Containers 973
Sequential Containers 974
Pitfall: Iterators and Removing Elements 978
Programming Tip: Type Definitions in Containers 979
Container Adapters stack and queue 979
Associative Containers set and map 983
Programming Tip: Use Initialization, Ranged For, and auto with Containers 990
Efficiency 990
18.3 Generic Algorithms 991
Running Times and Big-O Notation 992
Container Access Running Times 995
Nonmodifying Sequence Algorithms 997
Container Modifying Algorithms 1001
Set Algorithms 1003
Sorting Algorithms 1004
Chapter Summary 1005
Answers to Self-Test Exercises 1005
Practice Programs 1007
Programming Projects 1008
Appendices
1 C++ Keywords 1015
2 Precedence of Operators 1016
3 The ASCII Character Set 1018
4 Some Library Functions 1019
5 Inline Functions 1026
6 Overloading the Array Index Square Brackets 1027
7 The this Pointer 1029
8 Overloading Operators as Member Operators 1032
Index 1034