Download C++ - The Complete Reference 4th Ed. and more Study notes C programming in PDF only on Docsity!
C++:
The Complete Reference,
Fourth Edition
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-
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 george_hoare@mcgraw-hill.com 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/
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
13 Arrays, Pointers, References, and the Dynamic
This page intentionally left blank
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
For more information about this title, click here
Part II
- Allocation Operators
- and Default Arguments 14 Function Overloading, Copy Constructors,
- 15 Operator Overloading
- 16 Inheritance
- 17 Virtual Functions and Polymorphism
- 18 Templates
- 19 Exception Handling
- 20 The C++ I/O System Basics
- 21 C++ File I/O
- 22 Run-Time Type ID and the Casting Operators
- and Other Advanced Topics 23 Namespaces, Conversion Functions,
- 24 Introducing the Standard Template Library
- 25 The C-Based I/O Functions Part III The Standard Function Library
- 26 The String and Character Functions
- 27 The Mathematical Functions
- 28 Time, Date, and Localization Functions
- 29 The Dynamic Allocation Functions
- 30 Utility Functions
- 31 The Wide-Character Functions
- 32 The Standard C++ I/O Classes Part IV The Standard C++ Class Library
- 33 The STL Container Classes
- 34 The STL Algorithms
- 35 STL Iterators, Allocators, and Function Objects
- 36 The String Class
- 37 The Numeric Classes
- 38 Exception Handling and Miscellaneous Classes - Formal Parameters - Global Variables
- The const and volatile Qualifiers
- Storage Class Specifiers
- extern
- static Variables
- register Variables
- Variable Initializations
- Constants
- Hexadecimal and Octal Constants
- String Constants
- Backslash Character Constants
- Operators
- The Assignment Operator
- Type Conversion in Assignments
- Multiple Assignments
- Arithmetic Operators
- Increment and Decrement
- Relational and Logical Operators
- Bitwise Operators
- The? Operator
- The & and * Pointer Operators
- The Compile-Time Operator sizeof
- The Comma Operator
- The Dot (.) and Arrow (−>) Operators
- The [ ] and ( ) Operators
- Precedence Summary
- Expressions
- Order of Evaluation
- Type Conversion in Expressions
- Casts
- Spacing and Parentheses
- Compound Assignments
- 3 Statements
- True and False in C and C++
- Selection Statements
- if
- Nested ifs
- The if-else-if Ladder
- The? Alternative
- The Conditional Expression
- switch C o n t e n t s xi
- Nested switch Statements
- Iteration Statements
- The for Loop
- for Loop Variations
- The Infinite Loop
- for Loops with No Bodies
- The while Loop
- The do-while Loop
- Declaring Variables Within Selection and Iteration Statements
- Jump Statements
- The return Statement
- The goto Statement
- The break Statement
- The exit( ) Function
- The continue Statement
- Expression Statements
- Block Statements
- 4 Arrays and Null-Terminated Strings
- Single-Dimension Arrays
- Generating a Pointer to an Array
- Passing Single-Dimension Arrays to Functions
- Null-Terminated Strings
- Two-Dimensional Arrays
- Multidimensional Arrays
- Indexing Pointers
- Array Initialization
- Unsized Array Initializations
- A Tic-Tac-Toe Example
- 5 Pointers
- What Are Pointers?
- Pointer Variables
- The Pointer Operators
- Pointer Expressions
- Pointer Assignments
- Pointer Arithmetic
- Pointer Comparisons
- Pointers and Arrays
- Multiple Indirection
- Initializing Pointers
- Pointers to Functions
- C's Dynamic Allocation Functions xii C + + : T h e C o m p l e t e R e f e r e n c e
- Problems with Pointers
- 6 Functions
- The General Form of a Function
- Scope Rules of Functions
- Function Arguments - Call by Value, Call by Reference - Creating a Call by Reference - Calling Functions with Arrays
- argc and argv—Arguments to main( )
- The return Statement - Returning from a Function - Returning Values - Returning Pointers - Functions of Type void - What Does main( ) Return?
- Recursion
- Function Prototypes - Standard Library Function Prototypes
- Declaring Variable-Length Parameter Lists
- Old-Style Versus Modern FunctionParameter Declarations
- and User-Defined Types 7 Structures, Unions, Enumerations,
- Structures - Accessing Structure Members - Structure Assignments
- Arrays of Structures
- Passing Structures to Functions - Passing Structure Members to Functions - Passing Entire Structures to Functions
- Structure Pointers - Declaring a Structure Pointer - Using Structure Pointers
- Arrays and Structures Within Structures
- Bit-Fields
- Unions
- Enumerations
- Using sizeof to Ensure Portability
- typedef
- 8 C-Style Console I/O
- An Important Application Note
- Reading and Writing Characters C o n t e n t s xiii
- A Problem with getchar( )
- Alternatives to getchar( )
- Reading and Writing Strings
- Formatted Console I/O
- printf( )
- Printing Characters
- Printing Numbers
- Displaying an Address
- The %n Specifier
- Format Modifiers
- The Minimum Field Width Specifier
- The Precision Specifier
- Justifying Output
- Handling Other Data Types
- The * and # Modifiers
- scanf( )
- Format Specifiers
- Inputting Numbers
- Inputting Unsigned Integers
- Reading Individual Characters Using scanf( )
- Reading Strings
- Inputting an Address
- The %n Specifier
- Using a Scanset
- Discarding Unwanted White Space
- Non-White-Space Characters in the Control String
- You Must Pass scanf( ) Addresses
- Format Modifiers
- Suppressing Input
- 9 File I/O
- C Versus C++ File I/O
- Streams and Files
- Streams
- Text Streams
- Binary Streams
- Files
- File System Basics
- The File Pointer
- Opening a File
- Closing a File
- Writing a Character
- Reading a Character
- Using fopen( ), getc( ), putc( ), and fclose( ) xiv C + + : T h e C o m p l e t e R e f e r e n c e
- Using feof( )
- Working with Strings: fputs( ) and fgets( )
- rewind( )
- ferror( )
- Erasing Files
- Flushing a Stream
- fread( ) and fwrite( )
- Using fread( ) and fwrite( )
- fseek( ) and Random-Access I/O
- fprintf( ) and fscanf( )
- The Standard Streams
- The Console I/O Connection
- Using freopen( ) to Redirect the Standard Streams
- 10 The Preprocessor and Comments
- The Preprocessor
- #define
- Defining Function-like Macros
- #error
- #include
- Conditional Compilation Directives
- #if, #else, #elif, and #endif
- #ifdef and #ifndef
- #undef
- Using defined
- #line
- #pragma
- The # and ## Preprocessor Operators
- Predefined Macro Names
- Comments
- Single-Line Comments
- 11 An Overview of C++ C++
- The Origins of C++
- What Is Object-Oriented Programming?
- Encapsulation
- Polymorphism
- Inheritance
- Some C++ Fundamentals
- A Sample C++ Program
- A Closer Look at the I/O Operators C o n t e n t s xv
- Declaring Local Variables
- No Default to int
- The bool Data Type
- Old-Style vs. Modern C++ - The New C++ Headers - Namespaces - Working with an Old Compiler
- Introducing C++ Classes
- Function Overloading
- Operator Overloading
- Inheritance
- Constructors and Destructors
- The C++ Keywords
- The General Form of a C++ Program
- 12 Classes and Objects
- Classes
- Structures and Classes Are Related
- Unions and Classes Are Related - Anonymous Unions
- Friend Functions
- Friend Classes
- Inline Functions - Defining Inline Functions Within a Class
- Parameterized Constructors - Constructors with One Parameter: A Special Case
- Static Class Members - Static Data Members - Static Member Functions
- When Constructors and Destructors Are Executed
- The Scope Resolution Operator
- Nested Classes
- Local Classes
- Passing Objects to Functions
- Returning Objects
- Object Assignment
- Allocation Operators 13 Arrays, Pointers, References, and the Dynamic
- Arrays of Objects - Creating Initialized vs. Uninitialized Arrays
- Pointers to Objects
- Type Checking C++ Pointers
- The this Pointer xvi C + + : T h e C o m p l e t e R e f e r e n c e
- Pointers to Derived Types
- Pointers to Class Members
- References - Reference Parameters - Passing References to Objects - Returning References - Independent References - References to Derived Types - Restrictions to References
- A Matter of Style
- C++'s Dynamic Allocation Operators - Initializing Allocated Memory - Allocating Arrays - Allocating Objects - The nothrow Alternative - The Placement Form of new
- and Default Arguments 14 Function Overloading, Copy Constructors,
- Function Overloading
- Overloading Constructors - Overloading a Constructor to Gain Flexibility - Allowing Both Initialized and Uninitialized Objects
- Copy Constructors
- Finding the Address of an Overloaded Function
- The overload Anachronism
- Default Function Arguments - Default Arguments vs. Overloading - Using Default Arguments Correctly
- Function Overloading and Ambiguity
- 15 Operator Overloading
- Creating a Member Operator Function - of the Increment and Decrement Operators Creating Prefix and Postfix Forms - Overloading the Shorthand Operators - Operator Overloading Restrictions
- Operator Overloading Using a Friend Function - Using a Friend to Overload ++ or – – - Friend Operator Functions Add Flexibility
- Overloading new and delete - Overloading new and delete for Arrays - Overloading the nothrow Version of new and delete
- Overloading Some Special Operators
- Overloading [ ]
- Overloading ( )
- Overloading –>
- Overloading the Comma Operator
- 16 Inheritance
- Base-Class Access Control
- Inheritance and protected Members
- Protected Base-Class Inheritance
- Inheriting Multiple Base Classes
- Constructors, Destructors, and Inheritance
- When Constructors and Destructors Are Executed
- Passing Parameters to Base-Class Constructors
- Granting Access
- Virtual Base Classes
- 17 Virtual Functions and Polymorphism
- Virtual Functions - Class Reference Calling a Virtual Function Through a Base
- The Virtual Attribute Is Inherited
- Virtual Functions Are Hierarchical
- Pure Virtual Functions
- Using Virtual Functions
- Early vs. Late Binding
- 18 Templates
- Generic Functions
- A Function with Two Generic Types
- Explicitly Overloading a Generic Function
- Overloading a Function Template
- Using Standard Parameters with Template Functions
- Generic Function Restrictions
- Applying Generic Functions
- A Generic Sort
- Compacting an Array
- Generic Classes
- An Example with Two Generic Data Types
- Applying Template Classes: A Generic Array Class
- Using Non-Type Arguments with Generic Classes
- Using Default Arguments with Template Classes
- Explicit Class Specializations
- The typename and export Keywords
- The Power of Templates
- 19 Exception Handling
- Exception Handling Fundamentals
- Catching Class Types
- Using Multiple catch Statements
- Handling Derived-Class Exceptions
- Exception Handling Options
- Catching All Exceptions
- Restricting Exceptions
- Rethrowing an Exception
- Understanding terminate( ) and unexpected( )
- Setting the Terminate and Unexpected Handlers
- The uncaught_exception( ) Function
- The exception and bad_exception Classes
- Applying Exception Handling
- 20 The C++ I/O System Basics
- Old vs. Modern C++ I/O
- C++ Streams
- The C++ Stream Classes
- Formatted I/O
- Formatting Using the ios Members
- Setting the Format Flags
- Clearing Format Flags
- An Overloaded Form of setf( )
- Examining the Formatting Flags
- Setting All Flags
- Using width( ), precision( ), and fill( )
- Using Manipulators to Format I/O
- Overloading << and >>
- Creating Your Own Inserters
- Creating Your Own Extractors
- Creating Your Own Manipulator Functions
- 21 C++ File I/O
- and the File Classes
- Opening and Closing a File
- Reading and Writing Text Files
- Unformatted and Binary I/O
- Characters vs. Bytes
- put( ) and get( )
- read( ) and write( )