Teach Yourself C++ (Introductory Pages)
Teach Yourself C++ (Introductory Pages)
OSBORNE --
TE 1
/I
./Herbert Schildt
CIC+-i- Author with More Than 1.5 Million Books Sold 2'
Teach
Yourself C++,
Third Edition
Herbert Schildt
Osborne McGraw-Hill
1*eley New York SI. Louis San Francisco Auckland Boqou Hamburg London Madrid Mexico City
Milan Montreal New Delhi Panama o... ca ou,.i, e...... e..... . - ,-. -
PUBLISHER ( )slirne/McGraw-lIill
Brandon A. Nordin 2600 I'eiiih Street
0er1sekv. ( Ii1 ir na 94710
EDITOR-IN-CHIEF U.S.A.
Scott Rogers
Ir information tIn translations or hook distributors outside the U.S.A., or
ACQUISITIONS EDITOR its arrange hulk purchase discounts for sales peOmOtiOi1. premiums Or
A4egg Bonar 6usd-raisers, please contact Osborne/McGraw-Hill at the above address.
PROJECT EDITOR
Janet Walden Teach Yourself C++, Third Edition
EDITORIAL ASSISTANT
Gordon Hurd Cii 1 svright (6 20041 by llte McGraw-F till Companies. All rights rr'.rrved.
p rinted iii the United States of America. Except as permitted under the
Emily Wolman In fortnai nit has beets n,brai ned by Osborne/McGraw-Hill from sources
believed to be reliable. lii iwcvcr, because of the possibility of human or
INDEXER
mechanical error b y our sources, Osborne/McGraw-1 Ii11, or others,
Sheryl Schi/dt ()shornc/McGraw-HiU does not guarantee the accuracy, adequacy, or
completeness of any i nformation and is not responsible for any errors or
COUTER DESIGNER Otis issions or the results obtained from use of such information.
Jani Beckwith
ILLUSTRATOR
Roberta Steele
SERIES DESIGN
Roberta Steele
COVER DESIGN
Arlette Crosland
Contents at a Glance
1 An Overview of C++ I
2 Introducing Classes 41
3 A Closer Look at Classes 87
4 Arrays, Pointers, and References 117
5 Function Overloading 159
6 Introducing Operator Overloading 195
7 Inheritance 231
8 Introducing the C++ 1 /0 System 269
9 Advanced C++ I/O 307
10 Virtual Functions 345
11 Templates and Exception Handling 371
12 Run-nine Type Identification and the Casting Operators 407
13 Nanespaces, ConversiOn Functions, and
Miscellaneous Topics 435
14 Introducing the Standard Template Library 473
A A Few More Differences Between C and C++ 531
B Answers 533
Index 739
1.
An Overview of C++ 1
1.1 WHAT IS OBJECT-ORIENTED
PROGRAMMING?, 3
1.2 TWO VERSIONS OF C++, 7
1.3 C++ CONSOLE 1/0, 13
1.4 C++ COMMENTS, 19
1.5 CLASSES: A FIRST LOOK 21
1.6 SOME DIFFERENCES BETWEEN C AND C++, 28
1.7 INTRODUCING FUNCTION OVERLOADING, 33
1.8 C++ KEYWORDS, 39
SKILLS CHECK, 39
2 Introducing Classes 41
2.1 CONSTRUCTOR AND DESTRUCTOR
FUNCTIONS, 43
2.2 CONSTRUCTORS THAT TAKE
PARAMETERS, 52
2.3 INTRODUCING INHERITANCE, 59
2.4 OBJECT POINTERS, 66
2.5 CLASSES, STRUCTURES, AND UNIONS ARE
RELATED, 68
2.6 IN-LINE FUNCTIONS, 75
2.7 AUTOMATIC IN-LINING, 80
SKILLS CHECK, 83
B Answers • 533
1.3 EXERCISES, 534
1.4 EXERCISES, 535
1.5 EXERCISES, 535
1.6 EXERCISES, 538
1.7 EXERCISES, 538
MASTERY SKILLS CHECK: Chapter 1, 541
REVIEW SKILLS CHECK: Chapter 2, 543
2.1 EXERCISES, 545
2.2 EXERCISES, 548
2.3 EXERCISE, 551
2.5 EXERCISES, 553
2.6 EXERCISES, 555
2.7 EXERCISES, 556
MASTERY SKILLS CHECK: Chapter 2, 558
CUMULATIVE SKILLS CHECK: Chapter 2, 560
REVIEW SKILLS CHECK: Chapter 3, 562
3.1 EXERCISES, 563
3.2 EXERCISES, 565
3.3 EXERCISES, 567
3.4 EXERCISE, 567
MASTERY SKILLS CHECK: Chapter 3, 569
CUMULATIVE SKILLS CHECK: Chapter 3, 57]
REVIEW SKILLS CHECK: Chapter 4, 576
4.1 EXERCISES, 578
4.2 EXERCISES 580
4.3 EXERCISE, 582
4.4 EXERCISES, 583
4.5 EXERCISES, 584
4.6 EXERCISES, 585
4.7 EXERCISE 586
4.8 EXERCISES 587
'vIJj IkY I'ULL UHECK; CHAPTER 4, 589
CUMULATIVE SKILLS CHECK: Chapter 4, 592
REVIEW SKILLS CHECK: Chapter 5, 593
5.1 EXERCISES, 595
5.2 EXERCISES 598
5.4 EXERCISES, 600
5.6 EXERCISE, 601
MASTERY SKILLS ChECK; Chapter 5, 602
CUMULATIVE SKILLS CHECK: Chapter 5, 605
REVIEW SKILLS CHECK: Chapter 6, 607
6.2 EXERCISES, 608
6.3 EXERCISE, 609
6.4 EXERCISES, 610
6.5 EXERCISES, 612
6.6 EXERCISE, 616
6.7 EXERCISES, 618
MASTERY SKILLS CHECK: Chapter 6, 621
CUMULATIVE SKILLS CHECK: Chapter 6, 629
REVIEW SKILLS CHECK: Chapter 7, 631
7.1 EXERCISES 637
7.2 EXERCISES, 637
7.3 EXERCISES, 638
7.4 EXERCISES, 640
7.5 EXERCISES, 641
MASTERY SKILLS CHECK: Chapter 7, 641
CUMULATIVE SKILLS CHECK: Chapter 7, 643
REVIEW SKILLS CHECK: Chapter 8, 644
8.2 EXERCISES, 646
8.3 EXERCISES, 647
8.4 EXERCISES, 649
8.5 EXERCISES, 650
8.6 EXERCISES, 652
MASTERY SKILLS CHECK: Chapter 8, 655
CUMULATIVE SKILLS CHECK: Chapter 8, 659
REVIEW SKILLS CHECK: Chapter 9, 662
9.1 EXERCISES, 664
9.2 EXERCISES, 666
9.3 EXERCISES, 668
9.4 EXERCISES, 671
9.5 EXERCISES, 673
9.6 EXERCISE, 674
MASTERY SKILLS CHECK: Chapter 9, 677
CUMULATIVE SKILLS CHECK: Chapter 9, 682
REVIEW SKILLS CHECK: Chapter 10, 684
10.2 EXERCISES, 687
10.3 EXERCISES, 688
10.4 EXERCISE, 689
MASTERY SKILLS CHECK: Chapter 10, 69
CUMULATIVE SKILLS CHECK: Chapter 10, 694
REVIEW SKILLS CHECK: Chapter 11, 698
11.1 EXERCISES, 698
11.2 EXERCISES, 699
11.3 EXERCISES, 701
11.4 EXERCISES, 702
11.5 EXERCISES, 702
MASTERY SKILLS CHECK: Chapter 11, 703
REVIEW SKILLS CHECK: Chapter 12, 710
12.1 EXERCISES, 712
12.2 EXERCISES, 712
12.3 EXERCISES, 713
MASTERY SKILLS CHECK: Chapter 12, 714
CUMULATIVE SKILLS CHECK: Chapter 12, 715
REVIEW SKILLS CHECK: Chapter 13, 717
13.1 EXERCISES, 717
13.2 EXERCISES, 719
13.3 EXERCISES, 720
13.4 EXERCISES, 723
13.5 EXERCISES, 724
13.7 EXERCISES, 724
MASTERY SKILLS CHECK: Chapter 13, 726
CUMULATIVE SKILLS CHECK: Chapter 13, 727
REVIEW SKILLS CHECK: Chapter 14, 727
14.1 EXERCISES, 728
14.3 EXERCISES, 728
14.4 EXERCISES 729
14.5 EXERCISES, 732
14.6 EXERCISES, 733
14.7 EXERCISES, 735
MASTERY SKILLS CHECK: Chapter 14, 737
Index . 739
Acknowledgments
Bjarne Stroustrup
Steve Clamage
P J. Plauger
Al Stevens
This is the third edition of Teach Yourself C++. It includes all of the
material contained in the first two editions and adds two new chapters
and many new topics. The first new chapter covers Run-Time Type ID
(RTTI) and the new casting operators. The second covers the Standard
Template Library (STL). Both of these topics are major features added
to the C++ language since the previous edition was published. New
topics include namcspaces, the new-style headers, and coverage of the
modern-style I/O system, In all, the third edition of Teach Yourself
C++ is suhstJntially larger than its preceding two editions.
If You!re Using, Windows
using C++ allows the use of class libraries such as MFC, that can
greatly simplify the development of a Windows program.
Remember, the source code for all of the programs in this book is
available free-of-charge on the Web at http://www.osborne.com .
Downloading this code prevents you from having to type in
the examples.
For Further Study
If you will be developing programs for the Web, you will want
to read
Java: The Complete Reference