Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ Timesaving Techniques (2005) [eng].pdf
Скачиваний:
65
Добавлен:
16.08.2013
Размер:
8.35 Mб
Скачать

C++

Timesaving Techniques

FOR

DUMmIES

by Matthew Telles

C++

Timesaving Techniques

FOR

DUMmIES

C++

Timesaving Techniques

FOR

DUMmIES

by Matthew Telles

C++ Timesaving TechniquesFor Dummies®

Published by

Wiley Publishing, Inc.

111 River Street Hoboken, NJ 07030-5774 www.wiley.com

Copyright © 2005 by Wiley Publishing, Inc., Indianapolis, Indiana

Published by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, e-mail: brandreview@wiley.com.

Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Timesaving Techniques, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.

For technical support, please visit www.wiley.com/techsupport.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Library of Congress Control Number: 2005920299

ISBN: 0-7645-7986-X

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

1MA/RU/QS/QV/IN

About the Author

Matthew Telles is a 20-year veteran of the software-development wars. In his time, he has seen FORTRAN, COBOL, and other dinosaur languages come and go. Currently a senior software engineer for Research Systems, Inc., his days are spent finding and fixing bugs that other people have created. Besides trying to be tactful, he also enjoys working with other developers to teach the techniques he has mastered over his career. With expertise in programming, designing, documenting, and debugging applications, he has reached the pinnacle of a programmer’s existence: the ability to write his own bio blurbs for books. The author of seven other programming books, Matt lives in Lakewood, Colorado, and pines away for his beloved DEC 10.

Dedication

This book is dedicated to my friends and family, without whom I couldn’t have done it.

Author’s Acknowledgments

I would like to acknowledge my employer, Research Systems, for allowing me the time and space to work on this book. In addition, I would like to thank the following people: Carol, for being there and listening; my children, for bringing a ray of sunshine into a gloomy day; and, of course, all of the people behind the scenes as well: the editors, the marketing folk, and that nice guy who kept harassing me for stuff. (Thanks, Chris!)

Publisher’s Acknowledgments

We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/.

Some of the people who helped bring this book to market include the following:

Acquisitions, Editorial, and Media Development

Composition Services

Project Editor: Christopher Morris

Project Coordinator: Maridee Ennis

Acquisitions Editor: Katie Feltman

Layout and Graphics: Melissa Auciello-

Sr. Copy Editor: Barry Childs-Helton

Brogan,Denny Hager, Stephanie D. Jumper,

Melanee Prendergast, Jacque Roth, Heather Ryan,

Technical Editor: John Purdum

Janet Seib

Editorial Manager: Kevin Kirschner

Proofreaders: Laura Albert, Laura L. Bowman,

Media Development Manager: Laura VanWinkle

John Greenough, Leeann Harney, Arielle Mannelle,

Media Development Supervisor: Richard Graves

Joe Niesen, Carl Pierce, Dwight Ramsey, Brian Walls

Indexer: Ty Koontz

Editorial Assistant: Amanda Foxworth

 

Cartoons: Rich Tennant (www.the5thwave.com)

 

Publishing and Editorial for Technology Dummies

Richard Swadley, Vice President and Executive Group Publisher

Andy Cummings, Vice President and Publisher

Mary Bednarek, Executive Acquisitions Director

Mary C. Corder, Editorial Director

Publishing for Consumer Dummies

Diane Graves Steele, Vice President and Publisher

Joyce Pepple, Acquisitions Director

Composition Services

Gerry Fahey, Vice President of Production Services

Debbie Stailey, Director of Composition Services

Contents at a Glance

Introduction

1

Part I: Streamlining the Means

 

and Mechanics of OOP

5

Technique 1: Protecting Your Data

 

with Encapsulation

7

Technique 2: Using Abstraction

 

to Extend Functionality

12

Technique 3: Customizing a Class

 

with Virtual Functions

19

Technique 4: Inheriting Data and Functionality

23

Technique 5: Separating Rules and

 

Data from Code

30

Technique 18: Fixing Breaks with Casts

90

Technique 19: Using Pointers

 

to Member Functions

96

Technique 20: Defining Default Arguments

 

for Your Functions and Methods

101

Part IV: Classes

107

Technique 21: Creating a Complete Class

109

Technique 22: Using Virtual Inheritance

116

Technique 23: Creating Overloaded Operators 120

Technique 24: Defining Your Own new

 

and delete Handlers

128

Technique 25: Implementing Properties

136

Part II: Working with the Pre-Processor

37

Technique 26: Doing Data Validation

 

Technique 6: Handling Multiple

 

with Classes

142

Operating Systems

39

Technique 27: Building a Date Class

149

Technique 7: Mastering the Evils of Asserts

42

Technique 28: Overriding Functionality

 

 

 

 

Technique 8: Using const Instead of #define

45

with Virtual Methods

162

Technique 9: Macros and

 

Technique 29: Using Mix-In Classes

168

Why Not to Use Them

48

Part V: Arrays and Templates

173

Technique 10: Understanding sizeof

52

Part III: Types

57

Technique 30: Creating a Simple

 

Template Class

175

Technique 11: Creating Your Own Basic Types

59

Technique 31: Extending a Template Class

179

Technique 12: Creating Your Own Types

63

Technique 32: Creating Templates

 

Technique 13: Using Enumerations

70

from Functions and Methods

186

Technique 33: Working with Arrays

192

Technique 14: Creating and Using Structures

73

Technique 34: Implementing Your

 

Technique 15: Understanding Constants

77

 

Own Array Class

196

 

 

Technique 16: Scoping Your Variables

82

Technique 35: Working with

 

 

 

 

Technique 17: Using Namespaces

85

Vector Algorithms

200

Technique 36: Deleting an Array of Elements

Technique 37: Creating Arrays of Objects

Technique 38: Working with Arrays of Object Pointers

Technique 39: Implementing a Spreadsheet

Part VI: Input and Output

Technique 40: Using the Standard Streams

to Format Data

Technique 41: Reading In and

Processing Files

Technique 42: How to Read Delimited Files

Technique 43: Writing Your Objects as XML

Technique 44: Removing White Space

from Input

Technique 45: Creating a Configuration File

Part VII: Using the Built-In

Functionality

Technique 46: Creating an

Internationalization Class

Technique 47: Hashing Out Translations

Technique 48: Implementing Virtual Files

Technique 49: Using Iterators

for Your Collections

Technique 50: Overriding the Allocator

for a Collection Class

Technique 51: Using the auto_ptr Class to Avoid Memory Leaks

Technique 52: Avoiding Memory Overwrites

Technique 53:Throwing, Catching, and Re-throwing Exceptions

Technique 54: Enforcing Return Codes

Technique 55: Using Wildcards

204

Part VIII: Utilities

335

209

Technique 56: Encoding and Decoding

 

 

Data for the Web

337

213

Technique 57: Encrypting and

 

216

Decrypting Strings

343

Technique 58: Converting the

 

 

 

223

Case of a String

349

 

Technique 59: Implementing a

 

225

Serialization Interface

354

 

Technique 60: Creating a Generic

 

228

Buffer Class

360

234

Technique 61: Opening a File Using

 

240

Multiple Paths

366

 

 

246

Part IX: Debugging C++ Applications

373

Technique 62: Building Tracing

 

 

 

250

into Your Applications

375

 

Technique 63: Creating Debugging

 

263

Macros and Classes

387

Technique 64: Debugging

 

 

 

265

Overloaded Methods

399

 

 

279

Part X: The Scary (or Fun!) Stuff

405

283

Technique 65: Optimizing Your Code

407

 

 

 

Technique 66: Documenting the Data Flow

416

291

Technique 67: Creating a Simple

 

 

Locking Mechanism

420

297

Technique 68: Creating and

 

 

Using Guardian Classes

425

303

Technique 69: Working with Complex

 

307

Numbers

432

 

 

 

Technique 70: Converting Numbers to Words

439

312

Technique 71: Reducing the

 

323

Complexity of Code

447

 

 

330

Index

455

Table of Contents

Introduction

Saving Time with This Book

What’s Available on the Companion Web Site? Conventions Used in This Book

What’s In This Book

Part I: Streamlining the Means and Mechanics of OOP

Part II: Working with the Pre-Processor Part III: Types

Part IV: Classes

Part V: Arrays and Templates Part VI: Input and Output

Part VII: Using the Built-in Functionality Part VIII: Utilities

Part IX: Debugging C++ Applications Part X: The Scary (or Fun!) Stuff

Icons Used in This Book

Part I: Streamlining the Means and

Mechanics of OOP

Technique 1: Protecting Your Data

with Encapsulation

Creating and Implementing

an Encapsulated Class

Making Updates to an Encapsulated Class

Technique 2: Using Abstraction

to Extend Functionality

Creating a Mailing-List Application

Testing the Mailing-List Application

Technique 3: Customizing a Class

with Virtual Functions

Customizing a Class with Polymorphism

Testing the Virtual Function Code

Why Do the Destructors Work?

Technique 4: Inheriting Data and

Functionality

Implementing a ConfigurationFile Class

Testing the ConfigurationFile Class

Delayed Construction

1Technique 5: Separating Rules and

2

Data from Code

30

2

The cDate Class

31

2

Testing the cDate Class

35

3

Part II: Working with the Pre-Processor 37

3

Technique 6: Handling Multiple

 

3

 

Operating Systems

39

3

3

Creating the Header File

39

3

Testing the Header File

40

4

Technique 7: Mastering the Evils of Asserts

42

4

The Assert Problem

42

4

Fixing the Assert Problem

44

4

4

Technique 8: Using const Instead of #define

45

4

Using the const Construct

46

 

 

Identifying the Errors

47

5

Fixing the Errors

47

Technique 9: Macros and

 

 

 

7

Why Not to Use Them

48

Initiating a Function with a

 

 

 

7

String Macro — Almost

49

Fixing What Went Wrong with the Macro

50

10

Using Macros Appropriately

51

 

12

Technique 10: Understanding sizeof

52

Using the sizeof Function

52

12

Evaluating the Results

54

17

Using sizeof with Pointers

55

 

19

Part III: Types

57

20

Technique 11: Creating Your

 

21

Own Basic Types

59

22

Implementing the Range Class

60

 

 

Testing the Range Class

62

23

Technique 12: Creating Your Own Types

63

24

Creating the Matrix Class

64

27

Matrix Operations

65

27

 

 

xiv C++ Timesaving Techniques For Dummies

Multiplying a Matrix by a Scalar Value

Multiplying a Matrix by Scalar Values, Take 2

Testing the Matrix Class

Technique 13: Using Enumerations

Implementing the Enumeration Class

Testing the Enumeration Class

Technique 14: Creating and Using Structures

Implementing Structures

Interpreting the Output

Technique 15: Understanding Constants

Defining Constants

Implementing Constant Variables

Testing the Constant Application

Using the const Keyword

Technique 16: Scoping Your Variables

Illustrating Scope

Interpreting the Output

Technique 17: Using Namespaces

Creating a Namespace Application

Testing the Namespace Application

Technique 18: Fixing Breaks with Casts

Using Casts

Addressing the Compiler Problems

Testing the Changes

Technique 19: Using Pointers

to Member Functions

Implementing Member-Function Pointers

Updating Your Code with Member-Function Pointers

Testing the Member Pointer Code

66

Part IV: Classes

107

67

Technique 21: Creating a Complete Class

109

68

Creating a Complete Class Template

110

70

Testing the Complete Class

113

71

Technique 22: Using Virtual Inheritance

116

72

Implementing Virtual Inheritance

118

73

Correcting the Code

119

74

Technique 23: Creating Overloaded

 

75

 

Operators

120

77

Rules for Creating Overloaded Operators

121

77

Using Conversion Operators

122

78

Using Overloaded Operators

122

80

Testing the MyString Class

125

81

Technique 24: Defining Your Own

 

82

 

new and delete Handlers

128

83

Rules for Implementing new and delete Handlers 129

84

Overloading new and delete Handlers

129

 

85

Testing the Memory Allocation Tracker

133

86

Technique 25: Implementing Properties

136

88

Implementing Properties

137

 

90

Testing the Property Class

140

91

Technique 26: Doing Data Validation

 

93

with Classes

142

94

Implementing Data Validation with Classes

142

 

 

Testing Your SSN Validator Class

146

96

Technique 27: Building a Date Class

149

97

Creating the Date Class

150

 

99

Implementing the Date Functionality

152

Testing the Date Class

159

99

Some Final Thoughts on the Date Class

161

 

Technique 20: Defining Default Arguments

 

Technique 28: Overriding Functionality

 

for Your Functions and Methods

101

 

with Virtual Methods

162

Customizing the Functions We Didn’t Write

102

Creating a Factory Class

163

Customizing Functions We Wrote Ourselves

103

Testing the Factory

166

Testing the Default Code

105

Enhancing the Manager Class

167

Fixing the Problem

106

Technique 29: Using Mix-In Classes

168

 

 

 

 

Implementing Mix-In Classes

169

 

 

Compiling and Testing Your Mix-In Class

170

Table of Contents

xv

Part V: Arrays and Templates

Technique 30: Creating a Simple

Template Class

Technique 31: Extending a Template Class

Implementing Template Classes in Code

Testing the Template Classes

Using Non-class Template Arguments

Technique 32: Creating Templates

from Functions and Methods

Implementing Function Templates

Creating Method Templates

Technique 33: Working with Arrays

Using the Vector Class

Technique 34: Implementing Your

Own Array Class

Creating the String Array Class

Technique 35: Working with Vector Algorithms

Working with Vector Algorithms

Technique 36: Deleting an Array

of Elements

Examining Allocations of Arrays and Pointers

Technique 37: Creating Arrays of Objects

Technique 38: Working with Arrays of Object Pointers

Creating an Array of Heterogeneous Objects

173

Technique 41: Reading In and

 

 

Processing Files

228

175

Testing the File-Reading Code

232

Creating the Test File

233

179

Technique 42: How to Read Delimited Files

234

180

Reading Delimited Files

234

182

Testing the Code

238

184

Technique 43: Writing Your Objects as XML

240

 

186

Creating the XML Writer

241

Testing the XML Writer

243

186

Technique 44: Removing White Space

 

189

 

192

from Input

246

Technique 45: Creating a Configuration File

250

192

 

Creating the Configuration-File Class

251

196

Setting Up Your Test File

260

Testing the Configuration-File Class

260

196

 

 

 

Part VII: Using the Built-In

 

200

Functionality

263

200

Technique 46: Creating an

 

 

Internationalization Class

265

204

Building the Language Files

266

204

Creating an Input Text File

272

209

Reading the International File

272

Testing the String Reader

277

 

213

Technique 47: Hashing Out Translations

279

Creating a Translator Class

279

213

Testing the Translator Class

281

 

Technique 39: Implementing a Spreadsheet

216

Technique 48: Implementing Virtual Files

283

Creating the Column Class

217

Creating a Virtual File Class

283

Creating the Row Class

218

Testing the Virtual File Class

289

Creating the Spreadsheet Class

219

Improving Your Virtual File Class

290

Testing Your Spreadsheet

221

Technique 49: Using Iterators

 

Part VI: Input and Output

223

 

for Your Collections

291

Technique 40: Using the Standard

 

Technique 50: Overriding the Allocator

 

Streams to Format Data

225

for a Collection Class

297

Working with Streams

225

Creating a Custom Memory Allocator

298

xvi C++ Timesaving Techniques For Dummies

Technique 51: Using the auto_ptr Class to Avoid Memory Leaks

Using the auto_ptr Class

Technique 52: Avoiding Memory

Overwrites

Creating a Memory Safe Buffer Class

Technique 53: Throwing, Catching, and

Re-throwing Exceptions

Throwing and Logging Exceptions

Dealing with Unhandled Exceptions

Re-throwing Exceptions

Technique 54: Enforcing Return Codes

Technique 55: Using Wildcards

Creating the Wildcard Matching Class

Testing the Wildcard Matching Class

Part VIII: Utilities

Technique 56: Encoding and Decoding

Data for the Web

Creating the URL Codec Class

Testing the URL Codec Class

Technique 57: Encrypting and

Decrypting Strings

Implementing the Rot13 Algorithm

Testing the Rot13 Algorithm

Implementing the XOR Algorithm

Testing the XOR Algorithm

Technique 58: Converting the

Case of a String

Implementing the transform Function to Convert Strings

Testing the String Conversions

Technique 59: Implementing a

Serialization Interface

Implementing the Serialization Interface

Testing the Serialization Interface

303

Technique 60: Creating a Generic

 

Buffer Class

360

303

Creating the Buffer Class

361

 

Testing the Buffer Class

364

307

Technique 61: Opening a File

 

307

Using Multiple Paths

366

 

Creating the Multiple-Search-Path Class

367

312

Testing the Multiple-Search-Path Class

369

 

 

312

Part IX: Debugging C++ Applications

373

317

Technique 62: Building Tracing

 

319

 

into Your Applications

375

323

Implementing the Flow Trace Class

376

 

330

Testing the Flow Trace System

379

331

Adding in Tracing After the Fact

380

 

 

333

Technique 63: Creating Debugging

 

335

Macros and Classes

387

The assert Macro

387

 

337

Logging

389

Testing the Logger Class

390

338

Design by Contract

392

340

Technique 64: Debugging

 

 

 

343

Overloaded Methods

399

Adding Logging to the Application

401

344

Part X: The Scary (or Fun!) Stuff

405

345

346

Technique 65: Optimizing Your Code

407

347

Making Functions Inline

407

 

 

Avoiding Temporary Objects

408

349

Passing Objects by Reference

410

 

Postponing Variable Declarations

412

350

Choosing Initialization Instead of Assignment

413

351

Technique 66: Documenting the Data Flow

416

 

Learning How Code Operates

416

354

Testing the Properties Class

418

355

 

 

358

 

 

Table of Contents

xvii

Technique 67: Creating a Simple

 

Locking Mechanism

420

Creating the Locking Mechanism

421

Testing the Locking Mechanism

422

Technique 68: Creating and Using

 

Guardian Classes

425

Creating the File-Guardian Class

426

Testing the File-Guardian Class

430

Technique 69: Working with Complex

 

Numbers

432

Implementing the Complex Class

433

Testing the Complex Number Class

436

Technique 70: Converting

 

Numbers to Words

439

Creating the Conversion Code

440

Testing the Conversion Code

446

Technique 71: Reducing the

 

Complexity of Code

447

A Sample Program

447

Componentizing

449

Restructuring

451

Specialization

452

Index

455

Introduction

C++ is a flexible, powerful programming language with hundreds of thousands of applications. However, the knowledge of how to take advantage of its full potential comes only with time and experience.

That’s where this book comes in. Think of it as a “cookbook” for solving your programming problems, much as The Joy of Cooking is a guide to solving your dinner dilemmas.

C++ Timesaving Techniques For Dummies is a book for the beginning-to- advanced C++ programmer who needs immediate answers to the problems that crop up in the professional software-development world. I assume that you have prior programming experience, as well as experience specifically with the C++ programming language. “Fluff” — like discussions of looping structures or defining variables, or the basics of compiling applications — is kept to a minimum here. Instead, I offer quick, step-by-step instructions for solving specific problems in C++.

Each technique includes example code — which you are welcome to use in your own applications, or modify as you see fit. This is literally a case of “steal this code, please.” C++ is a language that lends itself well to component-based design and implementation. This means that you can take a piece from here and a piece from there to implement the solution that you have in mind.

C++ Timesaving Techniques For Dummies is not an operating-system- specific (or even compiler-specific) book. The techniques and code that you find here should work on all compilers that support the standard C++ language, and on all operating systems for which a standard compiler exists. This book is intended to be as useful to the Unix programmer as to the Microsoft Windows programmer, and just as useful for programming with X-windows as it is for .Net.

My goal in writing this book is to empower you with some of the stronger features of C++, as well as some great tips and methods to solve everyday problems, without the headaches and lost time that go with trying to figure out how to use those tools. C++ provides simple, fast, powerful solutions to meet the demands of day-to-day programming — my goal is to save you time while making the tools clear and easy to use.

2Introduction

Saving Time with This Book

The Timesaving Techniques For Dummies books focus on big-payoff techniques that save you time, either on the spot or somewhere down the road. And these books get to the point in a hurry, with step-by-step instructions to pace you through the tasks you need

to do, without any of the fluff you don’t want. I’ve identified more than 70 techniques that C++ programmers need to know to make the most of their time. In addition, each technique includes code samples that make programming a breeze. Decide for yourself how to use this book: Read it cover to cover if you like, or skip right to the technique that interests you the most.

In C++ Timesaving Techniques For Dummies, you can find out how to

Reduce time-consuming tasks: I’m letting you in on more than 70 tips and tricks for your C++ system, so you can spend more time creating great results and less time fiddling with a feature so that it works correctly.

Take your skills up a notch: You’re already familiar with the basics of using C++. Now this book takes you to the next level, helping you become a more powerful programmer.

Work with the basics of C++ to meet your needs: I show you how to bend the fundamentals of objectoriented programming and the pre-processor so that your programs work faster and more reliably.

Improve your skills with types, classes, arrays, and templates: Fine-tuning your abilities with these elements will improve your programs’ functionality and make your code more readable.

Understand the finer points of input and output:

Improving the way you work with input and output will reduce memory loss and increase speed.

Use built-in functionality and utilities: Gaining familiarity with these features will help you get the most out of what C++ already offers.

Improve your debugging skills: Getting better at debugging will speed up the whole programming process.

What’s Available on the

Companion Web Site?

The companion Web site for this book contains all the source code shown for the techniques and examples listed in this book. This resource can save you considerable typing when you want to use the code in your own applications, as well as allowing you to easily refer back to the original code if you modify the things you find here. You can find the site at www.dummies.com/go/cpluspluststfd.

Obviously, in order to utilize the code in the book, you will need a C++ compiler. The code in this book was all tested with the GNU C++ compiler, a copy of which you will find on the GNU organization’s Web site: www.gnu.org. This compiler is a public-domain (read: free) compiler that you can use in your own development, or simply to test things on computers that don’t have a full-blown commercial development system. The GNU C++ compiler contains all the standard header files, libraries, debuggers, and other tools that C++ programmers expect.

If you already own another compiler, such as Visual Studio, Borland’s C++Builder, or another compiler, hey, no worries. The code you find here should work with any of these compilers, as long as you follow the standards for defining header files and including code libraries.

Conventions Used in This Book

When I describe output from the compiler, operating system, or application you’re developing, you will see it in a distinctive typeface that looks like this:

This is some output

Source-code listings — such as the application you’re developing and feeding to the compiler to mangle into executable code — will look like this:

What’s In This Book

3

LISTING

// This is a loop

for ( int i=0; i<10; ++i ) printf(“This is line %d\n”, i );

If you are entering code by hand, you should enter it as you see it in the book, although spaces and blank lines won’t matter. Comments can be skipped, if you so choose, but in general, the code is commented as it would be in a production environment.

In general, the code and text in the book should be quite straightforward. The entries are all in list format, taking you step by step through the process of creating source files, compiling them, and running the resulting application. The code is all compiler-agnostic — that is, it doesn’t indicate (because it doesn’t know) the specific compiler commands you will use for the compiler you have on your machine. Please refer to your compiler’s documentation if you have specific questions about the compilation and linking process for your specific compiler or operating system.

What’s In This Book

This book is organized into parts — groups of techniques about a common subject that can save you time and help you get your program written fast and running better. Each technique is written to be independent of the others; you need only implement the techniques that benefit you and your users.

Part I: Streamlining the Means and

Mechanics of OOP

In this part, you learn the basic concepts of objectoriented programming and how they apply to the C++ programming language.

Part II: Working with the Pre-Processor

The C++ pre-processor is a powerful tool for customizing your application, making your code more readable, and creating portable applications. In this section, you get some handy ways to wring the most out of the pre-processor; some handy techniques explain how to create portable code, and the voice of experience reveals why you should avoid the assert macro.

Part III: Types

The C++ language is rich in data types and userdefinable types. In this section, we explore using the built-in types of the language, as well as creating your own types that can be used just like the built-in ones. You find techniques in this section that explain structures and how you can use them. You also zero in on enumerations and creating default arguments for methods.

Part IV: Classes

The core of the C++ programming language is the class. In this section, you get a look at how to create complete classes that work in any environment — as well as how to perform data validation and manipulation, create properties for your class, and inherit from other people’s classes.

Part V: Arrays and Templates

Container classes are a core element of the Standard Template Library (STL), an important part of the C++ programming environment. In this section, you get the goods on working with the various container classes, as well as creating your own containers. Here’s where to find out how to iterate over a collection of objects, and how to allocate and de-allocate blocks of objects.