- Page 1 and 2: Thinking in C++ 2 nd edition Volume
- Page 3 and 4: TICA18: July 29, 1999. Rewrote Chap
- Page 5 and 6: TICA7, August 14, 1998. Strings cha
- Page 7 and 8: http://www.microsoft.com/typography
- Page 9 and 10: In Bruce Eckel President, MindView
- Page 11 and 12: MindView Ad Space
- Page 13 and 14: Dedication To the scholar, the heal
- Page 15 and 16: 2: Making & Using Objects 87 The pr
- Page 17 and 18: Type-safe linkage............. 323
- Page 19 and 20: protected ............. 618 protect
- Page 21 and 22: Preface Like any human language, C+
- Page 23 and 24: So the short answer is: what isn’
- Page 25 and 26: end up losing some portion of the a
- Page 27 and 28: Chapters C++ is a language in which
- Page 29 and 30: Chapter 5: Hiding the Implementatio
- Page 31 and 32: existing type. In this chapter you
- Page 33 and 34: (and official mirror sites) where i
- Page 35 and 36: note that the CD ROM is browser-bas
- Page 37: Constantine, Lucy Lockwood, Tom Kef
- Page 41 and 42: abstraction of the underlying machi
- Page 43 and 44: 5. All objects of a particular type
- Page 45 and 46: Type Name Interface Light +on() +of
- Page 47 and 48: actually a service to users because
- Page 49 and 50: forced to use the idioms of the und
- Page 51 and 52: design is that it’s too simple to
- Page 53 and 54: interface function here, but I want
- Page 55 and 56: Such code is unaffected by the addi
- Page 57 and 58: can’t do object-oriented programm
- Page 59 and 60: to be executed than when calling dr
- Page 61 and 62: Exception handling: dealing with er
- Page 63 and 64: sort of process, no matter how limi
- Page 65 and 66: work: “It will be done when it’
- Page 67 and 68: Bank Lobby Uses ATM Machine Custome
- Page 69 and 70: and regardless of the moans and man
- Page 71 and 72: alternative to UML is a textual des
- Page 73 and 74: classes partition the problem and h
- Page 75 and 76: With iteration, you create somethin
- Page 77 and 78: in the long run to lose all the old
- Page 79 and 80: in”) rather than the terms of the
- Page 81 and 82: paradigm. You’ve done it before.
- Page 83 and 84: Management obstacles If you’re a
- Page 85 and 86: great acceleration in their program
- Page 87 and 88: 2: Making & Using Objects This chap
- Page 89 and 90:
Interpreters An interpreter transla
- Page 91 and 92:
The compilation process To program
- Page 93 and 94:
tool for breaking a program up into
- Page 95 and 96:
Arguments in function declarations
- Page 97 and 98:
float b; // Declaration & definitio
- Page 99 and 100:
necessary truncating the name and a
- Page 101 and 102:
code file when you build your own l
- Page 103 and 104:
particular type. With iostream obje
- Page 105 and 106:
However, in this book the standard
- Page 107 and 108:
send cout a variety of different ar
- Page 109 and 110:
At first, the code above can look l
- Page 111 and 112:
To test the program, run it, type a
- Page 113 and 114:
s1 = s3 + " " + s4; // Combining st
- Page 115 and 116:
that can be interpreted as “true
- Page 117 and 118:
that the class will work with (in t
- Page 119 and 120:
one that changes something, typical
- Page 121 and 122:
you can see that the vector is not
- Page 123 and 124:
3: The C in C++ Since C++ is based
- Page 125 and 126:
ugs that resulted is probably one r
- Page 127 and 128:
Function return values A C++ functi
- Page 129 and 130:
Creating your own libraries with th
- Page 131 and 132:
cout i; if(i < 10) if(i > 5) // "i
- Page 133 and 134:
for do { cout > guess; // Initializ
- Page 135 and 136:
cout
- Page 137 and 138:
ool quit = false; // Flag for quitt
- Page 139 and 140:
Precedence Operator precedence defi
- Page 141 and 142:
inary, this maximum value can be di
- Page 143 and 144:
An idiom that falls under “poor p
- Page 145 and 146:
Introduction to pointers Whenever y
- Page 147 and 148:
cout
- Page 149 and 150:
Since a general guideline for C++ p
- Page 151 and 152:
pointers come in handy. In a sense,
- Page 153 and 154:
} ///:~ In f( )’s argument list,
- Page 155 and 156:
vp = &f; vp = &d; } ///:~ Once you
- Page 157 and 158:
Defining variables on the fly As no
- Page 159 and 160:
Although the example also shows var
- Page 161 and 162:
The ExtractCode.cpp program in Volu
- Page 163 and 164:
linking this file with FileStatic2.
- Page 165 and 166:
keyword. Defining a variable or fun
- Page 167 and 168:
Legitimate floating-point constant
- Page 169 and 170:
: C03:Mathops.cpp // Mathematical o
- Page 171 and 172:
int main() { int i,j; cout > i; cou
- Page 173 and 174:
if(val & (1
- Page 175 and 176:
} else highbit = 0; // Left shift (
- Page 177 and 178:
and b are both assigned to -99. b i
- Page 179 and 180:
To perform a cast, put the desired
- Page 181 and 182:
the following sections, while the l
- Page 183 and 184:
int main() { const int i = 0; int*
- Page 185 and 186:
} ///:~ sizeof( ) can also give you
- Page 187 and 188:
int* x, y; This actually produces a
- Page 189 and 190:
actually repeat the name of the str
- Page 191 and 192:
enum ShapeType { circle, square, re
- Page 193 and 194:
largest item you’ve put in the un
- Page 195 and 196:
} } ///:~ Array access is extremely
- Page 197 and 198:
The fact that naming an array produ
- Page 199 and 200:
following program prints out all it
- Page 201 and 202:
exit(1); } double d = atof(argv[1])
- Page 203 and 204:
char c; short s; int i; long l; flo
- Page 205 and 206:
usually need is ++ and --. Pointer
- Page 207 and 208:
cout > reply; if(reply == "on") deb
- Page 209 and 210:
message telling you what the assert
- Page 211 and 212:
: C03:ComplicatedDefinitions.cpp /*
- Page 213 and 214:
combination of state variables). Th
- Page 215 and 216:
Because make is available in some f
- Page 217 and 218:
make which files depend on which ot
- Page 219 and 220:
directory where the book’s files
- Page 221 and 222:
ules for creating the executable, b
- Page 223 and 224:
4. Modify Menu.cpp to use switch st
- Page 225 and 226:
can figure out what’s going on (t
- Page 227 and 228:
4: Data Abstraction C++ is a produc
- Page 229 and 230:
about using libraries in C. Underst
- Page 231 and 232:
} s->next = 0; int add(CStash* s, c
- Page 233 and 234:
CStash called intStash, for example
- Page 235 and 236:
object, but to an array of objects:
- Page 237 and 238:
initialize( ). One of the problems
- Page 239 and 240:
up the stack. In fact, the float va
- Page 241 and 242:
All the data members are exactly th
- Page 243 and 244:
} int newQuantity = quantity + incr
- Page 245 and 246:
int i = 10; void* vp = &i; // OK in
- Page 247 and 248:
selection operator ‘.’ preceded
- Page 249 and 250:
Abstract data typing The ability to
- Page 251 and 252:
cout
- Page 253 and 254:
kind of declaration, you suspend al
- Page 255 and 256:
preprocessor (Standard C++ header f
- Page 257 and 258:
included - in a single compilation
- Page 259 and 260:
void initialize(); void push(void*
- Page 261 and 262:
the current head; then head is assi
- Page 263 and 264:
the local one, so you must tell it
- Page 265 and 266:
9. Create a file containing a funct
- Page 267:
add and get lines via member functi
- Page 270 and 271:
putting the function declarations i
- Page 272 and 273:
void B::func() {} int main() { A a;
- Page 274 and 275:
You can declare a global function a
- Page 276 and 277:
The declaration simply tells the co
- Page 278 and 279:
void Holder::Pointer::set(int i) {
- Page 280 and 281:
and protected ted that might requir
- Page 282 and 283:
}; int B::f() { return i + j + k; }
- Page 284 and 285:
int next; // Next empty space // Dy
- Page 286 and 287:
Hiding the implementation Some proj
- Page 288 and 289:
} require(smile != 0); smile->i = 0
- Page 290 and 291:
1. Create a class with public, priv
- Page 293 and 294:
6: Initialization & Cleanup Chapter
- Page 295 and 296:
This chapter examines the features
- Page 297 and 298:
Both the constructor and destructor
- Page 299 and 300:
void Tree::printsize() { cout
- Page 301 and 302:
cout > retval; require(retval != 0)
- Page 303 and 304:
Uncommenting the statements in the
- Page 305 and 306:
#include using namespace std; cons
- Page 307 and 308:
using namespace std; int main() { S
- Page 309 and 310:
Stack::Stack() { head = 0; } void S
- Page 311 and 312:
In this case, all the lines in text
- Page 313 and 314:
struct Y { float f; int i; Y(int a)
- Page 315 and 316:
int main() { V v, v2[10]; } ///:~ I
- Page 317:
22. Use aggregate initialization to
- Page 320 and 321:
When you create an object (a variab
- Page 322 and 323:
internal names the compiler uses mi
- Page 324 and 325:
int main() { //! f(1); // Causes a
- Page 326 and 327:
int Stash::add(void* element) { if(
- Page 328 and 329:
But it turns out that a union can a
- Page 330 and 331:
} void SuperVar::print() { switch (
- Page 332 and 333:
Stash A(100), B(100, 0); will produ
- Page 334 and 335:
#ifndef MEM_H #define MEM_H typedef
- Page 336 and 337:
The Mem class is designed to be use
- Page 338 and 339:
} strcpy((char*)buf->pointer(), str
- Page 340 and 341:
Exercises Solutions to selected exe
- Page 343 and 344:
8: Constants The concept of constan
- Page 345 and 346:
Most of the time, BUFSIZE will beha
- Page 347 and 348:
const int j = i + 10; // Value from
- Page 349 and 350:
internal linkage for consts so if y
- Page 351 and 352:
const int* u; Starting from the ide
- Page 353 and 354:
Assignment and type checking C++ is
- Page 355 and 356:
void f2(int ic) { const int& i = ic
- Page 357 and 358:
f5( ) returns a non-const X object,
- Page 359 and 360:
The choice of whether to return a p
- Page 361 and 362:
Standard argument passing In C it
- Page 363 and 364:
You might assume that the logical c
- Page 365 and 366:
This is especially critical when in
- Page 367 and 368:
const string* StringStack::pop() {
- Page 369 and 370:
would be instead: enum { size = 100
- Page 371 and 372:
: C08:Quoter.cpp // Random quote se
- Page 373 and 374:
Y(); void f() const; }; Y::Y() { i
- Page 375 and 376:
volatile tells the compiler not to
- Page 377 and 378:
Exercises Solutions to selected exe
- Page 379 and 380:
20. Create a class called MyString
- Page 381 and 382:
9: Inline Functions One of the impo
- Page 383 and 384:
the preprocessor expands it, somewh
- Page 385 and 386:
Here’s the output produced by the
- Page 387 and 388:
Inline functions In solving the C++
- Page 389 and 390:
Here, the two constructors and the
- Page 391 and 392:
: C09:Rectangle.cpp // Accessors &
- Page 393 and 394:
void mark() { lflag = aflag = 0; st
- Page 395 and 396:
int main() { Time start; for(int i
- Page 397 and 398:
unsigned char* b = new unsigned cha
- Page 399 and 400:
Most of the functions inline quite
- Page 401 and 402:
The compiler cannot perform inlinin
- Page 403 and 404:
Member(int x = 0) : i(x), j(x), k(x
- Page 405 and 406:
.setWidth(iHeight); } ///:~ Now if
- Page 407 and 408:
arguments or if a file cannot be op
- Page 409 and 410:
time error. The local declaration a
- Page 411 and 412:
4. Create two identical functions,
- Page 413:
Generate assembly code when the fun
- Page 416 and 417:
The static keyword was overloaded i
- Page 418 and 419:
extracting characters from the prev
- Page 420 and 421:
entered and destructed as main( ) e
- Page 422 and 423:
There are times when you’d like t
- Page 424 and 425:
Other storage class specifiers You
- Page 426 and 427:
#endif // HEADER2_H ///:~ //: C10:C
- Page 428 and 429:
public: Z(int i); int g(); }; X::Z:
- Page 430 and 431:
namespace Calculation { class Integ
- Page 432 and 433:
A using declaration is an alias, an
- Page 434 and 435:
ut here, the scope resolution opera
- Page 436 and 437:
}; float Values::table[4] = { 1.1,
- Page 438 and 439:
Local class cannot have static data
- Page 440 and 441:
int main() { X x; X* xp = &x; x.f()
- Page 442 and 443:
First file #include std::ofstream
- Page 444 and 445:
This technique requires an addition
- Page 446 and 447:
initCount will be zero so the initi
- Page 448 and 449:
#endif // DEPENDENCY2_H ///:~ The c
- Page 450 and 451:
Now, in the implementation files wh
- Page 452 and 453:
extern "C" { #include "Myheader.h"
- Page 454 and 455:
of out into an extern declaration a
- Page 456 and 457:
namespace with a using directive an
- Page 459 and 460:
11: References & the Copy-Construct
- Page 461 and 462:
References in C++ A reference (&) i
- Page 463 and 464:
} return x; // Safe, x lives outsid
- Page 465 and 466:
Argument-passing guidelines Your no
- Page 467 and 468:
supposed to know what to do This is
- Page 469 and 470:
eturn value that saves and restores
- Page 471 and 472:
} return x; int main() { HowMany h;
- Page 473 and 474:
so you take the reference of the so
- Page 475 and 476:
The print( ) function has been modi
- Page 477 and 478:
16) HowMany2(const HowMany2&) 17) h
- Page 479 and 480:
from existing classes. Now take the
- Page 481 and 482:
To create a copy-constructor for a
- Page 483 and 484:
argument to modify. If you always p
- Page 485 and 486:
Data d, *dp = &d; int Data::*pmint
- Page 487 and 488:
eally clean things up, you can use
- Page 489 and 490:
constructor when you pass or return
- Page 491 and 492:
function and explain how the way th
- Page 493 and 494:
12: Operator Overloading Operator o
- Page 495 and 496:
1
- Page 497 and 498:
number of arguments required by an
- Page 499 and 500:
} // Prefix; return decremented val
- Page 501 and 502:
int main() { Integer a; f(a); Byte
- Page 503 and 504:
const Integer& right); // Assignmen
- Page 505 and 506:
const Integer& right) { return Inte
- Page 507 and 508:
} int operator!=(const Integer& lef
- Page 509 and 510:
} const Byte operator|(const Byte&
- Page 511 and 512:
eturn b >= right.b; } int operator&
- Page 513 and 514:
1. As with any function argument, i
- Page 515 and 516:
The return optimization When new ob
- Page 517 and 518:
The global function allows the comm
- Page 519 and 520:
values using static data members. P
- Page 521 and 522:
points to the beginning of the ObjC
- Page 523 and 524:
typedef int (Dog::*PMF)(int) const;
- Page 525 and 526:
• There are no user-defined opera
- Page 527 and 528:
It’s very important that the over
- Page 529 and 530:
: C12:CopyingVsInitialization.cpp c
- Page 531 and 532:
A common mistake was made in this e
- Page 533 and 534:
DogHouse fidos(new Dog("Fido"), "Fi
- Page 535 and 536:
Dog(const Dog& d) : nm(d.nm + " cop
- Page 537 and 538:
} friend ostream& operator
- Page 539 and 540:
Created DogHouse: [SpotHouse] conta
- Page 541 and 542:
Automatic type conversion In C and
- Page 543 and 544:
Operator conversion The second way
- Page 545 and 546:
overloaded operator. Also, when the
- Page 547 and 548:
Now any function that takes a char*
- Page 549 and 550:
Fee(const Fi&) {} }; class Fo { int
- Page 551 and 552:
9. Create a class that contains a s
- Page 553:
25. Combine the classes in Overload
- Page 556 and 557:
How many planes will an air-traffic
- Page 558 and 559:
implemented by making calls for chu
- Page 560 and 561:
casual programmer, C’s approach t
- Page 562 and 563:
Simple demo of new & delete #includ
- Page 564 and 565:
void* data; // Some storage const i
- Page 566 and 567:
container. Using new and delete, it
- Page 568 and 569:
} // "Remove" the pointer: if(v !=
- Page 570 and 571:
As before, Stashes are created and
- Page 572 and 573:
ecause it is allocated in one big c
- Page 574 and 575:
count++; new int[1000]; // Exhausts
- Page 576 and 577:
global versions, you make the defau
- Page 578 and 579:
Overloading new & delete for a clas
- Page 580 and 581:
using the aggregate initialization
- Page 582 and 583:
class Widget { static const int sz
- Page 584 and 585:
even though the array operator new
- Page 586 and 587:
The way that you pass the extra arg
- Page 588 and 589:
Summary It’s convenient and optim
- Page 590 and 591:
information about vector in your St
- Page 592 and 593:
That’s the C approach, and it has
- Page 594 and 595:
class Y { int i; X x; // Embedded o
- Page 596 and 597:
never what you want 1 ; the desired
- Page 598 and 599:
an object) can get out of the start
- Page 600 and 601:
} ///:~ C inherits from B and has a
- Page 602 and 603:
macro is created to build some of t
- Page 604 and 605:
} }; class Derived3 : public Base {
- Page 606 and 607:
} string* peek() const { return (st
- Page 608 and 609:
Functions that don’t automaticall
- Page 610 and 611:
} Checkers& operator=(const Checker
- Page 612 and 613:
Game::operator=(c); These calls sho
- Page 614 and 615:
Because the composition of a Car is
- Page 616 and 617:
#include using namespace std; clas
- Page 618 and 619:
ob.sleep(); bob.sleep(1); //! bob.s
- Page 620 and 621:
until you’ve been programming qui
- Page 622 and 623:
: C14:Instrument.cpp // Inheritance
- Page 624 and 625:
Parent() : i(0) { cout
- Page 626 and 627:
Member(const Member&) values in c2:
- Page 628 and 629:
Pointer & reference upcasting In In
- Page 630 and 631:
4. Modify Combined.cpp to add anoth
- Page 632 and 633:
calling the base-class copy-constru
- Page 634 and 635:
It provides another dimension of se
- Page 636 and 637:
You also saw a problem arise, which
- Page 638 and 639:
member function. That is, the compi
- Page 640 and 641:
: C15:Instrument4.cpp // Extensibil
- Page 642 and 643:
} ///:~ You can see that another in
- Page 644 and 645:
: C15:Sizes.cpp // Object sizes wit
- Page 646 and 647:
derived from Instrument (and thus h
- Page 648 and 649:
The arguments of a C++ function cal
- Page 650 and 651:
using namespace std; class Pet { pu
- Page 652 and 653:
Anecdotal evidence suggests that th
- Page 654 and 655:
delays the appearance of the error
- Page 656 and 657:
New function: void f(Instrument& i)
- Page 658 and 659:
The other benefit to this feature i
- Page 660 and 661:
has only a pointer to a base-class
- Page 662 and 663:
}; void describe(Pet p) { // Slices
- Page 664 and 665:
class Base { public: virtual int f(
- Page 666 and 667:
: C15:VariantReturn.cpp // Returnin
- Page 668 and 669:
virtual functions & constructors Wh
- Page 670 and 671:
Behavior of virtual functions insid
- Page 672 and 673:
other functions, only that function
- Page 674 and 675:
class (although this would also be
- Page 676 and 677:
Virtuals in destructors There’s s
- Page 678 and 679:
usually named “Object”). It tur
- Page 680 and 681:
#include "../require.h" #include #
- Page 682 and 683:
class Pet { public: virtual ~Pet(){
- Page 684 and 685:
typeid, and you can also imagine th
- Page 686 and 687:
happens. Leaving it as a pure virtu
- Page 688 and 689:
and by making the destructor virtua
- Page 690 and 691:
35. Start with PointerToMemberOpera
- Page 692 and 693:
Although C++ templates are a genera
- Page 694 and 695:
for(i = 0; i < sz; i++) if(f[i] ==
- Page 696 and 697:
existing class. You find a class as
- Page 698 and 699:
Now OShape has Shape’s characteri
- Page 700 and 701:
template class Array { static const
- Page 702 and 703:
int main() { Array fa; fa[0] = 1.41
- Page 704 and 705:
: C16:StackTemplateTest.cpp // Test
- Page 706 and 707:
technique like this if you are crea
- Page 708 and 709:
~X() { cout
- Page 710 and 711:
} require(index >= 0, "PStash::oper
- Page 712 and 713:
Print both objects and pointers: fr
- Page 714 and 715:
cout
- Page 716 and 717:
eturn head head->data : 0; } T* po
- Page 718:
Default constructor performs object
- Page 721 and 722:
} int pop() { return stack[top > 0
- Page 723 and 724:
int top; public: IntStack() : top(0
- Page 725 and 726:
The next step, of course, is to mak
- Page 727 and 728:
cout
- Page 729 and 730:
}; iterator begin() const { return
- Page 731 and 732:
delete it2; } ///:~ A Stack is inst
- Page 733 and 734:
Create a new iterator that's moved
- Page 735 and 736:
The PStashIter follows the iterator
- Page 737 and 738:
Why iterators Up until now you’ve
- Page 739 and 740:
~Drawing() { cout
- Page 741 and 742:
containers without knowing the unde
- Page 743 and 744:
7. Repeat the above exercise using
- Page 745 and 746:
A: Coding Style This appendix is no
- Page 747 and 748:
appear first in the electronic vers
- Page 749 and 750:
Parens, braces and indentation You
- Page 751 and 752:
ody of the code, or whether they sh
- Page 753 and 754:
Order of header inclusion Headers a
- Page 755 and 756:
B: Programming Guidelines This appe
- Page 757 and 758:
5. Don’t fall into analysis paral
- Page 759 and 760:
(member objects) to inheritance. Th
- Page 761 and 762:
27. If your class contains pointers
- Page 763 and 764:
try to build the object “in place
- Page 765 and 766:
when initializing subobjects; other
- Page 767 and 768:
61. Hide your pointers inside conta
- Page 769 and 770:
C: Recommended Resources for furthe
- Page 771 and 772:
The C++ ANSI/ISO Standard. This is
- Page 773 and 774:
else”) and out of denial mode (
- Page 775 and 776:
D:Compiler specifics This appendix
- Page 777 and 778:
@CPPFLAGS = -w-inl -w-csu -wnak @OF
- Page 779 and 780:
# makefile lines are generated: &un
- Page 781 and 782:
AND (&&), 172 and, && (logical AND)
- Page 783 and 784:
address of, 351 and enum in classes
- Page 785 and 786:
incrementing, 194 type checking, 19
- Page 787 and 788:
static dependency, 443 static to ze
- Page 789 and 790:
not equivalent (!=), 172 not, ! (lo
- Page 791 and 792:
abstract base classes and pure virt
- Page 793 and 794:
system specification, 66 system() f
- Page 795 and 796:
MindView Ad Space 795
- Page 797:
CD ROM Instructions (facing CD ROM)