Summary

Offers complete coverage of the C++ programming language.This title offers provides all the tools necessary for experienced and novice programmers to master C++, including: thorough coverage of the Standard Template Library; complete and fully executable code throughout; sections highlighting programming tips and common pitfalls; and a logical order of coverage of C++ topics in order for readers to better understand the language.This book is appropriate for anyone interested in learning how to programming using the C++ programming language.

Table of Contents

C++ Basics

1

(44)

Introduction to C++

2

(4)

Origins of the C++ Language

2

(1)

C++ and Object-Oriented Programming

3

(1)

The Character of C++

3

(1)

C++ Terminology

4

(1)

A Sample C++ Program

4

(2)

Variables, Expressions, and Assignment Statements

6

(22)

Identifiers

6

(2)

Variables

8

(2)

Assignment Statements

10

(2)

Pitfall: Uninitialized Variables

12

(1)

Tip: Use Meaningful Names

13

(1)

More Assignment Statements

13

(1)

Assignment Compatibility

14

(1)

Literals

15

(2)

Escape Sequences

17

(1)

Naming Constants

17

(2)

Arithmetic Operators and Expressions

19

(2)

Integer and Floating-Point Division

21

(1)

Pitfall: Division and Whole Number

22

(1)

Type Casting

23

(2)

Increment and Decrement Operators

25

(2)

Pitfall: Order of Evaluation

27

(1)

Console Input/Output

28

(7)

Output Using cout

28

(1)

New Lines in Output

29

(1)

Tip: End Each Program with \n or endl

30

(1)

Formatting for Numbers with a Decimal Point

31

(1)

Output with cerr

32

(1)

Input Using cin

32

(2)

Tip: Line Breaks in I/O

34

(1)

Program Style

35

(1)

Comments

35

(1)

Libraries and Namespaces

36

(9)

Libraries and include Directives

36

(1)

Namespaces

37

(1)

Pitfall: Problems with Library Names

38

(1)

Chapter Summary

38

(1)

Answers to Self-Test Exercises

39

(2)

Programming Projects

41

(4)

Flow of Control

45

(50)

Boolean Expression

46

(11)

Building Boolean Expressions

46

(1)

Pitfall: Strings of Inequalities

47

(1)

Evaluating Boolean Expressions

48

(2)

Precedence Rule

50

(4)

Pitfall: Integer Values Can Be Used as Boolean Values

54

(3)

Branching Mechanisms

57

(12)

if-else Statements

57

(1)

Compound Statements

57

(2)

Pitfall: Using = in Place of = =

59

(2)

Omitting the else

61

(1)

Nested Statement

62

(1)

Multiway if-else Statement

62

(1)

The switch Statement

62

(4)

Pitfall: Forgetting a break in a switch Statement

66

(1)

Tip: Use switch Statements for Menu

66

(1)

Enumeration Types

66

(1)

The Conditional Operation

67

(2)

Loops

69

(26)

The while and do-while Statements

69

(3)

Increment and Decrement Operators Revisited

72

(3)

The Comma Operator

75

(1)

The for Statement

76

(3)

Tip: Repeat-N-Times Loops

79

(1)

Pitfall: Extra Semicolon in a for Statement

79

(1)

Pitfall: Infinite Loops

80

(3)

The break and continue Statement

83

(3)

Nested Loops

86

(1)

Chapter Summary

86

(1)

Answers to Self-Test Exercises

87

(5)

Programming Projects

92

(3)

Function Basics

95

(44)

Predefined Functions

96

(12)

Predefined Functions That Return a Value

96

(5)

Predefined void Functions

101

(2)

A Random Number Generator

103

(5)

Programmer-Defined Functions

108

(13)

Defining Functions That Return a Value

108

(3)

Alternate Form for Function Declarations

111

(1)

Pitfall: Arguments in the Wrong Order

111

(1)

Pitfall: Use the Terms Parameter and Argument

111

(1)

Functions Calling Functions

112

(1)

Example: A Rounding Function

112

(2)

Functions That Return a Boolean Value

114

(1)

Defining void Functions

115

(2)

return Statements in void Functions

117

(2)

Preconditions and Postconditions

119

(1)

main is a Function

119

(1)

Recursive Functions

120

(1)

Scope Rules

121

(18)

Local Variables

122

(1)

Procedural Abstraction

122

(3)

Global Constants and Global Variables

125

(3)

Blocks

128

(1)

Nested Scopes

129

(1)

Tip: Use Function Calls in Branching and Loop Statements

129

(1)

Variables Declared in a for Loop

130

(1)

Chapter Summary

131

(1)

Answers to Self-Test Exercises

131

(4)

Programming Projects

135

(4)

Parameters and Overloading

139

(42)

Parameters

140

(18)

Call-by-Value Parameters

140

(3)

A First Look at Call-by-Reference Parameters

143

(2)

Call-by-Reference Mechanism in Detail

145

(2)

Constant Reference Parameters

147

(1)

Example: The swap Values Function

148

(1)

Tip: Think of Actions, Not Code

149

(1)

Mixed Parameter Lists

150

(1)

Tip: What Kind of Parameter to Use

151

(1)

Pitfall: Inadvertent Local Variables

152

(3)

Tip: Choosing Formal Parameter Names

155

(1)

Example: Buying Pizza

155

(3)

Overloading and Default Arguments

158

(11)

Introduction to Overloading

159

(2)

Pitfall: Automatic Type Conversion and Overloading

161

(2)

Rules for Resolving Overloading

163

(1)

Example: Revised Pizza-Buying Program

164

(3)

Default Arguments

167

(2)

Testing and Debugging Functions

169

(12)

The assert Macro

169

(1)

Stubs and Drivers

170

(4)

Chapter Summary

174

(1)

Answers to Self-Test Exercises

174

(2)

Programming Projects

176

(5)

Arrays

181

(54)

Introduction to Arrays

182

(9)

Declaring and Referencing Arrays

182

(3)

Tip: Use for Loops with Arrays

185

(1)

Pitfall: Array Indexes Always Start with Zero

185

(1)

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

185

(1)

Arrays in Memory

186

(1)

Pitfall: Array Index Out of Range

187

(2)

Initializing Arrays

189

(2)

Arrays in Functions

191

(13)

Indexed Variables as Function Arguments

191

(2)

Entire Arrays as Function Arguments

193

(3)

The const Parameter Modifier

196

(2)

Pitfall: Inconsistent Use of const Parameters

198

(1)

Functions That Return an Array

198

(1)

Example: Production Graph

199

(5)

Programming With Arrays

204

(10)

Partially Filled Arrays

204

(1)

Tip: Do Not Skimp on Formal Parameters

205

(2)

Example: Searching an Array

207

(3)

Example: Sorting an Array

210

(4)

Multidimensional Arrays

214

(21)

Multidimensional Array Basics

214

(2)

Multidimensional Array Parameters

216

(1)

Example: Two-Dimensional Grading Program

217

(5)

Chapter Summary

222

(1)

Answers to Self-Test Exercises

223

(4)

Programming Projects

227

(8)

Structures and Classes

235

(36)

Structures

236

(13)

Structure Types

238

(4)

Pitfall: Forgetting a Semicolon in a Structure Definition

242

(1)

Structures as Function Arguments

242

(1)

Tip: Use Hierarchical Structures

243

(3)

Initializing Structures

246

(3)

Classes

249

(22)

Defining Classes and Member Functions

249

(5)

Encapsulation

254

(1)

Public and Private Members

255

(4)

Accessor and Mutator Functions

259

(2)

Tip: Separate Interface and Implementation

261

(1)

Tip: A Test for Encapsulation

262

(1)

Structures versus Classes

263

(2)

Tip: Thinking Objects

265

(1)

Chapter Summary

265

(1)

Answers to Self-Test Exercises

266

(2)

Programming Projects

268

(3)

Constructors and Other Tools

271

(46)

Constructors

272

(19)

Constructor Definitions

272

(5)

Pitfall: Constructors with No Arguments

277

(2)

Explicit Constructor Calls

279

(1)

Tip: Always Include a Default Constructor

279

(3)

Example: BankAccount Class

282

(6)

Class Type Member Variables

288

(3)

More Tools

291

(13)

The const Parameter Modifier

292

(2)

Pitfall: Inconsistent Use of const

294

(4)

Inline Functions

298

(1)

Static Members

299

(5)

Nested and Local Class Definitions

304

(1)

Vectors---A Previews of the Standard Template Library

304

(13)

Vector Basics

305

(3)

Pitfall: Using Square Brackets beyond the Vector Size

308

(1)

Tip: Vector Assignment Is Well Behaved

309

(1)

Efficiency Issues

309

(2)

Chapter Summary

311

(1)

Answers to Self-Test Exercises

311

(3)

Programming Projects

314

(3)

Operator Overloading, Friends, and References

317

(50)

Basic Operator Overloading

318

(16)

Overloading Basics

319

(5)

Tip: A Constructor Can Return an Object

324

(1)

Returning by const Value

325

(3)

Tip: Returning Member Variables of a Class Type

328

(1)

Overloading Unary Operators

329

(1)

Overloading as Members Functions

330

(2)

Tip: A Class Has Access to All Its Objects

332

(1)

Overloading Function Application ( )

333

(1)

Pitfall: Overloading &&, ||, and the Comma Operator

334

(1)

Friend Functions and Automatic Type Conversion

334

(8)

Constructors for Automatic Type Conversion

334

(1)

Pitfall: Member Operators and Automatic Type Conversion

335

(1)

Friend Functions

336

(4)

Pitfall: Compilers without Friends

340

(1)

Friend Classes

340

(2)

References and More Overloaded Operators

342

(25)

References

342

(1)

Pitfall: Returning a Reference to Certain Member Variables

343

(1)

Overloading >> and <<

344

(7)

Tip: What Mode of Returned Value to Use

351

(2)

The Assignment Operator

353

(1)

Overloading the Increment and Decrement Operators

354

(3)

Overloading the Array Operator [ ]

357

(2)

Overloading Based on L-Value versus R-Value

359

(1)

Chapter Summary

359

(1)

Answers to Self-Test Exercises

360

(2)

Programming Projects

362

(5)

Strings

367

(54)

An Array Type for Strings

368

(14)

C-String Values and C-String Variables

369

(4)

Pitfall: Using = and = = with C-Strings

373

(2)

Other Functions in <cstring>

375

(4)

C-String Input and Output

379

(3)

Character Manipulation Tools

382

(12)

Character I/O

382

(1)

The Member Functions get and put

382

(3)

Example: Checking Input Using a Newline Function

385

(1)

Pitfall: Unexpected `\n' in Input

386

(2)

The putback, peek, and ignore Member Functions

388

(2)

Character-Manipulating Functions

390

(2)

Pitfall: toupper and to lower Return int Values

392

(2)

The Standard Class string

394

(27)

Introduction to the Standard Class string

394

(2)

I/O with the Class string

396

(5)

Tip: More Versions of getline

401

(1)

Pitfall: Mixing cin >> variable; and getline

401

(1)

String Processing with the Class string

402

(4)

Example: Palindrome Testing

406

(5)

Converting between string Objects and C-Strings

411

(1)

Chapter Summary

411

(1)

Answers to Self-Test Exercises

412

(3)

Programming Projects

415

(6)

Pointers and Dynamic Arrays

421

(54)

Pointers

422

(17)

Pointer Variables

423

(8)

Basic Memory Management

431

(3)

Pitfall: Dangling Pointers

434

(1)

Dynamic Variables and Automatic Variables

434

(1)

Tip: Define Pointer Types

435

(2)

Pitfall: Pointers as Call-by-Value Parameters

437

(2)

Uses for Pointers

439

(1)

Dynamic Arrays

439

(12)

Array Variables and Pointer Variables

439

(2)

Creating and Using Dynamic Arrays

441

(4)

Example: A Function That Returns an Array

445

(2)

Pointer Arithmetic

447

(1)

Multidimensional Dynamic Arrays

448

(3)

Classes, Pointers, and Dynamic Arrays

451

(24)

The -< Operator

451

(1)

The this Pointer

452

(1)

Overloading the Assignment Operator

453

(3)

Example: A Class for Partially Filled Arrays

456

(7)

Destructors

463

(2)

Copy Constructors

465

(4)

Chapter Summary

469

(1)

Answers to Self-Test Exercises

470

(2)

Programming Projects

472

(3)

Separate Compilation and Namespaces

475

(44)

Separate Compilation

476

(15)

Encapsulation Reviewed

477

(1)

Header Files and Implementation Files

478

(8)

Example: DigitalTime Class

486

(1)

Tip: Reusable Components

487

(1)

Using #ifndef

487

(3)

Tip: Defining Other Libraries

490

(1)

Namespaces

491

(28)

Namespaces and using Directives

491

(2)

Creating a Namespace

493

(3)

Using Declarations

496

(2)

Qualifying Names

498

(2)

Tip: Choosing a Name for a Namespace

500

(1)

Example: A Class Definition in a Namespace

501

(1)

Unnamed Namespaces

502

(7)

Pitfall: Confusing the Global Namespace and the Unnamed Namespace

509

(1)

Tip: Unnamed Namespaces Replace the static Qualifer

509

(1)

Tip: Hiding Helping Functions

509

(1)

Nested Namespaces

510

(1)

Tip: What Namespace Specification Should You Use?

510

(4)

Chapter Summary

514

(1)

Answers to Self-Test Exercises

514

(2)

Programming Projects

516

(3)

Streams and File I/O

519

(52)

I/O Streams

521

(16)

File I/O

521

(5)

Pitfall: Restrictions on Stream Variables

526

(1)

Appending to a File

526

(2)

Tip: Another Syntax for Opening a File

528

(1)

Tip: Check That a File Was Opened Successfully

529

(3)

Character I/O

532

(1)

Checking for the End of a File

533

(4)

Tools for Stream I/O

537

(14)

File Names as Input

537

(1)

Formatting Output with Stream Functions

538

(4)

Manipulators

542

(1)

Saving Flag Settings

543

(1)

More Output Stream Member Functions

544

(2)

Example: Cleaning Up a File Format

546

(2)

Example: Editing a Text File

548

(3)

Stream Hierarchies: A Preview of Inheritance

551

(6)

Inheritance among Stream Classes

551

(2)

Example: Another new Line Function

553

(4)

Random Access to Files

557

(14)

Chapter Summary

559

(1)

Answers to Self-Test Exercises

559

(3)

Programming Projects

562

(9)

Recursion

571

(38)

Recursive void Functions

573

(12)

Example: Vertical Numbers

573

(3)

Tracing a Recursive Call

576

(3)

A Closer Look at Recursion

579

(1)

Pitfall: Infinite Recursion

580

(2)

Stacks for Recursion

582

(2)

Pitfall: Stack Overflow

584

(1)

Recursion versus Iteration

584

(1)

Recursive Functions That Return a Value

585

(6)

General Form for a Recursive Function That Returns a Value

585

(1)

Example: Another Powers Function

586

(5)

Thinking Recursively

591

(18)

Recursive Design Techniques

591

(1)

Binary Search

592

(9)

Chapter Summary

601

(1)

Answers to Self-Test Exercises

601

(5)

Programming Projects

606

(3)

Inheritance

609

(46)

Inheritance Basics

610

(23)

Derived Classes

610

(10)

Constructors in Derived Classes

620

(2)

Pitfall: Use of Private Member Variables from the Base Class

622

(2)

Pitfall: Private Member Functions Are Effectively Not Inherited

624

(1)

The protected Qualifier

624

(3)

Redefinition of Member Functions

627

(2)

Redefining versus Overloading

629

(1)

Access to a Redefined Base Function

630

(1)

Functions That Are Not Inherited

631

(2)

Programming With Inheritance

633

(22)

Assignment Operators and Copy Constructors in Derived Classes

633

(1)

Destructors in Derived Classes

634

(1)

Example: Partially Filled Array with Backup

634

(9)

Pitfall: Same Object on Both Sides of the Assignment Operator

643

(1)

Example: Alternate Implementation of PFArrayDBak

644

(3)

Tip: A Class Has Access to Private Members of All Objects of the Class