Seasoned C++ programmer Matthew Telles shows developers at any level of experience how to save hours by improving, refactoring, and debugging their code Some of the techniques covered include mastering asserts, creating a complete class, hiding a method of a base class, freeing blocks of memory, implementing a simple locking mechanism, creating debugging macros, checking for errors at run-time, finding and fixing memory leaks, and reducing code complexity The easy-to-follow, two-column Timesaving Techniques format makes mastering each timesaver a snap C++ is one of today's most widely used programming languages, with compilers offered by Microsoft, Borland, and Code Warrior This book is the perfect companion to C++ For Dummies, Fifth Edition (0-7645-6852-3)
Author(s): Matthew Telles
Series: For Dummies
Edition: 1
Publisher: Wiley Publishing
C++ Timesaving Techniques For Dummies
About the Author
Dedication
Author’s Acknowledgments
Contents at a Glance
Table of Contents
Introduction
What’s Available on the Companion Web Site?
Conventions Used in This Book
Saving Time with This Book
What’s In This Book
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
Technique 5: Separating Rules and Data from Code
The cDate Class
Testing the cDate Class
Part II: Working with the Pre-Processor
Technique 6: Handling Multiple Operating Systems
Creating the Header File
Testing the Header File
Technique 7: Mastering the Evils of Asserts
The Assert Problem
Fixing the Assert Problem
Technique 8: Using const Instead of # define
Using the const Construct
Identifying the Errors
Fixing the Errors
Technique 9: Macros and Why Not to Use Them
Initiating a Function with a String Macro — Almost
Fixing What Went Wrong with the Macro
Using Macros Appropriately
Technique 10: Understanding sizeof
Using the sizeof Function
Evaluating the Results
Using sizeof with Pointers
Part III: Types
Technique 11: Creating Your Own Basic Types
Implementing the Range Class
Testing the Range Class
Technique 12: Creating Your Own Types
Creating the Matrix Class
Matrix Operations
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
Technique 20: Defining Default Arguments for Your Functions and Methods
Customizing the Functions We Didn’t Write
Customizing Functions We Wrote Ourselves
Testing the Default Code
Fixing the Problem
Part IV: Classes
Technique 21: Creating a Complete Class
Creating a Complete Class Template
Testing the Complete Class
Technique 22: Using Virtual Inheritance
Implementing Virtual Inheritance
Correcting the Code
Technique 23: Creating Overloaded Operators
Rules for Creating Overloaded Operators
Using Conversion Operators
Using Overloaded Operators
Testing the MyString Class
Technique 24: Defining Your Own new and delete Handlers
Rules for Implementing new and delete Handlers
Overloading new and delete Handlers
Testing the Memory Allocation Tracker
Technique 25: Implementing Properties
Implementing Properties
Testing the Property Class
Technique 26: Doing Data Validation with Classes
Implementing Data Validation with Classes
Testing Your SSN Validator Class
Technique 27: Building a Date Class
Creating the Date Class
Implementing the Date Functionality
Testing the Date Class
Some Final Thoughts on the Date Class
Technique 28: Overriding Functionality with Virtual Methods
Creating a Factory Class
Testing the Factory
Enhancing the Manager Class
Technique 29: Using Mix-In Classes
Implementing Mix-In Classes
Compiling and Testing Your Mix-In Class
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
Technique 39: Implementing a Spreadsheet
Creating the Column Class
Creating the Row Class
Creating the Spreadsheet Class
Testing Your Spreadsheet
Part VI: Input and Output
Technique 40: Using the Standard Streams to Format Data
Working with Streams
Technique 41: Reading In and Processing Files
Testing the File-Reading Code
Creating the Test File
Technique 42: How to Read Delimited Files
Reading Delimited Files
Testing the Code
Technique 43: Writing Your Objects as XML
Creating the XML Writer
Testing the XML Writer
Technique 44: Removing White Space from Input
Technique 45: Creating a Configuration File
Creating the Configuration-File Class
Setting Up Your Test File
Testing the Configuration-File Class
Part VII: Using the Built-In Functionality
Technique 46: Creating an Internationalization Class
Building the Language Files
Creating an Input Text File
Reading the International File
Testing the String Reader
Technique 47: Hashing Out Translations
Creating a Translator Class
Testing the Translator Class
Technique 48: Implementing Virtual Files
Creating a Virtual File Class
Testing the Virtual File Class
Improving Your Virtual File Class
Technique 49: Using Iterators for Your Collections
Technique 50: Overriding the Allocator for a Collection Class
Creating a Custom Memory Allocator
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
Technique 60: Creating a Generic Buffer Class
Creating the Buffer Class
Testing the Buffer Class
Technique 61: Opening a File Using Multiple Paths
Creating the Multiple-Search-Path Class
Testing the Multiple-Search-Path Class
Part IX: Debugging C++ Applications
Technique 62: Building Tracing into Your Applications
Implementing the Flow Trace Class
Testing the Flow Trace System
Adding in Tracing After the Fact
Technique 63: Creating Debugging Macros and Classes
The assert Macro
Logging
Testing the Logger Class
Design by Contract
Technique 64: Debugging Overloaded Methods
Adding Logging to the Application
Part X: The Scary (or Fun!) Stuff
Technique 65: Optimizing Your Code
Making Functions Inline
Avoiding Temporary Objects
Passing Objects by Reference
Postponing Variable Declarations
Choosing Initialization Instead of Assignment
Technique 66: Documenting the Data Flow
Learning How Code Operates
Testing the Properties Class
Technique 67: Creating a Simple Locking Mechanism
Creating the Locking Mechanism
Testing the Locking Mechanism
Technique 68: Creating and Using Guardian Classes
Creating the File-Guardian Class
Testing the File-Guardian Class
Technique 69: Working with Complex Numbers
Implementing the Complex Class
Testing the Complex Number Class
Technique 70: Converting Numbers to Words
Creating the Conversion Code
Testing the Conversion Code
Technique 71: Reducing the Complexity of Code
A Sample Program
Componentizing
Restructuring
Specialization
Index