Problem Solving with C++. 10 Ed

Problem Solving with C++. 10 Ed

Problem Solving with C++. 10 Ed
Автор: Savitch Walter
Дата выхода: 2018
Издательство: Pearson Education Limited
Количество страниц: 1117
Размер файла: 5.9 MB
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

Cover....1

Inside Front Cover....2

Title Page ....6

Copyright Page ....7

Preface....8

Acknowledgments....13

Brief Contents....18

Contents....20

Chapter 1 Introduction to Computers and C++ Programming....36

1.1 COMPUTER SYSTEMS....37

Hardware....37

Software....42

High-Level Languages....43

Compilers....44

History Note....47

1.2 PROGRAMMING AND PROBLEM-SOLVING....47

Algorithms....47

Program Design....50

Object-Oriented Programming....51

The Software Life Cycle....52

1.3 INTRODUCTION TO C++....53

Origins of the C++ Language....53

A Sample C++ Program....54

Pitfall: Using the Wrong Slash in \n....58

Programming Tip: Input and Output Syntax....58

Layout of a Simple C++ Program....59

Pitfall: Putting a Space Before the include File Name....61

Compiling and Running a C++ Program....61

Pitfall: Compiling a C++11 Program....62

Programming Tip: Getting Your Program to Run....62

1.4 TESTING AND DEBUGGING....64

Kinds of Program Errors....65

Pitfall: Assuming Your Program Is Correct....66

Chapter Summary....67

Answers to Self-Test Exercises....68

Practice Programs....70

Programming Projects....71

Chapter 2 C++ Basics....74

2.1 VARIABLES AND ASSIGNMENTS....75

Variables....75

Names: Identifiers....77

Variable Declarations....79

Assignment Statements....81

Pitfall: Uninitialized Variables....83

Programming Tip: Use Meaningful Names....84

2.2 INPUT AND OUTPUT....85

Output Using cout....85

Include Directives and Namespaces....87

Escape Sequences....88

Programming Tip: End Each Program with a \n or end1....90

Formatting for Numbers with a Decimal Point....90

Input Using cin....91

Designing Input and Output....93

Programming Tip: Line Breaks in I/O....93

2.3 DATA TYPES AND EXPRESSIONS....95

The Types int and double....95

Other Number Types....97

C++11 Types....98

The Type char....99

The Type bool....101

Introduction to the Class string....101

Type Compatibilities....103

Arithmetic Operators and Expressions....104

Pitfall: Whole Numbers in Division....107

More Assignment Statements....109

2.4 SIMPLE FLOW OF CONTROL....109

A Simple Branching Mechanism....110

Pitfall: Strings of Inequalities....115

Pitfall: Using = in place of ==....116

Compound Statements....117

Simple Loop Mechanisms....119

Increment and Decrement Operators....122

Programming Example: Charge Card Balance....124

Pitfall: Infinite Loops....125

2.5 PROGRAM STYLE....128

Indenting....128

Comments....128

Naming Constants....130

Chapter Summary....133

Answers to Self-Test Exercises....133

Practice Programs....138

Programming Projects....140

Chapter 3 More Flow of Control....146

3.1 USING BOOLEAN EXPRESSIONS....147

Evaluating Boolean Expressions....147

Pitfall: Boolean Expressions Convert to int Values....151

Enumeration Types (Optional)....154

3.2 MULTIWAY BRANCHES....155

Nested Statements....155

Programming Tip: Use Braces in Nested Statements....156

Multiway if-else Statements....158

Programming Example: State Income Tax....160

The switch Statement....163

Pitfall: Forgetting a break in a switch Statement....167

Using switch Statements for Menus....168

Blocks....170

Pitfall: Inadvertent Local Variables....173

3.3 MORE ABOUT C++ LOOP STATEMENTS....174

The while Statements Reviewed....174

Increment and Decrement Operators Revisited....176

The for Statement....179

Pitfall: Extra Semicolon in a for Statement....184

What Kind of Loop to Use....185

Pitfall: Uninitialized Variables and Infinite Loops....187

The break Statement....188

Pitfall: The break Statement in Nested Loops....189

3.4 DESIGNING LOOPS....190

Loops for Sums and Products....190

Ending a Loop....192

Nested Loops....195

Debugging Loops....197

Chapter Summary....200

Answers to Self-Test Exercises....201

Practice Programs....207

Programming Projects....209

Chapter 4 Procedural Abstraction and Functions That Return a Value....216

4.1 TOP-DOWN DESIGN....217

4.2 PREDEFINED FUNCTIONS....218

Using Predefined Functions....218

Random Number Generation....223

Type Casting....225

Older Form of Type Casting....227

Pitfall: Integer Division Drops the Fractional Part....227

4.3 PROGRAMMER-DEFINED FUNCTIONS....228

Function Definitions....228

Functions That Return a Boolean Value....234

Alternate Form for Function Declarations....234

Pitfall: Arguments in the Wrong Order....235

Function Definition–Syntax Summary....236

More About Placement of Function Definitions....237

Programming Tip: Use Function Calls in Branching Statements....238

4.4 PROCEDURAL ABSTRACTION....239

The Black-Box Analogy....239

Programming Tip: Choosing Formal Parameter Names....242

Programming Tip: Nested Loops....243

Case Study: Buying Pizza....246

Programming Tip: Use Pseudocode....252

4.5 SCOPE AND LOCAL VARIABLES....253

The Small Program Analogy....253

Programming Example: Experimental Pea Patch....256

Global Constants and Global Variables....256

Call-by-Value Formal Parameters Are Local Variables....259

Block Scope....261

Namespaces Revisited....262

Programming Example: The Factorial Function....265

4.6 OVERLOADING FUNCTION NAMES....267

Introduction to Overloading....267

Programming Example: Revised Pizza-Buying Program....270

Automatic Type Conversion....273

Chapter Summary....275

Answers to Self-Test Exercises....275

Practice Programs....280

Programming Projects....282

Chapter 5 Functions for All Subtasks....286

5.1 VOID FUNCTIONS....287

Definitions of void Functions....287

Programming Example: Converting Temperatures....290

return Statements in void Functions....290

5.2 CALL-BY-REFERENCE PARAMETERS....294

A First View of Call-by-Reference....294

Call-by-Reference in Detail....297

Programming Example: The swapValues Function....302

Mixed Parameter Lists....303

Programming Tip: What Kind of Parameter to Use....304

Pitfall: Inadvertent Local Variables....305

5.3 USING PROCEDURAL ABSTRACTION....308

Functions Calling Functions....308

Preconditions and Postconditions....310

Case Study: Supermarket Pricing....311

5.4 TESTING AND DEBUGGING FUNCTIONS....316

Stubs and Drivers....317

5.5 GENERAL DEBUGGING TECHNIQUES....322

Keep an Open Mind....322

Check Common Errors....322

Localize the Error....323

The assert Macro....325

Chapter Summary....327

Answers to Self-Test Exercises....328

Practice Programs....331

Programming Projects....334

Chapter 6 I/O Streams as an Introduction to Objects and Classes....342

6.1 STREAMS AND BASIC FILE I/O....343

Why Use Files for I/O?....344

File I/O....345

Introduction to Classes and Objects....349

Programming Tip: Check Whether a File Was Opened Successfully....351

Techniques for File I/O....353

Appending to a File (Optional)....357

File Names as Input (Optional)....358

6.2 TOOLS FOR STREAM I/O....360

Formatting Output with Stream Functions....360

Manipulators....366

Streams as Arguments to Functions....369

Programming Tip: Checking for the End of a File....369

A Note on Namespaces....372

Programming Example: Cleaning Up a File Format....373

6.3 CHARACTER I/O....375

The Member Functions get and put....375

The putback Member Function (Optional)....379

Programming Example: Checking Input....380

Pitfall: Unexpected '\n' in Input....382

Programming Example: Another newLine Function....384

Default Arguments for Functions (Optional)....385

The eof Member Function....390

Programming Example: Editing a Text File....392

Predefined Character Functions....393

Pitfall: toupper and tolower Return Values....395

Chapter Summary....397

Answers to Self-Test Exercises....398

Practice Programs....405

Programming Projects....407

Chapter 7 Arrays....414

7.1 INTRODUCTION TO ARRAYS....415

Declaring and Referencing Arrays....415

Programming Tip: Use for Loops with Arrays....417

Pitfall: Array Indexes Always Start with Zero....417

Programming Tip: Use a Defined Constant for the Size of an Array....417

Arrays in Memory....419

Pitfall: Array Index Out of Range....420

Initializing Arrays....423

Programming Tip: C++11 Range-Based for Statement....423

7.2 ARRAYS IN FUNCTIONS....426

Indexed Variables as Function Arguments....426

Entire Arrays as Function Arguments....428

The const Parameter Modifier....431

Pitfall: Inconsistent Use of const Parameters....434

Functions That Return an Array....434

Case Study: Production Graph....435

7.3 PROGRAMMING WITH ARRAYS....448

Partially Filled Arrays....448

Programming Tip: Do Not Skimp on Formal Parameters....451

Programming Example: Searching an Array....451

Programming Example: Sorting an Array....454

Programming Example: Bubble Sort....458

7.4 MULTIDIMENSIONAL ARRAYS....461

Multidimensional Array Basics....462

Multidimensional Array Parameters....462

Programming Example: Two-Dimensional Grading Program....464

Pitfall: Using Commas Between Array Indexes....468

Chapter Summary....469

Answers to Self-Test Exercises....470

Practice Programs....474

Programming Projects....476

Chapter 8 Strings and Vectors....488

8.1 AN ARRAY TYPE FOR STRINGS....490

C-String Values and C-String Variables....490

Pitfall: Using = and == with C Strings....493

Other Functions in ....495

Pitfall: Copying past the end of a C-string using strcpy....498

C-String Input and Output....501

C-String-to-Number Conversions and Robust Input....503

8.2 THE STANDARD STRING CLASS....509

Introduction to the Standard Class string....509

I/O with the Class string....512

Programming Tip: More Versions of getline....515

Pitfall: Mixing cin >> variable; and getline ....516

String Processing with the Class string....517

Programming Example: Palindrome Testing....521

Converting between string Objects and C Strings....524

Converting Between Strings and Numbers....525

8.3 VECTORS....526

Vector Basics....526

Pitfall: Using Square Brackets Beyond the Vector Size....529

Programming Tip: Vector Assignment Is Well Behaved....530

Efficiency Issues....530

Chapter Summary....532

Answers to Self-Test Exercises....532

Practice Programs....534

Programming Projects....535

Chapter 9 Pointers and Dynamic Arrays....544

9.1 POINTERS....545

Pointer Variables....546

Basic Memory Management....553

Pitfall: Dangling Pointers....554

Static Variables and Automatic Variables....555

Programming Tip: Define Pointer Types....555

9.2 DYNAMIC ARRAYS....558

Array Variables and Pointer Variables....558

Creating and Using Dynamic Arrays....559

Pointer Arithmetic (Optional)....565

Multidimensional Dynamic Arrays (Optional)....567

Chapter Summary....569

Answers to Self-Test Exercises....569

Practice Programs....570

Programming Projects....571

Chapter 10 Defining Classes....578

10.1 STRUCTURES....579

Structures for Diverse Data....579

Pitfall: Forgetting a Semicolon in a Structure Definition....584

Structures as Function Arguments....585

Programming Tip: Use Hierarchical Structures....586

Initializing Structures....588

10.2 CLASSES....591

Defining Classes and Member Functions....591

Public and Private Members....596

Programming Tip: Make All Member Variables Private....604

Programming Tip: Define Accessor and Mutator Functions....604

Programming Tip: Use the Assignment Operator with Objects....606

Programming Example: BankAccount Class—Version 1....607

Summary of Some Properties of Classes....611

Constructors for Initialization....613

Programming Tip: Always Include a Default Constructor....621

Pitfall: Constructors with No Arguments....622

Member Initializers and Constructor Delegation in C++11....624

10.3 ABSTRACT DATA TYPES....625

Classes to Produce Abstract Data Types....626

Programming Example: Alternative Implementation of a Class....630

10.4 INTRODUCTION TO INHERITANCE....635

Derived Classes....636

Defining Derived Classes....637

Chapter Summary....641

Answers to Self-Test Exercises....642

Practice Programs....648

Programming Projects....649

Chapter 11 Friends, Overloaded Operators, and Arrays in Classes....656

11.1 FRIEND FUNCTIONS....657

Programming Example: An Equality Function....657

Friend Functions....661

Programming Tip: Define Both Accessor Functions and Friend Functions....663

Programming Tip: Use Both Member and Nonmember Functions....665

Programming Example: Money Class (Version 1)....665

Implementation of digitToInt (Optional)....672

Pitfall: Leading Zeros in Number Constants....673

The const Parameter Modifier....675

Pitfall: Inconsistent Use of const....676

11.2 OVERLOADING OPERATORS....680

Overloading Operators....681

Constructors for Automatic Type Conversion....684

Overloading Unary Operators....686

Overloading >> and <<....687

11.3 ARRAYS AND CLASSES....697

Arrays of Classes....697

Arrays as Class Members....701

Programming Example: A Class for a Partially Filled Array....702

11.4 CLASSES AND DYNAMIC ARRAYS....704

Programming Example: A String Variable Class....705

Destructors....708

Pitfall: Pointers as Call-by-Value Parameters....711

Copy Constructors....712

Overloading the Assignment Operator....717

Chapter Summary....720

Answers to Self-Test Exercises....720

Practice Programs....730

Programming Projects....731

Chapter 12 Separate Compilation and Namespaces....740

12.1 SEPARATE COMPILATION....741

ADTs Reviewed....742

Case Study: DigitalTime—A Class Compiled Separately....743

Using #ifndef....752

Programming Tip: Defining Other Libraries....755

12.2 NAMESPACES....756

Namespaces and using Directives....757

Creating a Namespace....758

Qualifying Names....761

A Subtle Point About Namespaces (Optional)....762

Unnamed Namespaces....763

Programming Tip: Choosing a Name for a Namespace....768

Pitfall: Confusing the Global Namespace and the Unnamed Namespace....769

Chapter Summary....770

Answers to Self-Test Exercises....771

Practice Programs....773

Programming Projects....775

Chapter 13 Pointers and Linked Lists....776

13.1 NODES AND LINKED LISTS....777

Nodes....777

nullptr....782

Linked Lists....783

Inserting a Node at the Head of a List....784

Pitfall: Losing Nodes....787

Searching a Linked List....788

Pointers as Iterators....792

Inserting and Removing Nodes Inside a List....792

Pitfall: Using the Assignment Operator with Dynamic Data Structures....794

Variations on Linked Lists....797

Linked Lists of Classes....799

13.2 STACKS AND QUEUES....802

Stacks....802

Programming Examples: A Stack Class....803

Queues....808

Programming Examples: A Queue Class....809

Chapter Summary....813

Answers to Self-Test Exercises....813

Practice Programs....816

Programming Projects....817

Chapter 14 Recursion....826

14.1 RECURSIVE FUNCTIONS FOR TASKS....828

Case Study: Vertical Numbers....828

A Closer Look at Recursion....834

Pitfall: Infinite Recursion....836

Stacks for Recursion....837

Pitfall: Stack Overflow....839

Recursion Versus Iteration....839

14.2 RECURSIVE FUNCTIONS FOR VALUES....841

General Form for a Recursive Function That Returns a Value....841

Programming Example: Another Powers Function....841

14.3 THINKING RECURSIVELY....846

Recursive Design Techniques....846

Case Study: Binary Search—An Example of Recursive Thinking....847

Programming Example: A Recursive Member Function....855

Chapter Summary....859

Answers to Self-Test Exercises....859

Practice Programs....864

Programming Projects....864

Chapter 15 Inheritance....870

15.1 INHERITANCE BASICS....871

Derived Classes....874

Constructors in Derived Classes....882

Pitfall: Use of Private Member Variables from the Base Class....885

Pitfall: Private Member Functions Are Effectively Not Inherited....887

The protected Qualifier....887

Redefinition of Member Functions....890

Redefining Versus Overloading....893

Access to a Redefined Base Function....895

15.2 INHERITANCE DETAILS....896

Functions That Are Not Inherited....896

Assignment Operators and Copy Constructors in Derived Classes....897

Destructors in Derived Classes....898

15.3 POLYMORPHISM....899

Late Binding....900

Virtual Functions in C++....901

Virtual Functions and Extended Type Compatibility....906

Pitfall: The Slicing Problem....910

Pitfall: Not Using Virtual Member Functions....911

Pitfall: Attempting to Compile Class Definitions Without Definitions for Every Virtual Member Function....912

Programming Tip: Make Destructors Virtual....912

Chapter Summary....914

Answers to Self-Test Exercises....914

Practice Programs....918

Programming Projects....921

Chapter 16 Exception Handling....930

16.1 EXCEPTION-HANDLING BASICS....932

A Toy Example of Exception Handling....932

Defining Your Own Exception Classes....941

Multiple Throws and Catches....941

Pitfall: Catch the More Specific Exception First....945

Programming Tip: Exception Classes Can Be Trivial....946

Throwing an Exception in a Function....946

Exception Specification....948

Pitfall: Exception Specification in Derived Classes....950

16.2 PROGRAMMING TECHNIQUES FOR EXCEPTION HANDLING....951

When to Throw an Exception....951

Pitfall: Uncaught Exceptions....953

Pitfall: Nested try-catch Blocks....953

Pitfall: Overuse of Exceptions....953

Exception Class Hierarchies....954

Testing for Available Memory....954

Rethrowing an Exception....955

Chapter Summary....955

Answers to Self-Test Exercises....955

Practice Programs....957

Programming Projects....958

Chapter 17 Templates....962

17.1 TEMPLATES FOR ALGORITHM ABSTRACTION....963

Templates for Functions....964

Pitfall: Compiler Complications....968

Programming Example: A Generic Sorting Function....970

Programming Tip: How to Define Templates....974

Pitfall: Using a Template with an Inappropriate Type....975

17.2 TEMPLATES FOR DATA ABSTRACTION....976

Syntax for Class Templates....976

Programming Example: An Array Class....979

Chapter Summary....986

Answers to Self-Test Exercises....986

Practice Programs....990

Programming Projects....990

Chapter 18 Standard Template Library and C++11....994

18.1 ITERATORS....996

using Declarations....996

Iterator Basics....997

Programming Tip: Use auto to Simplify Variable Declarations....1001

Pitfall: Compiler Problems....1001

Kinds of Iterators....1003

Constant and Mutable Iterators....1007

Reverse Iterators....1008

Other Kinds of Iterators....1009

18.2 CONTAINERS....1010

Sequential Containers....1011

Pitfall: Iterators and Removing Elements....1015

Programming Tip: Type Definitions in Containers....1016

Container Adapters stack and queue....1016

Associative Containers set and map....1020

Programming Tip: Use Initialization, Ranged for, and auto with Containers....1027

Efficiency....1027

18.3 GENERIC ALGORITHMS....1028

Running Times and Big-O Notation....1029

Container Access Running Times....1032

Nonmodifying Sequence Algorithms....1034

Container Modifying Algorithms....1038

Set Algorithms....1040

Sorting Algorithms....1041

18.4 C++ IS EVOLVING....1042

std::array....1042

Regular Expressions....1043

Threads....1048

Smart Pointers....1054

Chapter Summary....1060

Answers to Self-Test Exercises....1061

Practice Programs....1062

Programming Projects....1064

APPENDICES....1070

1 C++ Keywords....1070

2 Precedence of Operators....1071

3 The ASCII Character Set....1073

4 Some Library Functions....1074

5 Inline Functions....1081

6 Overloading the Array Index Square Brackets....1082

7 The this Pointer....1084

8 Overloading Operators as Member Operators....1087

CREDITS....1089

INDEX....1092

A....1093

B....1095

C....1096

D....1099

E....1101

F....1101

G....1103

H....1103

I....1103

L....1106

M....1107

N....1108

O....1109

P....1109

Q....1112

R....1112

S....1112

T....1114

U....1115

V....1115

W....1116

Z....1116

This book is meant to be used in a first course in programming and computer science using the C++ language. It assumes no previous programming experience and no mathematics beyond high school algebra.

If you have used the previous edition of this book, you should read the following section that explains the changes to this tenth edition and then you can skip the rest of this preface. If you are new to this book, the rest of this preface will give you an overview of the book.

Changes to the Tenth Edition

 This tenth edition presents the same programming philosophy as the ninth edition. All of the material from the ninth edition remains, but with the following enhancements:

  •  Consistent use of camelCase notation rather than underscore_case throughout the text.
  • Discussion in Chapter 10 of shallow vs. deep copy.
  • Additional material in Chapter 12 and 17 on compiling templates with header files.
  • Additional material in Chapter 18 on the std::array class, regular expressions, threads, and smart pointers in C++11.
  • Correction of errata and edits for clarity such as indicating preferred methods for file I/O, naming of terminology, better definition of encapsulation, and removing material that is now standard in C++11 and higher.
  • Ten new Programming Projects.
  • Five new VideoNotes for a total of sixty nine VideoNotes. These VideoNotes walk students through the process of both problem solving and coding to help reinforce key programming concepts. An icon appears in the margin of the book when a VideoNote is available regarding the topic covered in the text.

If you are an instructor already using the ninth edition, you can continue to teach your course almost without change.


Похожее:

Список отзывов:

Нет отзывов к книге.