Problem Solving with C++

Why Rent from Knetbooks?

Because Knetbooks knows college students. Our rental program is designed to save you time and money. Whether you need a textbook for a semester, quarter or even a summer session, we have an option for you. Simply select a rental period, enter your information and your book will be on its way!

Introductory Programming / C++ Problem Solving with C++, Sixth Edition Walter Savitch Walter Savitchrs"s Problem Solving with C++ is the most widely used textbook for the introduction to programming in C++ course. These are just a few of the reasons why: "My students and I love this textbook. Savitch makes the material so accessible, and he does it with a great sense of humor that we all enjoy. My students tell me that they finally have purchased a college textbook where theyrs"ve gotten their full moneyrs"s worth." Jennifer Perkins, University of Arkansas at Little Rock "Our school has used the Savitch text for many years, and it has been well received by both faculty and students. Walter Savitch explains difficult programming concepts in a clear and concise manner and discusses all the important features of the C++ language." Carol Roberts, University of Maine "Writing a book is an art if, and only if, it can create an artist. Savitchrs"s book does just this. It contains fundamental materials presented in a pleasant way in which not only the flow consistency, but also the example consistency, is preserved." Coskun Bayrak, University of Arkansas at Little Rock "The progression from programming basics to object-oriented concepts is logical and effectively leads beginning C++ students to an understanding of classes and more advanced topics." Stephen Weissman, Burlington County College This Sixth Edition features: bull; Savitchrs"s unparalleled clear and concise writing style bull; Extensive use of examples, exercises, and projects to promote good programming practice bull; Earlier coverage of loops and arrays bull; Enhanced discussion of debugging bull; All code updated to be ANSI/ISO compliant bull; Two new programming projects per chapter MyCodeMate is a web-based, textbook-specific homework tool and programming resource for an introduction to programming course. It provides a wide rangeof tools that students can use to help them learn programming concepts, prepare for tests, and earn better grades in the introductory programming course. Students can work on programming problems from this text or homework problems created by their professors, and receive guided hints with page references and English explanations of compiler errors. Instructors can assign textbook-specific or self-created homework problems, preset style attributes, view studentsrs" code and class compiler error logs, and track homework completion. A complimentary subscription is offered when an access code is ordered packaged with a new copy of this text. Subscriptions may also be purchased online. For more information visit www.myCodeMate.com.

Introduction to Computers and C++ Programming

1

(38)

Computer Systems

2

(10)

Hardware

2

(5)

Software

7

(1)

High-Level Languages

8

(1)

Compilers

9

(3)

History Note

12

(1)

Programming and Problem-Solving

12

(7)

Algorithms

12

(3)

Program Design

15

(2)

Object-Oriented Programming

17

(1)

The Software Life Cycle

17

(2)

Introduction to C++

19

(11)

Origins of the C++ Language

19

(1)

A Sample C++ Program

20

(4)

Pitfall: Using the Wrong Slash in \n

24

(1)

Programming Tip: Input and Output Syntax

24

(1)

Layout of a Simple C++ Program

24

(2)

Pitfall: Putting a Space before the include File Name

26

(1)

Compiling and Running a C++ Program

27

(1)

Programming Tip: Getting Your Program to Run

27

(3)

Testing And Debugging

30

(9)

Kinds of Program Errors

30

(1)

Pitfall: Assuming Your Program Is Correct

31

(1)

Chapter Summary

32

(1)

Answers to Self-Test Exercises

33

(3)

Programming Projects

36

(3)

C++ Basics

39

(70)

Variables and Assignments

40

(10)

Variables

40

(2)

Names: Identifiers

42

(2)

Variable Declarations

44

(1)

Assignment Statements

45

(2)

Pitfall: Uninitialized Variables

47

(2)

Programming Tip: Use Meaningful Names

49

(1)

Input and Output

50

(10)

Output Using cout

50

(2)

Include Directives and Namespaces

52

(1)

Escape Sequences

53

(2)

Programming Tip: End Each Program with a \n or endl

55

(1)

Formatting for Numbers with a Decimal Point

55

(1)

Input Using cin

56

(2)

Designing Input and Output

58

(1)

Programming Tip: Line Breaks in I/O

58

(2)

Data Types and Expressions

60

(14)

The Types int and double

60

(2)

Other Number Types

62

(1)

The Type char

63

(1)

The Type bool

64

(1)

Introduction to the Class string

65

(1)

Type Compatibilities

66

(3)

Arithmetic Operators and Expressions

69

(2)

Pitfall: Whole Numbers in Division

71

(2)

More Assignment Statements

73

(1)

Simple Flow of Control

74

(19)

A Simple Branching Mechanism

74

(6)

Pitfall: Strings of Inequalities

80

(1)

Pitfall: Using = in place of ==

81

(1)

Compound Statements

82

(2)

Simple Loop Mechanisms

84

(3)

Increment and Decrement Operators

87

(2)

Programming Example: Charge Card Balance

89

(1)

Pitfall: Infinite Loops

90

(3)

Program Style

93

(16)

Indenting

93

(1)

Comments

94

(2)

Naming Constants

96

(2)

Chapter Summary

98

(1)

Answers to Self-Test Exercises

99

(5)

Programming Projects

104

(5)

More Flow of Control

109

(70)

Using Boolean Expressions

110

(8)

Evaluating Boolean Expressions

110

(4)

Pitfall: Boolean Expressions Convert to int Values

114

(3)

Enumeration Types (Optional)

117

(1)

Multiway Branches

118

(20)

Nested Statements

118

(1)

Programming Tip: Use Braces in Nested Statements

119

(2)

Multiway if-else Statements

121

(2)

Programming Example: State Income Tax

123

(4)

The switch Statement

127

(4)

Pitfall: Forgetting a break in a switch Statement

131

(1)

Using switch Statements for Menus

132

(1)

Blocks

132

(5)

Pitfall: Inadvertent Local Variables

137

(1)

More About C++ Loop Statements

138

(16)

The while Statements Reviewed

139

(1)

Increment and Decrement Operators Revisited

140

(3)

The for Statement

143

(5)

Pitfall: Extra Semicolon in a for Statement

148

(1)

What Kind of Loop to Use

149

(2)

Pitfall: Uninitialized Variables and Infinite Loops

151

(1)

The break Statement

151

(2)

Pitfall: The break Statement in Nested Loops

153

(1)

Designing Loops

154

(25)

Loops for Sums and Products

154

(1)

Ending a Loop

155

(4)

Nested Loops

159

(2)

Debugging Loops

161

(3)

Chapter Summary

164

(1)

Answers to Self-Test Exercises

165

(6)

Programming Projects

171

(8)

Procedural Abstraction and Functions That Return a Value

179

(68)

Top-Down Design

180

(1)

Predefined Functions

181

(9)

Using Predefined Functions

181

(5)

Type Casting

186

(2)

Older Form of Type Casting

188

(1)

Pitfall: Integer Division Drops the Fractional Part

189

(1)

Programmer-Defined Functions

190

(12)

Function Definitions

190

(4)

Functions That Return a Boolean Value

194

(3)

Alternate Form for Function Declarations

197

(1)

Pitfall: Arguments in the Wrong Order

197

(2)

Function Definition-Syntax Summary

199

(1)

More About Placement of Function Definitions

200

(1)

Programming Tip: Use Function Calls in Branching Statements

200

(2)

Procedural Abstraction

202

(14)

The Black Box Analogy

202

(2)

Programming Tip: Choosing Formal Parameter Names

204

(2)

Programming Tip: Nested Loops

206

(3)

Case Study: Buying Pizza

209

(6)

Programming Tip: Use Pseudocode

215

(1)

Local Variables

216

(12)

The Small Program Analogy

216

(2)

Programming Example: Experimental Pea Patch

218

(1)

Global Constants and Global Variables

219

(3)

Call-by-Value Formal Parameters Are Local Variables

222

(2)

Namespaces Revisited

224

(3)

Programming Example: The Factorial Function

227

(1)

Overloading Function Names

228

(19)

Introduction to Overloading

229

(2)

Programming Example: Revised Pizza-Buying Program

231

(3)

Automatic Type Conversion

234

(3)

Chapter Summary

237

(1)

Answers to Self-Test Exercises

237

(5)

Programming Projects

242

(5)

Functions for All Subtasks

247

(52)

void Functions

248

(7)

Definitions of void Functions

248

(3)

Programming Example: Converting Temperatures

251

(1)

return Statements in void Functions

251

(4)

Call-By-Reference Parameters

255

(13)

A First View of Call-by-Reference

255

(3)

Call-by-Reference in Detail

258

(4)

Programming Example: The swap_values Function

262

(2)

Mixed Parameter Lists

264

(1)

Programming Tip: What Kind of Parameter to Use

264

(2)

Pitfall: Inadvertent Local Variables

266

(2)

Using Procedural Abstraction

268

(10)

Functions Calling Functions

269

(1)

Preconditions and Postconditions

269

(3)

Case Study: Supermarket Pricing

272

(6)

Testing and Debugging Functions

278

(5)

Stubs and Drivers

278

(5)

General Debugging Techniques

283

(16)

Keep an Open Mind

283

(1)

Check Common Errors

284

(1)

Localize the Error

284

(3)

The assert macro

287

(1)

Chapter Summary

288

(1)

Answers to Self-Test Exercises

289

(4)

Programming Projects

293

(6)

I/O Streams as an Introduction to Objects and Classes

299

(78)

Streams and Basic File I/O

300

(17)

Why Use Files for I/O?

301

(1)

File I/O

302

(4)

Introduction to Classes and Objects

306

(1)

Programming Tip: Check Whether a File Was Opened Successfully

307

(3)

Techniques for File I/O

310

(4)

Appending to a File (Optional)

314

(1)

File Names as Input (Optional)

314

(3)

Tools for Stream I/O

317

(15)

Formatting Output with Stream Functions

317

(5)

Manipulators

322

(4)

Streams as Arguments to Functions

326

(1)

Programming Tip: Checking for the End of a File

326

(4)

A Note on Namespaces

330

(1)

Programming Example: Cleaning Up a File Format

331

(1)

Character I/O

332

(19)

The Member Functions get and put

333

(3)

The putback Member Function (Optional)

336

(1)

Programming Example: Checking Input

337

(3)

Pitfall: Unexpected '\n' in Input

340

(3)

The eof Member Function

343

(3)

Programming Example: Editing a Text File

346

(1)

Predefined Character Functions

346

(3)

Pitfall: toupper and tolower Return Values

349

(2)

Inheritance

351

(26)

Inheritance Among Stream Classes

351

(4)

Programming Example: Another new_line Function

355

(1)

Default Arguments for Functions (Optional)

356

(3)

Chapter Summary

359

(2)

Answers to Self-Test Exercises

361

(6)

Programming Projects

367

(10)

Arrays

377

(70)

Introduction To Arrays

378

(9)

Declaring and Referencing Arrays

378

(2)

Programming Tip: Use for Loops with Arrays

380

(1)

Pitfall: Array Indexes Always Start with Zero

380

(1)

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

380

(2)

Arrays in Memory

382

(1)

Pitfall: Array Index Out of Range

383

(2)

Initializing Arrays

385

(2)

Arrays in Functions

387

(23)

Indexed Variables as Function Arguments

387

(3)

Entire Arrays as Function Arguments

390

(3)

The const Parameter Modifier

393

(2)

Pitfall: Inconsistent Use of const Parameters

395

(1)

Functions That Return an Array

396

(1)

Case Study: Production Graph

396

(14)

Programming With Arrays

410

(11)

Partially Filled Arrays

410

(3)

Programming Tip: Do Not Skimp on Formal Parameters

413

(1)

Programming Example: Searching an Array

414

(2)

Programming Example: Sorting an Array

416

(5)

Multidimensional Arrays

421

(26)

Multidimensional Array Basics

422

(1)

Multidimensional Array Parameters

422

(2)

Programming Example: Two-Dimensional Grading Program

424

(5)

Pitfall: Using Commas Between Array Indexes

429

(1)

Chapter Summary

429

(1)

Answers to Self-Test Exercises

430

(5)

Programming Projects

435

(12)

Strings and Vectors

447

(8)

An Array Type For Strings

449

(6)

C-String Values and C-String Variables

449

(4)

Pitfall: Using = and == with C Strings

453

(2)

Other Functions in <cstring>

455

(4)

C-String Input and Output

459

(3)

C-String-to-Number Conversions and Robust Input

462

(5)

The Standard string Class

467

(17)

Introduction to the Standard Class string

467

(3)

I/O with the Class string

470

(4)

Programming Tip: More Versions of getline

474

(1)

Pitfall: Mixing cin variable; and getline

474

(2)

String Processing with the Class string

476

(2)

Programming Example: Palindrome Testing

478

(5)

Converting between string Objects and C Strings

483

(1)

Vectors

484

(15)

Vector Basics

484

(2)

Pitfall: Using Square Brackets Beyond the Vector Size

486

(2)

Programming Tip: Vector Assignment Is Well Behaved

488

(1)

Efficiency Issues

489

(1)

Chapter Summary

490

(1)

Answers to Self-Test Exercises

491

(2)

Programming Projects

493

(6)

Pointers and Dynamic Arrays

499

(28)

Pointers

500

(13)

Pointer Variables

501

(7)

Basic Memory Management

508

(1)

Pitfall: Dangling Pointers

509

(1)

Static Variables and Automatic Variables

510

(1)

Programming Tip: Define Pointer Types

510

(3)

Dynamic Arrays

513

(14)

Array Variables and Pointer Variables

513

(1)

Creating and Using Dynamic Arrays

513

(6)

Pointer Arithmetic (Optional)

519

(2)

Multidimensional Dynamic Arrays (Optional)

521

(2)

Chapter Summary

523

(1)

Answers to Self-Test Exercises

523

(1)

Programming Projects

524

(3)

Defining Classes

527

(68)

Structures

528

(12)

Structures for Diverse Data

528

(5)

Pitfall: Forgetting a Semicolon in a Structure Definition

533

(1)

Structures as Function Arguments

534

(1)

Programming Tip: Use Hierarchical Structures

535

(2)

Initializing Structures

537

(3)

Classes

540

(33)

Defining Classes and Member Functions

540

(5)

Public and Private Members

545

(8)

Programming Tip: Make All Member Variables Private

553

(1)

Programming Tip: Define Accessor and Mutator Functions

553

(2)

Programming Tip: Use the Assignment Operator with Objects

555

(1)

Programming Example: BankAccount Class---Version 1

555

(5)

Summary of Some Properties of Classes

560

(2)

Constructors for Initialization

562

(8)

Programming Tip: Always Include a Default Constructor

570

(1)

Pitfall: Constructors with No Arguments

571

(2)

Abstract Data Types

573

(22)

Classes to Produce Abstract Data Types

574

(4)

Programming Example: Alternative Implementation of a Class

578

(4)

Chapter Summary

582

(1)

Answers to Self-Test Exercises

583

(6)

Programming Projects

589

(6)

Friends, Overloaded Operators, and Arrays in Classes

595

(86)

Friend Functions

596

(23)

Programming Example: An Equality Function

596

(4)

Friend Functions

600

(2)

Programming Tip: Define Both Accessor Functions and Friend Functions

602

(2)

Programming Tip: Use Both Member and Nonmember Functions

604

(1)

Programming Example: Money Class (Version 1)

604

(7)

Implementation of digit_to_int (Optional)

611

(1)

Pitfall: Leading Zeros in Number Constants

612

(2)

The const Parameter Modifier

614

(2)

Pitfall: Inconsistent Use of const

616

(3)

Overloading Operators

619

(18)

Overloading Operators

620

(4)

Constructors for Automatic Type Conversion

624

(2)

Overloading Unary Operators

626

(1)

Overloading >> and <<

626

(11)

Arrays and Classes

637

(8)

Arrays of Classes

637

(4)

Arrays as Class Members

641

(1)

Programming Example: A Class for a Partially Filled Array

642

(3)

Classes and Dynamic Arrays

645

(36)

Programming Example: A String Variable Class

645

(4)

Destructors

649

(2)

Pitfall: Pointers as Call-by-Value Parameters

651

(2)

Copy Constructors

653

(5)

Overloading the Assignment Operator

658

(3)

Chapter Summary

661

(1)

Answers to Self-Test Exercises

662

(10)

Programming Projects

672

(9)

Separate Compilation and Namespaces

681

(36)

Separate Compilation

682

(16)

ADTs Reviewed

683

(1)

Case Study: DigitalTime---A Class Compiled Separately

684

(9)

Using #ifndef

693

(3)

Programming Tip: Defining Other Libraries

696

(2)

Namespaces

698

(19)

Namespaces and using Directives

698

(2)

Creating a Namespace

700

(3)

Qualifying Names

703

(1)

A Subtle Point About Namespaces (Optional)

704

(1)

Unnamed Namespaces

705

(3)

Programming Tip: Choosing a Name for a Namespace

708

(2)

Pitfall: Confusing the Global Namespace and the Unnamed Namespace

710

(3)

Chapter Summary

713

(1)

Answers to Self-Test Exercises

713

(2)

Programming Projects

715

(2)

Pointers and Linked Lists

717

(46)

Nodes and Linked Lists

718

(26)

Nodes

718

(6)

Linked Lists

724

(1)

Inserting a Node at the Head of a List

725

(3)

Pitfall: Losing Nodes

728

(1)

Searching a Linked List

729

(4)

Pointers as Iterators

733

(1)

Inserting and Removing Nodes Inside a List

733

(3)

Pitfall: Using the Assignment Operator with Dynamic Data Structures

736

(2)

Variations on Linked Lists

738

(2)

Linked Lists of Classes

740

(4)

Stacks and Queues

744

(19)

Stacks

744

(1)

Programming Example: A Stack Class

745

(1)

Queues

745

(6)

Programming Example: A Queue Class

751

(4)

Chapter Summary

755

(1)

Answers to Self-Test Exercises

756

(3)

Programming Projects

759

(4)

Recursion

763

(42)

Recursive Functions for Tasks

765

(13)

Case Study: Vertical Numbers

765

(7)

A Closer Look at Recursion

772

(1)

Pitfall: Infinite Recursion

773

(2)

Stacks for Recursion

775

(1)

Pitfall: Stack Overflow

776

(1)

Recursion Versus Iteration

777

(1)

Recursive Functions for Values

778

(5)

General Form for a Recursive Function That Returns a Value

778

(1)

Programming Example: Another Powers Function

779

(4)

Thinking Recursively

783

(22)

Recursive Design Techniques

783

(2)

Case Study: Binary Search---An Example of Recursive Thinking

785

(7)

Programming Example: A Recursive Member Function

792

(5)

Chapter Summary

797

(1)

Answers to Self-Test Exercises

797

(5)

Programming Projects

802

(3)

Inheritance

805

(54)

Inheritance Basics

806

(23)

Derived Classes

807

(8)

Constructors in Derived Classes

815

(3)

Pitfall: Use of Private Member Variables from the Base Class

818

(2)

Pitfall: Private Member Functions Are Effectively Not Inherited

820

(1)

The protected Qualifier

820

(3)

Redefinition of Member Functions

823

(4)

Redefining Versus Overloading

827

(1)

Access to a Redefined Base Function

828

(1)

Inheritance Details

829

(4)

Functions That Are Not Inherited

830

(1)

Assignment Operators and Copy Constructors in Derived Classes

830

(1)

Destructors in Derived Classes

831

(2)

Polymorphism

833

(26)

Late Binding

833

(1)

Virtual Functions in C++

834

(6)

Virtual Functions and Extended Type Compatibility

840

(4)

Pitfall: The Slicing Problem

844

(1)

Pitfall: Not Using Virtual Member Functions

844

(1)

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