Table Of ContentC++ Programming
Wikibooks.org
April22,2012
This PDF was generated by a program written by Dirk Hünniger, which is freely
available under an open source license from HTTP://DE.WIKIBOOKS.ORG/WIKI/
BENUTZER:DIRK_HUENNIGER/WB2PDF. Thelistofcontributorsisincludedinchap-
terContributorsonpage661. ThelicensesGPL,LGPLandGFDLareincludedinchapter
Licensesonpage679,sincethisbookand/orpartsofitmayormaynotbelicensedunder
oneormoreoftheselicenses,andthusrequireinclusionoftheselicenses. Thelicensesof
thefiguresaregiveninthelistoffiguresonpage675.
Contents
1 ABOUT THE BOOK 3
1.1 FOREWORD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 GUIDE TO READERS . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 READER COMMENTS . . . . . . . . . . . . . . . . . . . . . . . 4
2 C++ A MULTI-PARADIGM LANGUAGE 7
2.1 INTRODUCING C++ . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 WHAT IS A PROGRAMMING LANGUAGE? . . . . . . . . . . . . 11
2.3 PROGRAMMING PARADIGMS . . . . . . . . . . . . . . . . . . . 16
2.4 CHAPTER SUMMARY . . . . . . . . . . . . . . . . . . . . . . . 40
3 FUNDAMENTALS FOR GETTING STARTED 41
3.1 THE CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.2 THE COMPILER . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.3 VARIABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
3.4 OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
3.5 TYPE CONVERSION . . . . . . . . . . . . . . . . . . . . . . . . 204
3.6 CONTROL FLOW STATEMENTS . . . . . . . . . . . . . . . . . . 213
3.7 FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
3.8 DEBUGGING . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
3.9 CHAPTER SUMMARY . . . . . . . . . . . . . . . . . . . . . . . 383
4 OBJECT ORIENTED PROGRAMMING 385
4.1 STRUCTURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
4.2 union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
4.3 CLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
4.4 COPY CONSTRUCTOR . . . . . . . . . . . . . . . . . . . . . . . 436
4.5 EQUALITY OPERATOR . . . . . . . . . . . . . . . . . . . . . . . 436
4.6 INEQUALITY OPERATOR . . . . . . . . . . . . . . . . . . . . . 437
4.7 OPERATOR OVERLOADING . . . . . . . . . . . . . . . . . . . . 438
4.8 I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
4.9 CHAPTER SUMMARY . . . . . . . . . . . . . . . . . . . . . . . 481
III
Contents
5 ADVANCED FEATURES 483
5.1 TEMPLATES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
5.2 STANDARD TEMPLATE LIBRARY (STL) . . . . . . . . . . . . . 499
5.3 SMART POINTERS . . . . . . . . . . . . . . . . . . . . . . . . . 515
5.4 SEMANTICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
5.5 EXCEPTION HANDLING . . . . . . . . . . . . . . . . . . . . . . 517
5.6 RUN-TIME TYPE INFORMATION (RTTI) . . . . . . . . . . . . . 530
5.7 CHAPTER SUMMARY . . . . . . . . . . . . . . . . . . . . . . . 535
6 BEYOND THE STANDARD 537
6.1 RESOURCE ACQUISITION IS INITIALIZATION (RAII) . . . . . 537
6.2 GARBAGE COLLECTION . . . . . . . . . . . . . . . . . . . . . . 540
6.3 PROGRAMMING PATTERNS . . . . . . . . . . . . . . . . . . . . 542
6.4 LIBRARIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
6.5 BOOST LIBRARY . . . . . . . . . . . . . . . . . . . . . . . . . . 588
6.6 CROSS-PLATFORM DEVELOPMENT . . . . . . . . . . . . . . . . 598
6.7 SOFTWARE INTERNATIONALIZATION . . . . . . . . . . . . . . 621
6.8 OPTIMIZATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . 628
6.9 FURTHER READING . . . . . . . . . . . . . . . . . . . . . . . . 640
6.10 MODELING TOOLS . . . . . . . . . . . . . . . . . . . . . . . . 640
6.11 CHAPTER SUMMARY . . . . . . . . . . . . . . . . . . . . . . . 641
7 APPENDIX A: INTERNAL REFERENCES 643
8 APPENDIX B: EXTERNAL REFERENCES 645
8.1 REFERENCE SITES . . . . . . . . . . . . . . . . . . . . . . . . . 645
8.2 COMPILERS AND IDES . . . . . . . . . . . . . . . . . . . . . . 646
8.3 MISC. C++ TOOLS . . . . . . . . . . . . . . . . . . . . . . . . 649
8.4 LIBRARIES1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
8.5 C++ CODING CONVENTIONS . . . . . . . . . . . . . . . . . . . 653
8.6 ONLINE C++ BOOKS, GUIDES AND GENERAL INFORMATION . 655
8.7 OTHER (DEAD TREE) BOOKS ON C++ . . . . . . . . . . . . . . 660
9 CONTRIBUTORS 661
LIST OF FIGURES 675
10 LICENSES 679
10.1 GNU GENERAL PUBLIC LICENSE . . . . . . . . . . . . . 679
10.2 GNU FREE DOCUMENTATION LICENSE . . . . . . . . . . . . . 680
1 Chapter6.3.3onpage584
1
Contents
10.3 GNU LESSER GENERAL PUBLIC LICENSE . . . . . . . . . . . 681
2
1 About the book
1.1 Foreword
This book covers the C++ programming language, its interactions with software
design and real life use of the language. It is presented as an introductory to ad-
vancecoursebutcanbeusedasreferencebook.
If you are familiar with programming in other languages you may just skim the
GETTING STARTED CHAPTER1. You should not skip the PROGRAMMING
PARADIGMS SECTION2,becauseC++doeshavesomeparticularsthatshouldbe
usefulevenifyoualreadyknowanotherObjectOrientedProgramminglanguage.
TheLANGUAGECOMPARISONSSECTION3providescomparisonsforsomelan-
guage(s)youmayalreadyknow,whichmaybeusefulforveteranprogrammers.
If this is your first contact with programming then read the book from the begin-
ning. BearinmindthattheProgrammingParadigmssectioncanbehardtodigest
if you lack some experience. Do not despair, the relevant points will be extended
asotherconceptsareintroduced. Thatsectionisprovidedsotogiveyouamental
framework, not only to understand C++, but to let you easily adapt to (and from)
otherlanguagesthatmayshareconcepts.
1.2 Guide to readers
This book is a WIKIBOOK4 (EN.WIKIBOOKS.ORG)5, an up-to-date copy of the
workishostedthere.
1 Chapter1.3onpage5
2 Chapter2.2.3onpage16
3 Chapter2.3.6onpage22
4 HTTP://EN.WIKIPEDIA.ORG/WIKI/WIKIBOOK
5 HTTP://EN.WIKIBOOKS.ORG/WIKI/MAIN%20PAGE
3
Aboutthebook
It is organized into different parts, but as this is a work that is always evolving,
thingsmaybemissingorjustnotwheretheyshouldbe, youarefreetobecomea
writerandcontributetofixthingsup...
1.3 Reader comments
If you have comments about the technical accuracy, content, or organization of
thisdocument,pleasetellus(e.g. byusingthe"discussion"pagesorbyemail). Be
suretoincludethesection/titleofthedocumentwithyourcommentsandthedate
ofyourcopyofthebook. Ifyouarereallyconvincedofyourpoint,informationor
correction then become a writer (at Wikibooks) and do it, it can always be rolled
backifsomeonedisagrees.
06
6 HTTP://EN.WIKIBOOKS.ORG/WIKI/CATEGORY%3A
4
Readercomments
Thefollowingpeopleareauthorstothisbook:
PANICa, THENUB314b
Youcanverifywhohascontributedtothisbookbyexaminingthehistorylogs
atWikibooks(http://en.wikibooks.org/).
Acknowledgment is given for using some contents from other works like
WIKIPEDIAc, the wikibooks JAVA PROGRAMMINGd and C PROGRAMMINGe
and the C++ REFERENCEf , as from the authors SCOTT WHEELERg,
STEPHEN FERGh andIvorHorton.
Theaboveauthorsreleasetheirworkunderthefollowinglicense:
This work is licensed under the Creative Commons Attribution-Share Alike
3.0 Unported license. In short: you are free to share and to make derivatives
of this work under the conditions that you appropriately attribute it, and that
you only distribute it under the same, similar or a compatible license. Any of
the above conditions can be waived if you get permission from the copyright
holder. Unlessotherwisenoted,mediaandsourcecode usedinthisbookhave
their own copyright, may use different licenses than the one used here, and
werenotcreatedbytheaboveauthors. Theauthors,contributors,andlicenses
usedshouldbeacknowledgedseparately.
a HTTP://EN.WIKIBOOKS.ORG/WIKI/USER%3APANIC2K4
b HTTP://EN.WIKIBOOKS.ORG/WIKI/USER%3ATHENUB314
c HTTP://EN.WIKIPEDIA.ORG/WIKI/
d HTTP://EN.WIKIBOOKS.ORG/WIKI/JAVA%20PROGRAMMING
e HTTP://EN.WIKIBOOKS.ORG/WIKI/C%20PROGRAMMING
f HTTP://WWW.CPPREFERENCE.COM
g HTTP://KTOWN.KDE.ORG/~{}WHEELER/BIO.HTML
h HTTP://WWW.FERG.ORG/INDEX.HTML
7
08
7 HTTP://EN.WIKIBOOKS.ORG/WIKI/CATEGORY%3AC%2B%2B%20PROGRAMMING
8 HTTP://EN.WIKIBOOKS.ORG/WIKI/CATEGORY%3A
5
Aboutthebook
6
2 C++ a multi-paradigm language
2.1 Introducing C++
(pronounced "see plus plus") is a general-purpose, statically typed, free-
form, multi-paradigm PROGRAMMING LANGUAGE1 supporting procedural pro-
gramming, data abstraction, and generic programming. During the 1990S2, C++
becameoneofthemostpopularcomputerprogramminglanguages.
2.1.1 Historyandstandardization
Figure2: PhotoofBjarneStroustrup,creatorofthe
programminglanguageC++.
1 Chapter2.1.3onpage11
2 HTTP://EN.WIKIPEDIA.ORG/WIKI/1990S
7