Brief Contents Table of Location of VideoNotes Inside front cover and inside back cover Chapter 1 Introduction to Computers and C++Programming 1 Chapter 2 C++Basics 39 Chapter 3 More Flow of Control 111 Chapter 4 Procedural Abstraction and Functions That Return a Value 181 Chapter 5 Functions for All Subtasks 251 Chapter 6 1/O Streams as an Introduction to Objects and Classes 305 Chapter 7 Arrays 377 Chapter 8 Strings and Vectors 451 Chapter 9 Pointers and Dynamic Arrays 507 Chapter 10 Defining Classes 541 Chapter 11 Friends,Overloaded Operators,and Arrays in Classes 619 xiv
xiv Brief Contents Table of Location of VideoNotes Inside front cover and inside back cover Chapter 1 Introduction to Computers and C++ Programming 1 Chapter 2 C++ Basics 39 Chapter 3 More Flow of Control 111 Chapter 4 Procedural Abstraction and Functions That Return a Value 181 Chapter 5 Functions for All Subtasks 251 Chapter 6 I/O Streams as an Introduction to Objects and Classes 305 Chapter 7 Arrays 377 Chapter 8 Strings and Vectors 451 Chapter 9 Pointers and Dynamic Arrays 507 Chapter 10 Defining Classes 541 Chapter 11 Friends, Overloaded Operators, and Arrays in Classes 619
BRIEF CONTENTS XV Chapter 12 Separate Compilation and Namespaces 703 Chapter 13 Pointers and Linked Lists 739 Chapter 14 Recursion 789 Chapter 15 Inheritance 833 Chapter 16 Exception Handling 893 Chapter 17 Templates 925 Chapter 18 Standard Template Library 957 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
Brief Contents xv Chapter 12 Separate Compilation and Namespaces 703 Chapter 13 Pointers and Linked Lists 739 Chapter 14 Recursion 789 Chapter 15 Inheritance 833 Chapter 16 Exception Handling 893 Chapter 17 Templates 925 Chapter 18 Standard Template Library 957 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
Contents Table of Location of VideoNotes Inside front cover and inside back cover Chapter 1 Introduction to Computers and C++ Programming 1 1.1 COMPUTER SYSTEMS 2 Hardware 2 Software 7 High-Level Languages 8 Compilers 9 History Note 12 1.2 PROGRAMMING AND PROBLEM-SOLVING 12 Algorithms 12 Program Design 15 Object-Oriented Programming 16 The Software Life Cycle 17 1.3 INTRODUCTION TO C++18 Origins of the C++Language 18 A Sample C++Program 19 Pitfall:Using the Wrong Slash in \n 23 Programming Tip:Input and Output Syntax 23 Layout of a Simple C++Program 24 Pitfall:Putting a Space Before the include File Name 26 Compiling and Running a C++Program 26 Pitfall:Compiling a C++11 program 27 Programming Tip:Getting Your Program to Run 27 1.4 TESTING AND DEBUGGING 29 Kinds of Program Errors 30 Pitfall:Assuming Your Program Is Correct 31 xvi
xvi Contents Table of Location of VideoNotes Inside front cover and inside back cover Chapter 1 Introduction to Computers and C++ Programming 1 1.1 Computer Systems 2 Hardware 2 Software 7 High-Level Languages 8 Compilers 9 History Note 12 1.2 Programming and Problem-Solving 12 Algorithms 12 Program Design 15 Object-Oriented Programming 16 The Software Life Cycle 17 1.3 Introduction to C++ 18 Origins of the C++ Language 18 A Sample C++ Program 19 Pitfall: Using the Wrong Slash in \n 23 Programming Tip: Input and Output Syntax 23 Layout of a Simple C++ Program 24 Pitfall: Putting a Space Before the include File Name 26 Compiling and Running a C++ Program 26 Pitfall: Compiling a C++11 program 27 Programming Tip: Getting Your Program to Run 27 1.4 Testing and Debugging 29 Kinds of Program Errors 30 Pitfall: Assuming Your Program Is Correct 31
CONTENTS xvii Chapter Summary 32 Answers to Self-Test Exercises 33 Practice Programs 35 Programming Projects 36 Chapter 2 C++Basics 39 2.1 VARIABLES AND ASSIGNMENTS 40 Variables 40 Names:Identifiers 42 Variable Declarations 44 Assignment Statements 45 Pitfall:Uninitialized Variables 47 Programming Tip:Use Meaningful Names 49 2.2 INPUT AND OUTPUT 50 Output Using cout 50 Include Directives and Namespaces 52 Escape Sequences 53 Programming Tip:End Each Program with a \n or end1 55 Formatting for Numbers with a Decimal Point 55 Input Using cin 56 Designing Input and Output 58 Programming Tip:Line Breaks in I/O 58 2.3 DATA TYPES AND EXPRESSIONS 60 The Types int and double 60 Other Number Types 62 C++11 Types 63 The Type char 64 The Type bool 66 Introduction to the Class string 66 Type Compatibilities 68 Arithmetic Operators and Expressions 69 Pitfall:Whole Numbers in Division 72 More Assignment Statements 74 2.4 SIMPLE FLOW OF CONTROL 74 A Simple Branching Mechanism 75 Pitfall:Strings of Inequalities 80 Pitfall:Using in place of =81 Compound Statements 82
Contents xvii Chapter Summary 32 Answers to Self-Test Exercises 33 Practice Programs 35 Programming Projects 36 Chapter 2 C++ Basics 39 2.1 Variables and Assignments 40 Variables 40 Names: Identifiers 42 Variable Declarations 44 Assignment Statements 45 Pitfall: Uninitialized Variables 47 Programming Tip: Use Meaningful Names 49 2.2 Input and Output 50 Output Using cout 50 Include Directives and Namespaces 52 Escape Sequences 53 Programming Tip: End Each Program with a \n or endl 55 Formatting for Numbers with a Decimal Point 55 Input Using cin 56 Designing Input and Output 58 Programming Tip: Line Breaks in I/O 58 2.3 Data Types and Expressions 60 The Types int and double 60 Other Number Types 62 C++11 Types 63 The Type char 64 The Type bool 66 Introduction to the Class string 66 Type Compatibilities 68 Arithmetic Operators and Expressions 69 Pitfall: Whole Numbers in Division 72 More Assignment Statements 74 2.4 Simple Flow of Control 74 A Simple Branching Mechanism 75 Pitfall: Strings of Inequalities 80 Pitfall: Using = in place of == 81 Compound Statements 82
xviii CONTENTS Simple Loop Mechanisms 84 Increment and Decrement Operators 87 Programming Example:Charge Card Balance 89 Pitfall:Infinite Loops 90 2.5 PROGRAM STYLE 93 Indenting 93 Comments 93 Naming Constants 95 Chapter Summary 98 Answers to Self-Test Exercises 98 Practice Programs 103 Programming Projects 105 Chapter 3 More Flow of Control 111 3.1 USING BOOLEAN EXPRESSIONS 112 Evaluating Boolean Expressions 112 Pitfall:Boolean Expressions Convert to int Values 116 Enumeration Types (Optional 119 3.2 MULTIWAY BRANCHES 120 Nested Statements 120 Programming Tip:Use Braces in Nested Statements 121 Multiway if-else Statements 123 Programming Example:State Income Tax 125 The switch Statement 128 Pitfall:Forgetting a break in a switch Statement 132 Using switch Statements for Menus 133 Blocks 135 Pitfall:Inadvertent Local Variables 138 3.3 MORE ABOUT C++LOOP STATEMENTS 139 The while Statements Reviewed 139 Increment and Decrement Operators Revisited 141 The for Statement 144 Pitfall:Extra Semicolon in a for Statement 149 What Kind of Loop to Use 150 Pitfall:Uninitialized Variables and Infinite Loops 152 The break Statement 153 Pitfall:The break Statement in Nested Loops 154
xviii Contents Simple Loop Mechanisms 84 Increment and Decrement Operators 87 Programming Example: Charge Card Balance 89 Pitfall: Infinite Loops 90 2.5 Program Style 93 Indenting 93 Comments 93 Naming Constants 95 Chapter Summary 98 Answers to Self-Test Exercises 98 Practice Programs 103 Programming Projects 105 Chapter 3 More Flow of Control 111 3.1 Using Boolean Expressions 112 Evaluating Boolean Expressions 112 Pitfall: Boolean Expressions Convert to int Values 116 Enumeration Types (Optional) 119 3.2 Multiway Branches 120 Nested Statements 120 Programming Tip: Use Braces in Nested Statements 121 Multiway if-else Statements 123 Programming Example: State Income Tax 125 The switch Statement 128 Pitfall: Forgetting a break in a switch Statement 132 Using switch Statements for Menus 133 Blocks 135 Pitfall: Inadvertent Local Variables 138 3.3 More About C++ Loop Statements 139 The while Statements Reviewed 139 Increment and Decrement Operators Revisited 141 The for Statement 144 Pitfall: Extra Semicolon in a for Statement 149 What Kind of Loop to Use 150 Pitfall: Uninitialized Variables and Infinite Loops 152 The break Statement 153 Pitfall: The break Statement in Nested Loops 154