c++ for dummies, 5th (2004) - Pdf 12

TEAM LinG - Live, Informative, Non-cost and Genuine!
C++
FOR
DUMmIES

5TH EDITION
by Stephen Randy Davis
TEAM LinG - Live, Informative, Non-cost and Genuine!
TEAM LinG - Live, Informative, Non-cost and Genuine!
C++
FOR
DUMmIES

5TH EDITION
by Stephen Randy Davis
TEAM LinG - Live, Informative, Non-cost and Genuine!
C++ For Dummies
®
, 5th Edition
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
Copyright © 2004 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

ISBN: 0-7645-6852-3
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
5B/SW/QU/QU/IN
TEAM LinG - Live, Informative, Non-cost and Genuine!
About the Author
Stephen R. Davis lives with his wife and son near Dallas, Texas. He and
his family have written numerous books including C++ For Dummies and
C++ Weekend Crash Course. Stephen works for L-3 Communications.
Dedication
To my friends and family, who help me be the best Dummy I can be.
Author’s Acknowledgments
I find it very strange that only a single name appears on the cover of any
book, but especially a book like this. In reality, many people contribute to
the creation of a For Dummies book. From the beginning, editorial director
Mary Corder and my agent, Claudette Moore, were involved in guiding and
molding the book’s content. During the development of the five editions of
this book, I found myself hip-deep in edits, corrections, and suggestions from
a group of project editors, copyeditors, and technical reviewers — this book
would have been a poorer work but for their involvement. And nothing would
have made it into print without the aid of the person who coordinated the
first and second editions of the project, Suzanne Thomas. Nevertheless, one
name does appear on the cover and that name must take responsibility for
any inaccuracies in the text.
I also have to thank my wife, Jenny, and son, Kinsey, for their patience and
devotion. I hope we manage to strike a reasonable balance.
Finally, a summary of the animal activity around my house. For those of you
who have not read any of my other books, I should warn you that this has
become a regular feature of my For Dummies books.
My two dogs, Scooter and Trude, continue to do well, although Trude is all

Layout and Graphics: Amanda Carter,
Andrea Dahl, Denny Hager, Michael Kruzil,
Lynsey Osborn, Jacque Schneider
Proofreaders: Andy Hollandbeck, Carl Pierce,
Dwight Ramsey, TECHBOOKS Production
Services
Indexer: TECHBOOKS Production Services
Special Help:
Barry Childs-Helton
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
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
TEAM LinG - Live, Informative, Non-cost and Genuine!
Table of Contents
Introduction 1
What’s in This Book 1
What’s on the CD 2
What Is C++? 2
Conventions Used in This Book 2
How This Book Is Organized 3
And There’s More 4
Part I: Introduction to C++ Programming 4
Part II: Becoming a Functional C++ Programmer 4

Examining the remainder of Conversion.cpp 26
Chapter 2: Declaring Variables Constantly . . . . . . . . . . . . . . . . . . . . . . .27
Declaring Variables 27
Declaring Different Types of Variables 28
Reviewing the limitations of integers in C++ 29
Solving the truncation problem 30
Looking at the limits of floating-point numbers 31
Declaring Variable Types 33
Types of constants 34
Special characters 35
Are These Calculations Really Logical? 36
Mixed Mode Expressions 36
Chapter 3: Performing Mathematical Operations . . . . . . . . . . . . . . . . .39
Performing Simple Binary Arithmetic 40
Decomposing Expressions 41
Determining the Order of Operations 42
Performing Unary Operations 43
Using Assignment Operators 45
Chapter 4: Performing Logical Operations . . . . . . . . . . . . . . . . . . . . . . .47
Why Mess with Logical Operations? 47
Using the Simple Logical Operators 48
Storing logical values 49
Using logical int variables 51
Be careful performing logical operations
on floating-point variables
51
Expressing Binary Numbers 53
The decimal number system 54
Other number systems 54
The binary number system 54

Including Include Files 91
Chapter 7: Storing Sequences in Arrays . . . . . . . . . . . . . . . . . . . . . . . . .93
Considering the Need for Arrays 93
Using an array 95
Initializing an array 98
Accessing too far into an array 99
Using arrays 99
Defining and using arrays of arrays 100
Using Arrays of Characters 100
Creating an array of characters 100
Creating a string of characters 101
Manipulating Strings with Character 103
String-ing Along Variables 106
Chapter 8: Taking a First Look at C++ Pointers . . . . . . . . . . . . . . . . . .109
Variable Size 109
What’s in an Address? 110
Address Operators 111
Using Pointer Variables 112
Comparing pointers and houses 114
Using different types of pointers 114
Passing Pointers to Functions 117
Passing by value 117
Passing pointer values 118
Passing by reference 119
TEAM LinG - Live, Informative, Non-cost and Genuine!
viii
C++ For Dummies, 5th Edition
Making Use of a Block of Memory Called the Heap 119
Limiting scope 120
Examining the scope problem 121

Introducing the Class 161
The Format of a Class 162
Accessing the Members of a Class 163
TEAM LinG - Live, Informative, Non-cost and Genuine!
Table of Contents
ix
Chapter 13: Making Classes Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
Activating Our Objects 168
Simulating real-world objects 168
Why bother with member functions? 169
Adding a Member Function 169
Creating a member function 170
Naming class members 171
Calling a Member Function 171
Accessing a member function 172
Accessing other members from a member function 174
Scope Resolution (And I Don’t Mean How Well
Your Microscope Works)
175
Defining a Member Function in the Class 177
Keeping a Member Function After Class 179
Overloading Member Functions 181
Chapter 14: Point and Stare at Objects . . . . . . . . . . . . . . . . . . . . . . . . .183
Defining Arrays of and Pointers to Simple Things 183
Declaring Arrays of Objects 184
Declaring Pointers to Objects 185
Dereferencing an object pointer 186
Pointing toward arrow pointers 187
Passing Objects to Functions 187
Calling a function with an object value 188

Working with destructors 217
Chapter 17: Making Constructive Arguments . . . . . . . . . . . . . . . . . . . .221
Outfitting Constructors with Arguments 221
Justifying constructors 222
Using a constructor 222
Placing Too Many Demands on the Carpenter:
Overloading the Constructor
223
Defaulting Default Constructors 227
Constructing Class Members 228
Constructing a complex data member 228
Constructing a constant data member 232
Constructing the Order of Construction 233
Local objects construct in order 234
Static objects construct only once 234
All global objects construct before main( ) 235
Global objects construct in no particular order 235
Members construct in the order in which they are declared 236
Destructors destruct in the reverse order
of the constructors
237
Chapter 18: Copying the Copy Copy Copy Constructor . . . . . . . . . . . .239
Copying an Object 239
Why you need the copy constructor 239
Using the copy constructor 240
The Automatic Copy Constructor 242
Creating Shallow Copies versus Deep Copies 244
It’s a Long Way to Temporaries 248
Avoiding temporaries, permanently 249
Referring to the copy constructor’s referential argument 250

Making an honest class out of an abstract class 287
Passing abstract classes 289
Declaring pure virtual functions — is it really necessary? 290
Factoring C++ Source Code 291
Dividing the program — Student 292
Defining a namespace 293
Implementing Student 294
Dividing the program — GraduateStudent 295
Implementing an application 296
Project file 298
Creating a project file under Dev-C++ 298
Part V: Optional Features 303
Chapter 23: A New Assignment Operator,
Should You Decide to Accept It
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
Comparing Operators with Functions 305
Inserting a New Operator 306
TEAM LinG - Live, Informative, Non-cost and Genuine!
xii
C++ For Dummies, 5th Edition
Creating Shallow Copies Is a Deep Problem 307
Overloading the Assignment Operator 308
Protecting the Escape Hatch 311
Chapter 24: Using Stream I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313
How Stream I/O Works 313
The fstream Subclasses 315
Reading Directly from a Stream 320
What’s Up with endl? 322
Using the strstream Subclasses 322
Manipulating Manipulators 325

Single-Stepping Every Path at Least Once 381
Avoid Overloading Operators 382
Heap Handling 382
Using Exceptions to Handle Errors 382
Avoiding Multiple Inheritance 383
Chapter 30: The Ten Most Important Optional Features
of Dev-C++
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385
Customize Editor Settings to Your Taste 385
Highlight Matching Braces/Parentheses 386
Enable Exception Handling 387
Include Debugging Information (Sometimes) 387
Create a Project File 388
Customize the Help Menu 388
Reset Breakpoints after Editing the File 388
Avoid Illegal Filenames 389
Include #include Files in Your Project 389
Executing the Profiler 389
Appendix: About the CD 393
System Requirements 393
Using the CD with Microsoft Windows 394
Using the CD with Linux 395
What You’ll Find 396
Development tools 396
Program source code 397
If You’ve Got Problems (Of the CD Kind) 397
Bonus Chapters on the CD-ROM! CD
Bonus Chapter 1: A Functional Budget Program . . . . . . . . . . . . . . . .BC1
BUDGET1 BC1
BUDGET2 BC7

prior knowledge, at least, not of programming.
C++ For Dummies is rife with examples. Every concept is documented in numer-
ous snippets and several complete programs.
Unlike other C++ programming books, C++ For Dummies considers the “why”
just as important as the “how.” The features of C++ are like pieces of a jigsaw
puzzle. Rather than just present the features, I think it’s important that you
understand how they fit together.
If you don’t understand why a particular feature is in the language, you won’t
truly understand how it works. After you finish this book, you’ll be able to write
a reasonable C++ program, and, just as important, you’ll understand why and
how it works.
C++ For Dummies can also be used as a reference: If you want to understand
what’s going on with all the template stuff, just flip to Chapter 27, and you’re
there. Each chapter contains necessary references to other earlier chapters
in case you don’t read the chapters in sequence.
C++ For Dummies is not operating- or system-specific. It is just as useful to Unix
or Linux programmers as it is to Windows-based developers. C++ For Dummies
doesn’t cover Windows or .NET programming. You have to master C++ before
you can move on to Windows and .NET programming.
TEAM LinG - Live, Informative, Non-cost and Genuine!
2
C++ For Dummies, 5th Edition
What’s on the CD
The CD-ROM included with C++ For Dummies contains the source code for the
examples in this book. This can spare you considerable typing.
Your computer can’t execute these or any other C++ program directly. You have
to run your C++ programs through a C++ development environment, which
spits out an executable program. (Don’t worry, this procedure is explained in
Chapter 1.)
The programs in C++ For Dummies are compatible with any standard C++ envi-

// some program
void main()
{

}
If you are entering these programs by hand, you must enter the text exactly
as shown with one exception: The number of spaces is not critical, so don’t
worry if you enter one too many or one too few spaces.
C++ words are usually based on English words with similar meanings. This can
make reading a sentence containing both English and C++ difficult to make out
without a little help. To help out, C++ commands and function names appear
in a different font
like this. In addition, function names are always followed
by an open and closed parenthesis like
myFavoriteFunction(). The argu-
ments to the function are left off except when there’s a specific need to make
them easier to read. It’s a lot easier to say: “this is
myFavoriteFunction()”
than “this is
myFavoriteFunction(int, float).”
Sometimes, the book directs you to use specific keyboard commands. For exam-
ple, when the text instructs you to press Ctrl+C, it means that you should hold
down the Ctrl key while pressing the C key and then release both together.
Don’t type the plus sign.
Sometimes, I’ll tell you to use menu commands, such as File➪Open. This nota-
tion means to use the keyboard or mouse to open the File menu and then
choose the Open option. Finally, both Dev-C++ and Visual Studio.NET define
function keys for certain common operations — unfortunately, they don’t
use the same function keys. To avoid confusion, I rarely use function keys in
the book — I couldn’t have kept the two straight anyway.

Part I: Introduction to C++ Programming
Part I starts you on your journey. You begin by examining what it means to
write a computer program. From there, you step through the syntax of the
language (the meaning of the C++ commands).
Part II: Becoming a Functional
C++ Programmer
In this part, you expand upon your newly gained knowledge of the basic com-
mands of C++ by adding the capability to bundle sections of C++ code into mod-
ules and reusing these modules in programs.
TEAM LinG - Live, Informative, Non-cost and Genuine!
5
Introduction
In this section, I also introduce that most dreaded of all topics, the C++ pointer.
If you don’t know what that means, don’t worry — you’ll find out soon enough.
Part III: Introduction to Classes
The plot thickens in this part. Part III begins the discussion of object-oriented
programming. Object-oriented programming is really the reason for the exis-
tence of C++. Take the OO features out of C++, and you’re left with its prede-
cessor language, C. I discuss things such as classes, constructors, destructors,
and making nachos (I’m not kidding, by the way). Don’t worry if you don’t
know what these concepts are (except for nachos — if you don’t know what
nachos are, we’re in big trouble).
Part IV: Inheritance
Inheritance is where object-oriented programming really comes into its own.
Understanding this most important concept is the key to effective C++ pro-
gramming and the goal of Part IV. There’s no going back now — after you’ve
completed this part, you can call yourself an Object-Oriented Programmer,
First Class.
Part V: Optional Features
By the time you get to Part V, you know all you need to program effectively in

TEAM LinG - Live, Informative, Non-cost and Genuine!
In this part . . .
B
oth the newest, hottest flight simulator and the
application.
simplest yet most powerful accounting programs
use the same basic building blocks. In this part, you dis-
cover the basic features you need to write your killer
TEAM LinG - Live, Informative, Non-cost and Genuine!


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status