Learn C++ Under Windows.pdf

(1055 KB) Pobierz
Print
Learn
C ++
Windows
95/NT ®
under
Dave Mark
1598718.002.png
Table of Contents
1 Welcome Aboard . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
What’s in the Package? . . . . . . . . . . . . . . . . . . . 5
Why Learn C++? . . . . . . . . . . . . . . . . . . . . . . 5
What Should I Know to Get Started? . . . . . . . . . . . . . 6
What Equipment Do I Need?. . . . . . . . . . . . . . . . . 6
The Lay of the Land. . . . . . . . . . . . . . . . . . . . . 6
Strap Yourself In... . . . . . . . . . . . . . . . . . . . . . 9
2 Using CodeWarrior . . . . . . . . . . . . . . . . . . . . . . . . . 11
Opening a Project. . . . . . . . . . . . . . . . . . . . . 11
Compiling a Project . . . . . . . . . . . . . . . . . . . . 13
What’s Next?. . . . . . . . . . . . . . . . . . . . . . . 14
3 Remembering C . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Literal Constants . . . . . . . . . . . . . . . . . . . . . 15
Variables . . . . . . . . . . . . . . . . . . . . . . . . 19
Operators . . . . . . . . . . . . . . . . . . . . . . . . 28
Statements. . . . . . . . . . . . . . . . . . . . . . . . 34
Functions . . . . . . . . . . . . . . . . . . . . . . . . 34
Preprocessor Directives . . . . . . . . . . . . . . . . . . 35
Comments . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . 36
4 Introducing C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Getting C Code to Run under C++ . . . . . . . . . . . . . 39
New Features of C++ . . . . . . . . . . . . . . . . . . . 43
The // Comment Marker . . . . . . . . . . . . . . . . . 43
Handling Input and Output . . . . . . . . . . . . . . . . 44
Default Argument Initializers . . . . . . . . . . . . . . . 55
Reference Variables . . . . . . . . . . . . . . . . . . . . 59
Function Name Overloading . . . . . . . . . . . . . . . . 67
The new and delete Operators . . . . . . . . . . . . . . . 71
The Scope Resolution Operator . . . . . . . . . . . . . . . 78
Inline Functions . . . . . . . . . . . . . . . . . . . . . 81
Summary . . . . . . . . . . . . . . . . . . . . . . . . 84
Learn C++ under Windows 95/NT 1
1598718.003.png
Table of Contents
5 Object Programming Basics . . . . . . . . . . . . . . . . . . . . 85
The Organizational Power of the Struct . . . . . . . . . . . 85
Encapsulating Data and Functions . . . . . . . . . . . . . 87
Creating an Object . . . . . . . . . . . . . . . . . . . . 87
Accessing an Object’s Members. . . . . . . . . . . . . . . 89
Deleting an Object . . . . . . . . . . . . . . . . . . . . 92
Writing Member Functions. . . . . . . . . . . . . . . . . 93
Access Privileges . . . . . . . . . . . . . . . . . . . . . 99
Bringing It All Together . . . . . . . . . . . . . . . . . . 102
Friends . . . . . . . . . . . . . . . . . . . . . . . . . 109
Summary . . . . . . . . . . . . . . . . . . . . . . . . 119
6 Derived Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . 121
Derivation, Constructors, and Destructors . . . . . . . . . . 131
Base Classes and Constructors with Parameters. . . . . . . . 139
Overriding Member Functions . . . . . . . . . . . . . . . 146
Summary . . . . . . . . . . . . . . . . . . . . . . . . 151
7 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . 153
The Operator Overloading Alternative . . . . . . . . . . . 154
A Few Restrictions . . . . . . . . . . . . . . . . . . . . 164
Covering All the Bases . . . . . . . . . . . . . . . . . . 167
Special Cases. . . . . . . . . . . . . . . . . . . . . . . 178
Summary . . . . . . . . . . . . . . . . . . . . . . . . 202
8 Inside iostream . . . . . . . . . . . . . . . . . . . . . . . . . . 203
The Character-Based Interface . . . . . . . . . . . . . . . 203
The iostream Classes . . . . . . . . . . . . . . . . . . . 204
Working with Files . . . . . . . . . . . . . . . . . . . . 213
The iostream State Bits . . . . . . . . . . . . . . . . . . 217
More File Info . . . . . . . . . . . . . . . . . . . . . . 223
Customizing iostream . . . . . . . . . . . . . . . . . . . 226
Formatting Your Output . . . . . . . . . . . . . . . . . . 233
Using Manipulators. . . . . . . . . . . . . . . . . . . . 239
istrstream and ostrstream . . . . . . . . . . . . . . . . . 241
Summary . . . . . . . . . . . . . . . . . . . . . . . . 244
2 Learn C++ under Windows 95/NT
1598718.004.png
Table of Contents
9 C++ Potpourri . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Templates . . . . . . . . . . . . . . . . . . . . . . . . 245
Multiple Inheritance . . . . . . . . . . . . . . . . . . . 259
Resolving Ambiguities . . . . . . . . . . . . . . . . . . 268
Initialization Versus Assignment . . . . . . . . . . . . . . 280
Memberwise Initialization . . . . . . . . . . . . . . . . . 285
Summary . . . . . . . . . . . . . . . . . . . . . . . . 293
10 Moving On . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
The User Interface . . . . . . . . . . . . . . . . . . . . 295
The Windows API and Class Libraries . . . . . . . . . . . . 297
Books and Other Reference Material. . . . . . . . . . . . . 297
Go Get ‘Em.... . . . . . . . . . . . . . . . . . . . . . . 298
A Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
B Source Code Listings . . . . . . . . . . . . . . . . . . . . . . 305
call (Chapter 7) . . . . . . . . . . . . . . . . . . . . . . 305
cin (Chapter 4) . . . . . . . . . . . . . . . . . . . . . . 306
cout (Chapter 4) . . . . . . . . . . . . . . . . . . . . . 307
derived (Chapter 6) . . . . . . . . . . . . . . . . . . . . 307
employee (Chapter 5) . . . . . . . . . . . . . . . . . . . 309
equals (Chapter 7) . . . . . . . . . . . . . . . . . . . . 311
formatter (Chapter 8) . . . . . . . . . . . . . . . . . . . 313
friends (Chapter 5) . . . . . . . . . . . . . . . . . . . . 314
gramps (Chapter 6) . . . . . . . . . . . . . . . . . . . . 317
hello++ (Chapter 2) . . . . . . . . . . . . . . . . . . . . 320
init (Chapter 9) . . . . . . . . . . . . . . . . . . . . . . 320
inline (Chapter 4) . . . . . . . . . . . . . . . . . . . . . 321
memberWise (Chapter 9) . . . . . . . . . . . . . . . . . 322
menu (Chapter 7) . . . . . . . . . . . . . . . . . . . . . 325
multInherit (Chapter 9) . . . . . . . . . . . . . . . . . . 327
new (Chapter 7) . . . . . . . . . . . . . . . . . . . . . 330
newTester (Chapter 4) . . . . . . . . . . . . . . . . . . . 331
nonVirtual (Chapter 9) . . . . . . . . . . . . . . . . . . 332
overload (Chapter 4) . . . . . . . . . . . . . . . . . . . 334
overload (Chapter 8) . . . . . . . . . . . . . . . . . . . 335
Learn C++ under Windows 95/NT 3
1598718.005.png
Table of Contents
protoTester (Chapter 4) . . . . . . . . . . . . . . . . . . 337
readMe (Chapter 8) . . . . . . . . . . . . . . . . . . . . 338
reference (Chapter 4) . . . . . . . . . . . . . . . . . . . 339
scopeTester (Chapter 4) . . . . . . . . . . . . . . . . . . 340
smartPtr (Chapter 7) . . . . . . . . . . . . . . . . . . . 341
square (Chapter 6) . . . . . . . . . . . . . . . . . . . . 342
stateBits (Chapter 8). . . . . . . . . . . . . . . . . . . . 344
strstream (Chapter 8) . . . . . . . . . . . . . . . . . . . 346
subscript (Chapter 7) . . . . . . . . . . . . . . . . . . . 346
template (Chapter 9) . . . . . . . . . . . . . . . . . . . 348
time (Chapter 7) . . . . . . . . . . . . . . . . . . . . . 350
virtual (Chapter 9) . . . . . . . . . . . . . . . . . . . . 353
whatAmI (Chapter 6) . . . . . . . . . . . . . . . . . . . 356
C fstream.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
D iomanip.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
E iostream.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
F sstream.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
G Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
4 Learn C++ under Windows 95/NT
1598718.001.png
Zgłoś jeśli naruszono regulamin