91% found this document useful (11 votes)
33 views60 pages

C The Complete Reference Herbert Schildt All Chapter Instant Download

Complete

Uploaded by

udovcsuff91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
91% found this document useful (11 votes)
33 views60 pages

C The Complete Reference Herbert Schildt All Chapter Instant Download

Complete

Uploaded by

udovcsuff91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 60

Get ebook downloads in full at ebookname.

com

C The Complete Reference Herbert Schildt

https://ebookname.com/product/c-the-complete-reference-
herbert-schildt/

OR CLICK BUTTON

DOWNLOAD EBOOK

Explore and download more ebook at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Java 2 The Complete Reference 5th Edition Herbert


Schildt

https://ebookname.com/product/java-2-the-complete-reference-5th-
edition-herbert-schildt/

Herb Schildt s Java Programming Cookbook 1st Edition


Herbert Schildt

https://ebookname.com/product/herb-schildt-s-java-programming-
cookbook-1st-edition-herbert-schildt/

The Art of Java 1st Edition Herbert Schildt

https://ebookname.com/product/the-art-of-java-1st-edition-
herbert-schildt/

The Future of Kurdistan The Iraqi Dilemma 1st Edition


Kerim Yildiz

https://ebookname.com/product/the-future-of-kurdistan-the-iraqi-
dilemma-1st-edition-kerim-yildiz/
Schools for All Kinds of Minds Boosting Student Success
by Embracing Learning Variation 1st Edition Mary-Dean
Barringer

https://ebookname.com/product/schools-for-all-kinds-of-minds-
boosting-student-success-by-embracing-learning-variation-1st-
edition-mary-dean-barringer/

Facing Cancer and the Fear of Death A Psychoanalytic


Perspective on Treatment 1st Edition Norman Straker
(Ed.)

https://ebookname.com/product/facing-cancer-and-the-fear-of-
death-a-psychoanalytic-perspective-on-treatment-1st-edition-
norman-straker-ed/

Bernard Shaw and the BBC 1st Edition L. W. Conolly

https://ebookname.com/product/bernard-shaw-and-the-bbc-1st-
edition-l-w-conolly/

Union Tactics and Economic Change A Case Study of Three


Philadelphia Textile Unions Gladys L. Palmer

https://ebookname.com/product/union-tactics-and-economic-change-
a-case-study-of-three-philadelphia-textile-unions-gladys-l-
palmer/

Seismic Amplitude An Interpreter s Handbook Simm R.

https://ebookname.com/product/seismic-amplitude-an-interpreter-s-
handbook-simm-r/
Identifying and Managing Project Risk Essential Tools
for Failure Proofing Your Project 3rd Edition Tom
Kendrick

https://ebookname.com/product/identifying-and-managing-project-
risk-essential-tools-for-failure-proofing-your-project-3rd-
edition-tom-kendrick/
C++:
The Complete Reference,
Fourth Edition
About the Author
Herbert Schildt is the world’s leading programming
author. He is an authority on the C, C++, Java, and C#
languages, and is a master Windows programmer. His
programming books have sold more than 3 million
copies worldwide and have been translated into all
major foreign languages. He is the author of numerous
bestsellers, including C++: The Complete Reference, C#:
The Complete Reference, Java 2: The Complete Reference,
C: The Complete Reference, C++ from the Ground Up,
C++: A Beginner’s Guide, C#: A Beginner’s Guide, and
Java 2: A Beginner’s Guide. Schildt holds a master’s
degree in computer science from the University of
Illinois. He can be reached at his consulting office at
(217) 586-4683.

Copyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.
C++:
The Complete Reference,
Fourth Edition
Herbert Schildt

McGraw-Hill/Osborne
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
Copyright © 2003 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as per-
mitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any
means, or stored in a database or retrieval system, without the prior written permission of the publisher.

0-07-150239-4

The material in this eBook also appears in the print version of this title: 0-07-222680-3.

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked
name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the
trademark. Where such designations appear in this book, they have been printed with initial caps.

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate
training programs. For more information, please contact George Hoare, Special Sales, at [email protected] or
(212) 904-4069.

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work.
Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one
copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, trans-
mit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the
work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be
terminated if you fail to comply with these terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO
THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,
INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,
AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WAR-
RANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant
or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free.
Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in
the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through
the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, conse-
quential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibil-
ity of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in con-
tract, tort or otherwise.

DOI: 10.1036/0072226803
Professional

Want to learn more?


We hope you enjoy this
McGraw-Hill eBook! If
you’d like more information about this book,
its author, or related books and websites,
please click here.
Contents at a Glance
Part I The Foundation of C++: The C Subset

1 An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4 Arrays and Null-Terminated Strings . . . . . . . . . . . . . . . . 89
5 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
7 Structures, Unions, Enumerations,
and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . 161
8 C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
9 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
10 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . 237

Part II C++

11 An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255


12 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

v
vi C++: The Complete Reference

13 Arrays, Pointers, References, and the Dynamic


Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
14 Function Overloading, Copy Constructors,
and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 359
15 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
16 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
17 Virtual Functions and Polymorphism . . . . . . . . . . . . . . . 443
18 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
19 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
20 The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . 509
21 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
22 Run-Time Type ID and the Casting Operators . . . . . . . 567
23 Namespaces, Conversion Functions,
and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 591
24 Introducing the Standard Template Library . . . . . . . . . . 629

Part III The Standard Function Library

25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 699


26 The String and Character Functions . . . . . . . . . . . . . . . . 723
27 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 737
28 Time, Date, and Localization Functions . . . . . . . . . . . . . 747
29 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . 757
30 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
31 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . . 775

Part IV The Standard C++ Class Library

32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . 787


33 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . 811
34 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
35 STL Iterators, Allocators, and Function Objects . . . . . . 861
36 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
37 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897
38 Exception Handling and Miscellaneous Classes . . . . . . 925
Contents at a Glance vii

Part V Applying C++

39 Integrating New Classes: A Custom String Class . . . . . 935


40 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
A The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999
B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
This page intentionally left blank
For more information about this title, click here

Contents
Introduction ............................................. xxix

Part I
The Foundation of C++: The C Subset

1 An Overview of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Origins and History of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
C Is a Middle-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
C Is a Structured Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
C Is a Programmer’s Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
The Form of a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Library and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Separate Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Understanding the .C and .CPP File Extensions . . . . . . . . . . . . . . . . 12

2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The Five Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Modifying the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Identifier Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Where Variables Are Declared . . . . . . . . . . . . . . . . . . . . . . . . 18
Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

ix
x C++: The Complete Reference

Formal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
The const and volatile Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Storage Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Variable Initializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Hexadecimal and Octal Constants . . . . . . . . . . . . . . . . . . . . 33
String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Backslash Character Constants . . . . . . . . . . . . . . . . . . . . . . . 33
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Type Conversion in Assignments . . . . . . . . . . . . . . . . . . . . . 35
Multiple Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . 40
Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
The & and * Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . 48
The Compile-Time Operator sizeof . . . . . . . . . . . . . . . . . . . 50
The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
The Dot (.) and Arrow (−>) Operators . . . . . . . . . . . . . . . . . 51
The [ ] and ( ) Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Precedence Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Type Conversion in Expressions . . . . . . . . . . . . . . . . . . . . . . 53
Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Spacing and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Compound Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

3 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
True and False in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The if-else-if Ladder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The ? Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Conditional Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Contents xi

switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Nested switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
for Loop Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
The Infinite Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
for Loops with No Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Declaring Variables Within Selection and Iteration Statements . . . 81
Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The exit( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Expression Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Block Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

4 Arrays and Null-Terminated Strings . . . . . . . . . . . . . . . . 89


Single-Dimension Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Generating a Pointer to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Passing Single-Dimension Arrays to Functions . . . . . . . . . . . . . . . . 92
Null-Terminated Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Indexing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Unsized Array Initializations . . . . . . . . . . . . . . . . . . . . . . . . . 106
A Tic-Tac-Toe Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

5 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
What Are Pointers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Pointer Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
The Pointer Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Pointer Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Pointer Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Pointer Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Multiple Indirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Initializing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
xii C++: The Complete Reference

C's Dynamic Allocation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 129


Problems with Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

6 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
The General Form of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Scope Rules of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Call by Value, Call by Reference . . . . . . . . . . . . . . . . . . . . . . 139
Creating a Call by Reference . . . . . . . . . . . . . . . . . . . . . . . . . 140
Calling Functions with Arrays . . . . . . . . . . . . . . . . . . . . . . . 142
argc and argv—Arguments to main( ) . . . . . . . . . . . . . . . . . . . . . . . . 144
The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Returning from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Returning Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Functions of Type void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
What Does main( ) Return? . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Standard Library Function Prototypes . . . . . . . . . . . . . . . . . 157
Declaring Variable-Length Parameter Lists . . . . . . . . . . . . . . . . . . . . 158
Old-Style Versus Modern FunctionParameter Declarations . . . . . . 158

7 Structures, Unions, Enumerations,


and User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . 161
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Accessing Structure Members . . . . . . . . . . . . . . . . . . . . . . . . 165
Structure Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Passing Structures to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Passing Structure Members to Functions . . . . . . . . . . . . . . . 167
Passing Entire Structures to Functions . . . . . . . . . . . . . . . . . 168
Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Declaring a Structure Pointer . . . . . . . . . . . . . . . . . . . . . . . . . 170
Using Structure Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Arrays and Structures Within Structures . . . . . . . . . . . . . . . . . . . . . . 173
Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Using sizeof to Ensure Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

8 C-Style Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187


An Important Application Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Contents xiii

Reading and Writing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189


A Problem with getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Alternatives to getchar( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Reading and Writing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Formatted Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
printf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Printing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Printing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Displaying an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
The Minimum Field Width Specifier . . . . . . . . . . . . . . . . . . 199
The Precision Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Handling Other Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 202
The * and # Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
scanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Inputting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Inputting Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . 205
Reading Individual Characters Using scanf( ) . . . . . . . . . . 205
Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Inputting an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
The %n Specifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Using a Scanset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Discarding Unwanted White Space . . . . . . . . . . . . . . . . . . . 207
Non-White-Space Characters in the Control String . . . . . . 208
You Must Pass scanf( ) Addresses . . . . . . . . . . . . . . . . . . . . . 208
Format Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Suppressing Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

9 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211


C Versus C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Streams and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Text Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Binary Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
File System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
The File Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Writing a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
xiv C++: The Complete Reference

Using fopen( ), getc( ), putc( ), and fclose( ) . . . . . . . . . . . . . 218


Using feof( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Working with Strings: fputs( ) and fgets( ) . . . . . . . . . . . . . . 222
rewind( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
ferror( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Erasing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Flushing a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Using fread( ) and fwrite( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
fseek( ) and Random-Access I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
fprintf( ) and fscanf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
The Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
The Console I/O Connection . . . . . . . . . . . . . . . . . . . . . . . . . 233
Using freopen( ) to Redirect the Standard Streams . . . . . . 234

10 The Preprocessor and Comments . . . . . . . . . . . . . . . . . . 237


The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
#define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Defining Function-like Macros . . . . . . . . . . . . . . . . . . . . . . . 240
#error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
#include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Conditional Compilation Directives . . . . . . . . . . . . . . . . . . . . . . . . . . 242
#if, #else, #elif, and #endif . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
#ifdef and #ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
#undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Using defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
#line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
#pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The # and ## Preprocessor Operators . . . . . . . . . . . . . . . . . . . . . . . . . 248
Predefined Macro Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

Part II
C++

11 An Overview of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255


The Origins of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
What Is Object-Oriented Programming? . . . . . . . . . . . . . . . . . . . . . . 257
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Some C++ Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
A Sample C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Contents xv

A Closer Look at the I/O Operators . . . . . . . . . . . . . . . . . . . 263


Declaring Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
No Default to int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The bool Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Old-Style vs. Modern C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
The New C++ Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Working with an Old Compiler . . . . . . . . . . . . . . . . . . . . . . . 270
Introducing C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
The C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
The General Form of a C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . 288

12 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289


Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Structures and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Unions and Classes Are Related . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Anonymous Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Friend Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Defining Inline Functions Within a Class . . . . . . . . . . . . . . 306
Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Constructors with One Parameter: A Special Case . . . . . . . 309
Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Static Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Static Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
When Constructors and Destructors Are Executed . . . . . . . . . . . . . 317
The Scope Resolution Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Local Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Passing Objects to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Object Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

13 Arrays, Pointers, References, and the Dynamic


Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Creating Initialized vs. Uninitialized Arrays . . . . . . . . . . . 328
Pointers to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Type Checking C++ Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
xvi C++: The Complete Reference

The this Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332


Pointers to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Pointers to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Reference Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Passing References to Objects . . . . . . . . . . . . . . . . . . . . . . . . 343
Returning References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Independent References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
References to Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . 346
Restrictions to References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
A Matter of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
C++'s Dynamic Allocation Operators . . . . . . . . . . . . . . . . . . . . . . . . . 347
Initializing Allocated Memory . . . . . . . . . . . . . . . . . . . . . . . 349
Allocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Allocating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
The nothrow Alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
The Placement Form of new . . . . . . . . . . . . . . . . . . . . . . . . . 357

14 Function Overloading, Copy Constructors,


and Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 359
Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Overloading a Constructor to Gain Flexibility . . . . . . . . . . 362
Allowing Both Initialized and Uninitialized Objects . . . . . 364
Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Finding the Address of an Overloaded Function . . . . . . . . . . . . . . . 370
The overload Anachronism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Default Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Default Arguments vs. Overloading . . . . . . . . . . . . . . . . . . 376
Using Default Arguments Correctly . . . . . . . . . . . . . . . . . . . 377
Function Overloading and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . 378

15 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383


Creating a Member Operator Function . . . . . . . . . . . . . . . . . . . . . . . 384
Creating Prefix and Postfix Forms
of the Increment and Decrement Operators . . . . . . . . . . 389
Overloading the Shorthand Operators . . . . . . . . . . . . . . . . . 390
Operator Overloading Restrictions . . . . . . . . . . . . . . . . . . . . 390
Operator Overloading Using a Friend Function . . . . . . . . . . . . . . . . 391
Using a Friend to Overload ++ or – – . . . . . . . . . . . . . . . . . . 393
Friend Operator Functions Add Flexibility . . . . . . . . . . . . . 396
Overloading new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Overloading new and delete for Arrays . . . . . . . . . . . . . . . 403
Overloading the nothrow Version of new and delete . . . . 406
Contents xvii

Overloading Some Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . 407


Overloading [ ] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Overloading ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Overloading –> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Overloading the Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

16 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Base-Class Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Inheritance and protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Protected Base-Class Inheritance . . . . . . . . . . . . . . . . . . . . . . 424
Inheriting Multiple Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Constructors, Destructors, and Inheritance . . . . . . . . . . . . . . . . . . . . 426
When Constructors and Destructors Are Executed . . . . . . 426
Passing Parameters to Base-Class Constructors . . . . . . . . . 430
Granting Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Virtual Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

17 Virtual Functions and Polymorphism . . . . . . . . . . . . . . . 443


Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Calling a Virtual Function Through a Base
Class Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
The Virtual Attribute Is Inherited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Virtual Functions Are Hierarchical . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Pure Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Early vs. Late Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458

18 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
A Function with Two Generic Types . . . . . . . . . . . . . . . . . . . 463
Explicitly Overloading a Generic Function . . . . . . . . . . . . . 463
Overloading a Function Template . . . . . . . . . . . . . . . . . . . . . 466
Using Standard Parameters with Template Functions . . . . 466
Generic Function Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 467
Applying Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
A Generic Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Compacting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
An Example with Two Generic Data Types . . . . . . . . . . . . . 476
Applying Template Classes: A Generic Array Class . . . . . 477
Using Non-Type Arguments with Generic Classes . . . . . . 479
Using Default Arguments with Template Classes . . . . . . . 481
Explicit Class Specializations . . . . . . . . . . . . . . . . . . . . . . . . . 483
xviii C++: The Complete Reference

The typename and export Keywords . . . . . . . . . . . . . . . . . . . . . . . . . 484


The Power of Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

19 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487


Exception Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Catching Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Using Multiple catch Statements . . . . . . . . . . . . . . . . . . . . . . 495
Handling Derived-Class Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Exception Handling Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Catching All Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Restricting Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Rethrowing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Understanding terminate( ) and unexpected( ) . . . . . . . . . . . . . . . . . 503
Setting the Terminate and Unexpected Handlers . . . . . . . . 504
The uncaught_exception( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . 505
The exception and bad_exception Classes . . . . . . . . . . . . . . . . . . . . . 506
Applying Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506

20 The C++ I/O System Basics . . . . . . . . . . . . . . . . . . . . . . . 509


Old vs. Modern C++ I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
C++ Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
The C++ Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
C++'s Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Formatting Using the ios Members . . . . . . . . . . . . . . . . . . . . 513
Setting the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Clearing Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
An Overloaded Form of setf( ) . . . . . . . . . . . . . . . . . . . . . . . . 516
Examining the Formatting Flags . . . . . . . . . . . . . . . . . . . . . . 518
Setting All Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Using width( ), precision( ), and fill( ) . . . . . . . . . . . . . . . . . 520
Using Manipulators to Format I/O . . . . . . . . . . . . . . . . . . . 522
Overloading << and >> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Creating Your Own Inserters . . . . . . . . . . . . . . . . . . . . . . . . . 526
Creating Your Own Extractors . . . . . . . . . . . . . . . . . . . . . . . . 532
Creating Your Own Manipulator Functions . . . . . . . . . . . . 535

21 C++ File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539


<fstream> and the File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Opening and Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Reading and Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Unformatted and Binary I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Characters vs. Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
put( ) and get( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
read( ) and write( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Contents xix

More get( ) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551


getline( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Detecting EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
The ignore( ) Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
peek( ) and putback( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
flush( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Obtaining the Current File Position . . . . . . . . . . . . . . . . . . . 561
I/O Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Customized I/O and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563

22 Run-Time Type ID and the Casting Operators . . . . . . . 567


Run-Time Type Identification (RTTI) . . . . . . . . . . . . . . . . . . . . . . . . . 568
The Casting Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
dynamic_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578

23 Namespaces, Conversion Functions,


and Other Advanced Topics . . . . . . . . . . . . . . . . . . . . . 591
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
Namespace Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Unnamed Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Some Namespace Options . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
The std Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Creating Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
const Member Functions and mutable . . . . . . . . . . . . . . . . . . . . . . . . 607
Volatile Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Explicit Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
The Member Initialization Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Using the asm Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Linkage Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
Array-Based I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
The Array-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Creating an Array-Based Output Stream . . . . . . . . . . . . . . . 619
Using an Array as Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Input/Output Array-Based Streams . . . . . . . . . . . . . . . . . . . 623
Using Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Using Binary I/O with Array-Based Streams . . . . . . . . . . . 625
Summarizing the Differences Between C and C++ . . . . . . . . . . . . . 626

24 Introducing the Standard Template Library . . . . . . . . . . 629


An Overview of the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
xx C++: The Complete Reference

Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Other STL Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
General Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Accessing a Vector Through an Iterator . . . . . . . . . . . . . . . . 639
Inserting and Deleting Elements in a Vector . . . . . . . . . . . . 641
Storing Class Objects in a Vector . . . . . . . . . . . . . . . . . . . . . . 643
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Understanding end( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
push_front( ) vs. push_back( ) . . . . . . . . . . . . . . . . . . . . . . . . 651
Sort a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Merging One List with Another . . . . . . . . . . . . . . . . . . . . . . 653
Storing Class Objects in a List . . . . . . . . . . . . . . . . . . . . . . . . 655
Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Storing Class Objects in a Map . . . . . . . . . . . . . . . . . . . . . . . 662
Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Removing and Replacing Elements . . . . . . . . . . . . . . . . . . . 670
Reversing a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Transforming a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Using Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Unary and Binary Function Objects . . . . . . . . . . . . . . . . . . . 675
Using the Built-in Function Objects . . . . . . . . . . . . . . . . . . . 675
Creating a Function Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
Using Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
The string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Some string Member Functions . . . . . . . . . . . . . . . . . . . . . . . 687
Strings Are Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Putting Strings into Other Containers . . . . . . . . . . . . . . . . . 694
Final Thoughts on the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

Part III
The Standard Function Library

25 The C-Based I/O Functions . . . . . . . . . . . . . . . . . . . . . . . 699


clearerr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
fclose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
feof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
ferror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
fflush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
fgetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
fgetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Contents xxi

fgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
fopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
fprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
fputc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
fputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
fread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
freopen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
fscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
fseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
fsetpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
ftell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
fwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
getc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
getchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
gets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
perror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
putc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
puts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
rewind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
setbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
setvbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
tmpfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
tmpnam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
ungetc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
vprintf, vfprintf, and vsprintf . . . . . . . . . . . . . . . . . . . . . . . . 722

26 The String and Character Functions . . . . . . . . . . . . . . . . 723


isalnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
isalpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
iscntrl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
isdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
isgraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
islower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
isprint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
ispunct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
isspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
isupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
xxii C++: The Complete Reference

isxdigit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
memchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
memcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
memcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
memmove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
memset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
strchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
strcoll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
strcspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
strerror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
strncat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
strncmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732
strncpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
strpbrk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
strrchr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
strspn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
strstr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
strtok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
strxfrm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735

27 The Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 737


acos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
asin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
atan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
atan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
cosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
fabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
fmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
frexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
ldexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
log10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
modf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Contents xxiii

sinh ............................................... 744


sqrt ............................................... 744
tan ............................................... 744
tanh .............................................. 745

28 Time, Date, and Localization Functions . . . . . . . . . . . . . 747


asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
ctime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
difftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
gmtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
localeconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
localtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
mktime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
setlocale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
strftime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754

29 The Dynamic Allocation Functions . . . . . . . . . . . . . . . . . 757


calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
realloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759

30 Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761


abort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
abs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
atexit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
atof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
atoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
atol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
bsearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
getenv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
labs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
ldiv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
longjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
mblen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
mbstowcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
mbtowc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
raise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
xxiv C++: The Complete Reference

rand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
setjmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
srand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
strtod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
strtol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
strtoul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
va_arg, va_start, and va_end . . . . . . . . . . . . . . . . . . . . . . . . . 773
wcstombs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
wctomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774

31 The Wide-Character Functions . . . . . . . . . . . . . . . . . . . . . 775


The Wide-Character Classification Functions . . . . . . . . . . . . . . . . . . 776
The Wide-Character I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 779
The Wide-Character String Functions . . . . . . . . . . . . . . . . . . . . . . . . . 779
Wide-Character String Conversion Functions . . . . . . . . . . . . . . . . . . 779
Wide-Character Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
Multibyte/Wide-Character Conversion Functions . . . . . . . . . . . . . . 783

Part IV
The Standard C++ Class Library

32 The Standard C++ I/O Classes . . . . . . . . . . . . . . . . . . . . 787


The I/O Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
The I/O Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
The Format Flags and I/O Manipulators . . . . . . . . . . . . . . . . . . . . . . 791
Several Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
The streamsize and streamoff Types . . . . . . . . . . . . . . . . . . . 793
The streampos and wstreampos Types . . . . . . . . . . . . . . . . 793
The pos_type and off_type Types . . . . . . . . . . . . . . . . . . . . . 793
The openmode Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
The iostate Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
The seekdir Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
The failure Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
Overload << and >> Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
The General-Purpose I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . 795
bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
eof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Contents xxv

flush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
fstream, ifstream, and ofstream . . . . . . . . . . . . . . . . . . . . . . . 797
gcount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
ignore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
peek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
put . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
putback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
rdstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
readsome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
seekg and seekp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
setf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
setstate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
stringstream, istringstream, ostringstream . . . . . . . . . . . . . 806
sync_with_stdio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
tellg and tellp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
unsetf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809

33 The STL Container Classes . . . . . . . . . . . . . . . . . . . . . . . . 811


The Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
bitset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
multimap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
multiset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
priority_queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834

34 The STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839


adjacent_find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
binary_search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
xxvi C++: The Complete Reference

copy_backward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
count_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
equal_range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
fill and fill_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
find_end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
find_first_of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
find_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
for_each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
generate and generate_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
inplace_merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
iter_swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
lexicographical_compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
lower_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
make_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
max_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
min_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
next_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
nth_element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
partial_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
partial_sort_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
pop_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
prev_permutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
push_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
random_shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
remove, remove_if, remove_copy, and remove_copy_if . 852
replace, replace_copy, replace_if, and replace_copy_if . . . 852
reverse and reverse_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
rotate and rotate_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
search_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
set_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
set_intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
set_symmetric_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
set_union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
sort_heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
Contents xxvii

stable_partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
stable_sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
swap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
swap_ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
unique and unique_copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
upper_bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859

35 STL Iterators, Allocators, and Function Objects . . . . . . 861


Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
The Basic Iterator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
The Low-Level Iterator Classes . . . . . . . . . . . . . . . . . . . . . . . 863
The Predefined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864
Two Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
Negators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875
Adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876
Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879

36 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881


The basic_string Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
The char_traits Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894

37 The Numeric Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897


The complex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
The valarray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902
The slice and gslice Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 917
The Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
The Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
adjacent_difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921
inner_product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
partial_sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923

38 Exception Handling and Miscellaneous Classes . . . . . . 925


Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
<exception> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
<stdexcept> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
The pair Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
Other Classes of Interest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
xxviii C++: The Complete Reference

Part V
Applying C++

39 Integrating New Classes: A Custom String Class . . . . . 935


The StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
The Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
I/O on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
The Assignment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Substring Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
The Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
Miscellaneous String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
The Entire StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
Using the StrType Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
Creating and Integrating New Types in General . . . . . . . . . . . . . . . 961
A Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961

40 Parsing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963


Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964
Parsing Expressions: The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Parsing an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
The Parser Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
Dissecting an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
A Simple Expression Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
Understanding the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
Adding Variables to the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Syntax Checking in a Recursive-Descent Parser . . . . . . . . . . . . . . . . 988
Building a Generic Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989
Some Things to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997

A The .NET Managed Extensions to C++ . . . . . . . . . . . . . . 999


The .NET Keyword Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
Preprocessor Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
The attribute Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
Compiling Managed C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003

B C++ and the Robotics Age . . . . . . . . . . . . . . . . . . . . . . . . 1005

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
f there is one language that defines modern programming, it is C++. Its syntax,

I style, and philosophy have set the standard by which all other languages are
judged. Furthermore, C++ is the universal language of programming. When an
algorithm or technique is described, it is usually done so using the C++ syntax. The
long-term success of C++ has also left a lasting impression on computer language
development. For example, both Java and C# are descended from C++. Frankly, to be
a professional programmer implies proficiency in C++. It is the one language that no
programmer can afford to ignore.
This is the fourth edition of C++: The Complete Reference. It fully describes and
demonstrates the keywords, syntax, functions, classes, and features that define the C++
language. More specifically, this book fully describes Standard C++. This is the version
of C++ defined by the ANSI/ISO Standard for C++ and it is the version of C++ that is
supported by all major compilers, including Microsoft’s Visual C++ and Borland’s C++
Builder. Thus, the information in this book is applicable to all modern programming
environments.
In the time that has passed since the previous edition of this book, there have
been no changes to the C++ language. There have, however, been big changes to the
computing environment. For example, a new standard for C, called C99, was created,
Java became the dominant language for Web programming, the .NET Framework was

xxix
Copyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.
xxx C++: The Complete Reference

released, and C# was invented. Through all the changes of the past few years, one thing
has remained constant: the staying power of C++. C++ has been, is, and will remain the
preeminent language for the development of high-performance software well into the
foreseeable future.

What’s New in the Fourth Edition


The overall structure and organization of the fourth edition is similar to the third edition.
Thus, if you have been using the third edition, you will feel right at home with the fourth
edition. Most of the changes to the fourth edition involve updating and expanding the
coverage throughout. In some cases, additional details were added. In other cases, the
presentation of a topic was improved. In still other situations, descriptions were
modernized to reflect the current programming environment. Several new sections were also
added. In Part One, the relationship of C++ to the new C standard, called C99, is noted
where appropriate.
Two appendices were also added. The first described the extended keywords
defined by Microsoft that are used for creating managed code for the .NET Framework.
The second shows off an area of personal interest: robotics. Robotics has long been a
hobby of mine and I thought that many readers would find my experimental robot to
be of interest. Most of the software that drives it is, of course, written in C++!
Finally, all code examples were retested against the current crop of compilers,
including Microsoft’s Visual Studio .NET and Borland’s C++ Builder.

What’s Inside
This books covers in detail all aspects of the C++ language, including its foundation, C.
The book is divided into these five parts:

■ The C Subset—The foundation of C++


■ The C++ language
■ The Standard Function Library
■ The Standard Class Library
■ Sample C++ applications

Part One provides a comprehensive discussion of the C subset of C++. As most


readers will know, C is the foundation upon which C++ was built. It is the C subset
that defines the bedrock features of C++, including such things as for loops and if
statements. It also defines the essential nature of C++’s block structure, pointers, and
functions. Since many readers are already familiar with and proficient in C, discussing
the C subset separately in Part One prevents the knowledgeable C programmer from
having to “wade through” reams of information he or she already knows. Instead, the
xxxi

experienced C programmer can simply turn to the sections of this book that cover the
C++-specific features.
Part Two discusses in detail the features that move beyond the C foundation and
define the C++ language These include its object-oriented features such as classes,
constructors, destructors, RTTI, and templates. Thus, Part Two covers those constructs
that “make C++, C++.”
Part Three describes the standard function library and Part Four examines the
standard class library, including the STL (Standard Template Library). Part Five
shows two practical examples of applying C++ and object-oriented programming.

A Book for All Programmers


This C++ reference is designed for all C++ programmers, regardless of their experience
level. It does assume, however, a reader able to create at least a simple program. If you
are just learning C++, this book will make an excellent companion to any C++ tutorial
and serve as a source of answers to your specific questions. Experienced C++ pros will
find the in-depth coverage of C++’s more advanced features especially useful.

If You’re Using Windows


If your computer uses Windows, then you have chosen the right language. C++ is
completely at home with Windows programming. However, none of the programs in
this book are Windows programs. Instead, they are console-based programs. The reason for
this is easy to understand: Windows programs are, by their nature, large and complex. The
overhead required to create even a minimal Windows skeletal program is 50 to 70 lines
of code. To write Windows programs that demonstrate the features of C++ would
require hundreds of lines of code each. Put simply, Windows is not an appropriate
environment in which to discuss the features of a programming language. However,
you can still use a Windows-based compiler to compile the programs in this book
because the compiler will automatically create a console session in which to execute
your program.

Don’t Forget: Code on the Web


Remember, the source code for all of the programs in this book is available free-of-
charge on the Web at www.osborne.com. Downloading this code prevents you from
having to type in the examples.
xxxii C++: The Complete Reference

For Further Study


C++: The Complete Reference is your gateway to the Herb Schildt series of
programming books. Here are some others that you will find of interest.

To learn more about C++, try

C++: A Beginner’s Guide


C++ from the Ground Up
Teach Yourself C++
STL Programming from the Ground Up
C++ Programmer’s Reference

To learn about Java programming, we recommend the following:

Java 2: A Beginner’s Guide


Java 2: The Complete Reference
Java 2 Programmer’s Reference

To learn about C#, Herb offers these books:

C#: A Beginner’s Guide


C#: The Complete Reference

To learn about Windows programming we suggest the following Schildt books:

Windows 98 Programming from the Ground Up


Windows 2000 Programming from the Ground Up
MFC Programming from the Ground Up
The Windows Programming Annotated Archives

If you want to learn about the C language, which is the foundation of all modern
programming, then the following titles will be of interest.

C: The Complete Reference


Teach Yourself C

When you need solid answers, fast, turn to Herbert Schildt,


the recognized authority on programming.
Part I
The Foundation of C++:
The C Subset

This book divides its description of the C++ language into two parts.
Part One discusses the C-like features of C++. This is commonly
referred to as the C subset of C++. Part Two describes those features
specific to C++. Together, these parts describe the entire C++ language.

Copyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.
Other documents randomly have
different content
wurde, war geradezu kläglich. Nun aber macht sich eine Bewegung
bemerklich, die erkennen läßt, daß man bestrebt ist, „den Kindern
das Beste“ zu bieten. Und hier haben nun eine Anzahl von
Verlegern, vor allem die Wigands, im Verein mit unserem Künstler
mit sicherer und glücklichster Hand eingegriffen und wirklich wie im
Sprunge Versäumtes nachzuholen sich bemüht. Ihr Bestreben war
aber auch vom schönsten Erfolg gekrönt, die Bücher mit Bildern von
unserem Ludwig Richter wurden von alt und jung mit Jubel
aufgenommen.

Abb. 62. A n d e r K r i p p e . Aus „Illustrierte


Jugendzeitung“. 1847.
Verlag von Otto Wigand in Leipzig. (Zu Seite 59.)
Abb. 63. R ü b e z a h l . Radierung. 1848.
Verlag von Alphons Dürr in Leipzig. (Zu Seite
58.)

Außer einer Anzahl von Blättern für Lithographie zu Erzählungen


von Karl Stöber folgen 1842 bis 1856 siebzig Zeichnungen für
Radierungen und Holzschnitt zu Nieritz’ Volkskalender. Die sehr
vollendeten Zeichnungen: „Harmlose Freude“ (Abb. 47) und „Wir
gratulieren“ (Abb. 48), radierte Hugo Bürkner. Zu Jeremias Gotthelfs
Erzählung „Der Besenbinder“ ist die Zeichnung Abb. 49. Unter
diesen Blättern befinden sich auch die ungemein humoristischen
Weinproben vom Most, Rheinwein, Burgunder, Steinwein bis herab
zum Grüneberger. Hieran reiht sich noch ein Ölbild nach einem
Motiv aus Böhmen, „Hirten mit der Herde durchs Wasser gehend“,
im Besitz des Herrn Hoff in Frankfurt a. M.

Abb. 64. B e t e n d e s K i n d . Aus „Illustrierte


Jugendzeitung“. 1847. Keils Märchen. Verlag
von Otto Wigand in Leipzig. (Zu Seite 59.)
1844–1846 erschienen bei Gustav Mayer in Leipzig „Alte und
neue Studenten- und Volkslieder“ mit hunderteinunddreißig Bildern.
Die ebenda erschienenen „Soldaten- und Jägerlieder“ hatte Pocci,
vor dessen Geschicklichkeit Richter großen Respekt hatte, illustriert.
Franz Graf Pocci, 1807 in München als Sohn des Grafen Pocci aus
Viterbo und der Freiin Xaveria von Posch aus Dresden geboren, war
Dichter, Zeichner und Musiker, später Hofmusikintendant in
München. Die geschickte Art, wie er in den obenerwähnten
Soldaten- und Jägerliedern usw. die Buchseiten mehr „dekorierte“,
war für unseren jungen Meister sehr anregend und fördernd. Diese
Illustrationen sind ebenso wie die Neureuthers von großem Einfluß
auf ihn gewesen. Poccis Arbeiten, soweit sie hier in Betracht
kommen, zeugen trotz ihres dilettantischen Gepräges und trotz des
Mangels an Können doch von einem kindlich naiven Sinn. Der
Meister erwähnt selbst Pocci in der Biographie; er sieht dessen mit
Guido Görres herausgegebenen Festkalender, als er noch in Meißen
war, und sagt: „Pocci interessierte mich doch bei weitem am meisten
und wirkte höchst anregend auf mich.“ Wie hat es nun unser Richter
verstanden, in dem engen gegebenen Raum, der für ihn neben
Noten und Text übrig blieb, bei aller Freiheit hauszuhalten und in den
kleinen Illustrationen bei so schlichter Form den geheimnisvollen
Zauber unserer Volkslieder, ebenso wie die Frische und Fröhlichkeit
und den Humor unserer Studentenlieder wiederzugeben! Von den
Volksliedern seien genannt: „Zu Lauterbach hab’ i mein Strumpf
verloren“, Abb. 50. Das „Liebespaar“ mit dem lieblichen Mädchen,
welches das Licht putzt (Abb. 51), wurde erst 1875 in „Aus der
Jugendzeit“ veröffentlicht. Eine so innige Gruppe, wie das
Liebespaar, das in sein stilles Glück versunken in die Ferne
hinausschaut, zu „Kein Feuer, keine Kohle kann brennen so heiß, als
heimliche Liebe, von der niemand nicht weiß“ kann nur unser Richter
zeichnen. Voller Humor sind „Nur fröhliche Leute lassen wir herein“
(Abb. 52) und „Gestern, Brüder, könnt ihr’s glauben ... gestern kam
der Tod zu mir“ (Abb. 53). Wie ergreifend sind die Bilder: „Es zogen
drei Burschen wohl über den Rhein“ und „Es fiel ein Reif in der
Frühlingsnacht“ (Abb. 54), wie innig die Gruppe der beiden Kinder,
die „verdorben, gestorben“! Ferner das prächtige Bildchen: „So hab’
ich nun die Stadt verlassen, wo ich gelebet lange Zeit“, — in
Gedanken versunken wandert der Bursch aus der Stadt, er denkt an
die „eine“, sie aber denkt auch an ihn, und wie lieblich und hold ist
sie, die oben, hinter dem Vorhang lauschend, dem Wandernden
sinnend nachschaut (Abb. 55). — Das liegende Mädchen, eine
schön gezeichnete Studie (Abb. 56) zu „Wenn ich ein Vöglein wär“
erregte Rethels größtes Interesse; er betrachtete oft mit besonderem
Wohlgefallen diese fein empfundene naive Zeichnung. — Diese
Volks- und Studentenliederbilder sind 1875–1878 in vier Bänden, mit
Illustrationen zu Musäus’ Volksmärchen durchsetzt, wieder im
Buchhandel erschienen unter den Titeln: „Aus der Jugendzeit“,
„Deutsche Art und Sitte“, „Aus dem Volksleben“ und „Aus Dichtung
und Sage“. 1845 vollendete Richter ein Ölbild, einen
Mondscheinabend, für Bendemann in Berlin. Aus dem in demselben
Jahre erschienenen „Vaterunser“ von Ammon bringen wir die
reizende Rehgruppe „Ruhe im Walde“ (Abb. 57). Ein Ölbild, das ihn
so recht kennzeichnet: den „Brautzug im Frühling“ (Abb. 58)
vollendet er 1847. Aus dem im Frühlingsschmuck prangenden Walde
tritt der festliche Hochzeitszug, des Müllers Töchterlein mit ihrem
Angetrauten, der stattliche Müller mit der stillen Frau Müllerin und
weiteres Gefolge, voran fröhliche Kinder mit Blumengewinden. Im
Mittelgrund eine liebliche Hirtengruppe, ein Hirtenbübel schwenkt
den Hut. Vom hohen Giebel der unterhalb des Schlosses liegenden
Mühle weht eine stattliche Fahne, im jungen Tannenwald lauschen
Rehe, draußen stille, blaue Fernen. Ein seltener Liebreiz mutet uns
beim Betrachten dieses hinsichtlich der Konzeption schönsten Bildes
Richters an, es macht Haydnsche, auch Mozartsche Weisen in uns
erklingen. Interessant ist ein Vergleich dieses Bildes mit seinem
hervorragendsten italienischen Bild „Tal von Amalfi“ (Abb. 13). Die
Anregung zu diesem Bilde wurde ihm bei der Erstaufführung von
Wagners Tannhäuser 1845. Auf der Ausstellung in Dresden erwarb
es die Lindenaustiftung und überwies es der dortigen
Gemäldegalerie. Die neue deutsche Kunst, die in Rom durch
Cornelius, Overbeck, Schnorr und Veit in der Mitte des zweiten
Dezenniums des 19. Jahrhunderts einsetzte, war eine Sezession
radikalster Art; durch sie waren alle Traditionen der Ölmaltechnik
ebenso wie der Freskotechnik durchschnitten worden und schließlich
verloren gegangen. Es hat in Deutschland trotz allen Ringens einer
langen Reihe von Jahren bedurft, bis man dies erkannte und der
Technik in der Malerei wieder den ihr gebührenden Platz einräumte,
die Art des Studienganges änderte und nicht nur einseitig vom
Umriß und von der Zeichnung ausging, sondern auch die Farben zu
Wort kommen ließ, bei der Komposition auch mit Ton- und
Farbenwerten rechnete. Die Unzulänglichkeit des technischen
Könnens im Ölmalen empfand Richter oft genug. Er sprach sich in
späteren Jahren oft dahin aus, daß ihm ursprünglich ein ganz früher
Frühlingstag vorgeschwebt habe, er wollte den Wald in seinem
ersten Lenzesschmuck, knospende und blühende Bäume, die
Eichen mit dem lichten zarten Grün der jungen Blättchen, kurz, einen
wonnigen ersten Frühlingstag in dem Bilde schildern, aber die Kraft
dazu habe ihm versagt, die Studien dazu gefehlt. Auf der
Weltausstellung in Paris 1855 wurde ihm für dieses Bild die goldene
Medaille zuerkannt; der Bildhauer E. Rietschel wurde dort ebenso
ausgezeichnet; die Künstlerschaft Dresdens brachte beiden
Männern daraufhin einen Fackelzug.
Abb. 65. Zu: D a s K i n d a n d e r M u t t e r
Grab.
Aus „Illustrierte Zeitung für die Jugend“. 1849. (Zu
Seite 59.)

Abb. 66. Z u H i m m e l s m ü t t e r l e i n . Aus


„Die schwarze Tante“. 1848.
Verlag von Georg Wigand in Leipzig. (Zu Seite
60.)
1846 vollendet er ein Frühlingsbild „Mädchen am Brunnen“, das
er 1849 variiert als Radierung wiederholte (Abb. 78). Er sprach oft
den Wunsch aus, dieses Bild einmal wiederzusehen; es schien, als
erinnere er sich dessen mit einer gewissen Befriedigung. Es folgen
die „Hymnen für Kinder“ von Thekla von Gumpert mit sechzehn
Zeichnungen.
Vielleicht 1846 hat der Meister seine älteste Tochter Maria
gezeichnet (Abb. 60), die im April 1847 an einem unheilbaren
Brustleiden im blühenden Alter von achtzehn Jahren starb. Inmitten
einer Zeit voll reichen Schaffens durchweht tiefe Trauer sein Herz
und das seiner Frau; sie sehen, wie die geliebte Tochter nach und
nach hinsiecht, der Arzt weiß keine Rettung mehr und gibt alle
Hoffnung auf. „Erschüttert und tiefgebeugt knieen die Eltern am Bett
und begleiten die erlöste Seele unter Tränen mit ihren Gebeten in
das Jenseits!“ Mit diesen Worten schließt Richter seine Biographie
ab; er konnte sich nicht entschließen, dieselbe weiterzuführen, und
fügt derselben dann nur noch Tagebuchnotizen als Anhang bei.
Abb. 67. Z u H i m m e l s m ü t t e r l e i n .
(Zu Seite 60.)

1848 starb, siebzigjährig, Richters Vater; sein arbeitsreiches


Leben war zu Ende; viel Mühsale und unverdiente Kränkungen hatte
er getragen; sein Lebensabend gestaltete sich etwas freundlicher,
tätig war er bis zum Tage vor seiner letzten Erkrankung. Er hatte
wohl nicht so recht den Platz in seiner Kunst gefunden, für den er
von Haus aus bestimmt und geeignet war. Unser Meister sprach oft
davon, wie schon in seinem Vater unverkennbar die Neigungen
durchblickten, die ihn, den Sohn, auf seinen Weg brachten, und daß,
wenn die äußeren Verhältnisse des Vaters günstigere gewesen
wären, dieser als Landschaftsmaler gewiß Bedeutendes geleistet
haben würde, mehr als in der Kupferstecherei, zu der er entschieden
weniger Veranlagung hatte. Schon in des Vaters Zeichnungen war
die Art der Staffage abweichend von der landläufigen Manier; was
von den wenigen Zeichnungen bekannt ist — seine meisten Blätter
sind, in der Hauptsache unter Zinggs Namen, nach Polen gewandert
— bestätigt dies. In demselben Jahre starb auch unseres Meisters
jüngster Bruder Julius in Warschau an der Cholera; er war dort seit
vielen Jahren als Aquarellmaler tätig gewesen. Der zweitälteste
Bruder Willibald, der während einer Reihe von Jahren die Gräfin
Potocka auf deren Reisen durch Europa als Zeichner und
Aquarellmaler begleitet hatte, lebte in Wien. Sein Zeichenunterricht
war in den dortigen Hofkreisen sehr geschätzt und gesucht; er starb
kinderlos in Wien 1880. Die einzige Schwester Richters, Hildegard,
die sehr tüchtig im Blumenmalen war, starb als Witwe des
Kunstgärtners Ludwig Liebig in Dresden an ihrem 90. Geburtstage
1898.
Abb. 68. A u s d e r S c h m i e d e . Schmiedjakobs
Geschichten von Horn. 1852.
(Zu Seite 61.)
Abb. 69. A u s d e r S c h m i e d e . (Zu
Seite 61.)

1847 und 1848 entstanden die beiden Kompositionen


„Genoveva“ und „Rübezahl“, die Richter dann im Auftrage des
Sächsischen Kunstvereins so meisterhaft radierte. Frieden des
Waldes atmet das Blatt „Genoveva“. Vor der von lauschigem Wald
umschlossenen Höhle sitzt im Sonnenschein die sinnige liebliche
Frauengestalt, in ihren Schoß gelehnt ihr zur Seite
Schmerzensreich, die Hirschkuh liebkosend; im Rasen wilde
Tauben, Spechte und allerhand andere Vögel, auch Häschen haben
sich zutraulich gelagert, Eichhörnchen tummeln sich; im Vorgrund,
mit Erdbeeren besäumt, ein frisches Wässerchen, das unter mit
Farnen bewachsenen Steinen hervorsprudelt; über der Höhle gegen
dunklen Tannenwald schreiten Hirsch und Hirschkuh. Es ist eine
köstliche Waldidylle, wie sie schöner nicht gezeichnet werden kann.
Wir geben hier (Abb. 59) eine Variante der lieblichen Figurengruppe
mit einfacherem landschaftlichen Hintergrund nach einer Aquarelle
von 1850 in farbiger Reproduktion. Die Genoveva ist auch hier innig
und lieblich; wie seelenvoll ist der Ausdruck des Kopfes der Dulderin!
Auch in der Art wie der Meister die Farben nur andeutend sprechen
und wirken läßt und worin er so unerreicht und einzig ist, mutet uns
das Bild so außerordentlich wohltuend an. Zu dem aufgelösten Haar
der Genoveva zeichnete er eine Studie nach seiner Tochter Aimée
mit wenigen Bleistiftstrichen (Abb. 61). Die andere Komposition „Der
Rübezahl“ (Abb. 63) schildert den bekannten Vorwurf: die Mutter ruft,
um einen ihrer Schreier zum Schweigen zu bringen, Rübezahl, er
möge ihn mitnehmen, da — plötzlich steht Rübezahl vor ihr und
fordert das Kind. Wie die Küchlein bei drohender Gefahr sich zur
Henne flüchten und sich zu verbergen suchen, so schmiegen sich
die erschrockenen Kinder schutzsuchend an die ebenso
erschrockene Mutter, die die schützenden Arme um sie schließt und
betroffen, aber doch der Gefahr trotzend, den bärtigen „Rübezahl“
anstarrt; nur den kleinen an der Erde liegenden Schreihals kümmert
Rübezahl nicht, er schreit und strampelt fort; als Kuriosum sei hier
erwähnt, daß das am Boden liegende Kind zwei rechte Füße hat.
Die über den Figuren sich erhebende Gruppe von Bäumen ist von
großer Schönheit. Über sonnige Höhen schaut man auf in duftigem
Blau liegende Bergzüge, am schattigen Waldesrand lagert Rotwild.
Das ist Bergespoesie! Die Gestalt des Rübezahl, um die Hauptsache
nicht zu vergessen, ist voller Humor: halb drohend, aber mit dem
Ausdruck eines gutmütigen Schalks, einen entwurzelten Baum in der
Linken haltend, die Rechte nach den Kindern ausgestreckt, als
Köhler gekleidet, steht er mit gespreizten nackten Beinen vor der
erschrockenen Mutter. Er gehört mit der ebenso komischen
Rübezahlgestalt von Schwind, die leider so wenig bekannt ist, zu
den weitaus besten Darstellungen dieser urwüchsigen Figur aus
dem Sagenkreise des Riesengebirges. Denselben Gegenstand hatte
Richter schon früher im „Musäus“ ähnlich behandelt. Er war gerade
beim Ätzen dieser beiden Kupferplatten, als Kanonendonner und
Knattern von Gewehrsalven die Luft erdröhnen machten; preußische
Regimenter halfen den 1849er Maiaufstand in Dresden, das infolge
des Feldzugs in Schleswig-Holstein von Militär fast entblößt war,
niederzuwerfen; unser Richter stand im unvermeidlichen Hauspelz
an seinem Ätztisch in seine Arbeit vertieft und kümmerte sich nicht
um das, was in den Straßen der Stadt sich abspielte. In
Kupferstecher Thäters Biographie finden wir von 1848 folgende
Aufzeichnung: „Wer hätte wohl noch vor wenigen Wochen sich
träumen lassen, daß die beiden friedliebendsten Menschen, Richter
und ich, einem ‚Deutschen Verein‘ und einer ‚Akademischen Legion‘
beitreten und täglich zwei Stunden mit dem Schießprügel sich
tummeln würden? Wir hätten eher daran geglaubt, ins Gras, statt in
Patronen beißen zu müssen. Und doch konnte es nicht umgangen
werden; wir müssen eben mit fort, wie jeder andere auch.“ Von den
Dienstleistungen der „Akademischen Legion“, die in dieser
wunderlichen Zeit gebildet worden war und die nicht „zu martialisch“
in ihrer ganzen Erscheinung gewesen sein soll, wurde er aber
enthoben. Das Exerzieren war ihm recht unbequem, wie man sich
leicht denken kann. Kurz vor Ausbruch des Maiaufstandes wurde
des Meisters Sohn Heinrich, mit einem Freunde auf einer
Wanderung nach Meißen begriffen, in Dresden-Neustadt, weil er
eine, wenn auch verrostete Waffe unter dem Rock verborgen trug,
vom Militärposten abgefaßt und mit vielen anderen Sistierten in der
Frauenkirche gefangen gehalten. Unser Meister wurde aus seiner
stillen künstlerischen Tätigkeit herausgerissen, als er diese
Nachricht erhielt; mit größter Mühe und nur durch schwerwiegende
Fürsprache gelang es, die an sich so harmlosen Arrestanten erst
nach zwei schweren langen Tagen wieder frei zu machen. Für
unseren Meister waren es Tage großer Aufregung.
Für die Illustrierte Jugendzeitung Otto Wigands und die Illustrierte
Zeitung für die Jugend (Brockhaus) zeichnete Richter 1846 bis 1852
achtundachtzig Blätter; wir bringen davon die Abbildungen 62, 64
und 65, von denen besonders die beiden letzten Blätter durch ihr
überaus warmes Empfinden hervorragen. Weiter folgen eine Reihe
von Illustrationen zu verschiedenen Jugendschriften, 1847 bis 1853,
auch unter anderen bei Justus Naumann in Dresden „W.
Redenbachers neueste Volksbibliothek“, wozu Richter siebzehn
Zeichnungen lieferte, die lithographisch vervielfältigt wurden. Unter
diesen Zeichnungen, die hier als Vorlage für den Lithographen
dienten, sind eine Reihe ganz vorzüglicher Blätter, von größter
Vollendung in der Zeichnung und lebendigster Charakteristik. Es sei
hier nur das eine Blatt genannt: „Wie Parzival von seiner Mutter in
der Wildnis Soltane erzogen wird“, vom Jahre 1853. 1848 erscheint
„Robinson der Jüngere“ bei Fr. Vieweg in Braunschweig, mit
achtundvierzig Holzschnitten. Wer von uns hätte diesen Robinson in
jungen Jahren nicht in der Hand gehabt und sich daran begeistert!
Abb. 70. T i t e l b l a t t z u R h e i n i s c h e
D o r f g e s c h i c h t e n von Horn. 1854.
(Zu Seite 61.)

Gleichzeitig erscheint die erste Ausgabe des „Richter-Album“ bei


Georg Wigand, hundertfünfzehn Blatt Holzschnitte, die in
vorhergehenden Werken bereits publiziert waren. Diese Ausgabe
hat zur Verbreitung Richterscher Kunst in den weitesten Kreisen und
Schichten des deutschen Volkes ungemein viel beigetragen. Es war
eine glückliche Idee, diese Bilder so zwanglos aneinanderzureihen
und, mit kleinen Textstellen versehen, noch zugänglicher für das
allgemeine Verständnis zu machen. Sehr bald folgt ein zweiter Band
mit hundertachtundfünfzig Blatt. Seitdem ist diese Sammlung wohl
fünf- oder mehrmal aufgelegt worden.

Abb. 71. Z u D e r G e i z h a l s u n d s e i n
N a c h b a r.
Spinnstubengeschichten von Horn. 1855. (Zu Seite
61.)
Abb. 72. Z u Z w e i g e h o l z t e O h r f e i g e n .
Spinnstubengeschichten von Horn. 1856. (Zu Seite
61.)
Abb. 73. Zu D e r S c h l e i c h e r .
Spinnstubengeschichten von Horn.
1856. (Zu Seite 61.)
Abb. 74. Zu J ö r j a k o b . Spinnstubengeschichten von Horn.
1860. (Zu Seite 62.)

Aus dem Buch „Die Schwarze Tante“, Märchen und Geschichten


für Kinder (von Frau Professor Fechner in Leipzig), mit
vierundvierzig der liebenswürdigsten Illustrationen, erschienen 1848
bei Georg Wigand, sind zwei Bilder zum „Himmelsmütterlein“
besonders reizvoll. Die sterbende Mutter nimmt Abschied von ihrem
Töchterchen und ermahnt es, brav zu bleiben (Abb. 66), und weiter
der Traum des Kindes (Abb. 67): das verstorbene Mütterlein
erscheint dem in der dunklen Kammer eingesperrten Kinde, nimmt
es zu sich auf den Schoß und erzählt ihm vom himmlischen
Paradies. — 1849 bis 1860 folgen die Illustrationen zur „Spinnstube“
und zu „Gesammelte Erzählungen“, ferner zu „Des alten
Schmiedjakob Geschichten“ und zu den „Rheinischen
Dorfgeschichten“, sämtlich herausgegeben von W. O. von Horn; im
ganzen fünfhundertvierundsiebzig Zeichnungen. 1873 erschien der
weitaus größte Teil derselben in einer Separatausgabe bei
Sauerländer in Frankfurt am Main. Die Verlagshandlung hatte
Richter alle Holzschnitte vorher zur Durchsicht eingesendet, der
Meister zog mich zur Auswahl zu; für manches dieser köstlichen

You might also like