1,805 84 6MB
Pages 435 Page size 600 x 450 pts Year 2006
TeamLRN
SCHAUM’S OUTLINE OF
THEORY AND PROBLEMS of
PROGRAMMING WITH C++ Second Edition
· JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science University of Richmond
· SCHAUM’S OUTLINE SERIES McGRAW-HILL New York San Francisco Washington, D.C. Auckland Bogota′ Caracas Lisbon London Madrid Mexico City Milan Montreal New Delhi San Juan Singapore Sydney Tokyo Toronto
TeamLRN
abc
McGraw-Hill
Copyright © 2000, 1996 by the McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted 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-136811-6
The material in this eBook also appears in the print version of this title: ISBN 0-07-135346-1.
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 t he 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, transmit, 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 A Ò S ISÓ. McGRA W-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 WARRANTIES 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, consequential 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 possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. DOI: 10.1036/0071368116
Preface Like all Schaum’s Outline Series books, this volume is intended to be used primarily for self study, preferably in conjunction with a regular course in C++ programming language or computer science. However, it is also well-suited for use in independent study or as a reference. The book includes over 200 examples and solved problems. The author firmly believes that the principles of data structures can be learned from a well-constructed collection of examples with complete explanations. This book is designed to provide that support. C++ was created by Bjarne Stroustrup in the early 1980s. Based upon C and Simula, it is now one of the most popular languages for object-oriented programming. The language was standardized in 1998 by the American National Standards Institute (ANSI) and the International Standards Organization (ISO). This new ANSI/ISO Standard includes the powerful Standard Template Library (STL). This book conforms to these standards. Although most people who undertake to learn C++ have already had some previous programming experience, this book assumes none. It approaches C++ as one’s first programming language. Therefore, those who have had previous experience may need only skim the first few chapters. C++ is a difficult language for at least two reasons. It inherits from the C language an economy of expression that novices often find cryptic. And as an object-oriented language, its widespread use of classes and templates presents a formidable challenge to those who have not thought in those terms before. It is the intent of this book to provide the assistance necessary for first-time programmers to overcome these obstacles. Source code for all the examples and problems in this book, including the Supplementary Problems, may be downloaded from these websites http://projectEuclid.net/schaums , http://www.richmond.edu/~hubbard/schaums, http://hubbards.org/schaums, or http://jhubbard.net/schaums . Any corrections or addenda for the book will also be available at these sites. I wish to thank all my friends, colleagues, students, and the McGraw-Hill staff who have helped me with the critical review of this manuscript, including John Aliano, Arthur Biderman, Francis Minhthang Bui, Al Dawson, Peter Dailey, Mohammed El-Beltagy, Gary Galvez, Libbie Geiger, Sergei Gorlatch, Chris Hanes, John B. Hubbard, Raana Jeelani, Dick Palas, Blake Puhak, Arni Sigurjonsson, Andrew Somers, Joe Troncale, Maureen Walker, Stefan Wentzig, and Nat Withers. Their editorial advice and debugging skills are gratefully appreciated. Special thanks to my wife and colleague, Anita H. Hubbard, for her advice, encouragement, and creative ideas for this book. Many of the original problems used here are hers. JOHN R. HUBBARD Richmond, Virginia
iii
Copyright 2000 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.
TeamLRN
This page intentionally left blank.
Contents Chapter 1
Elementary C++ Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
Chapter 2
Fundamental Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14
Chapter 3
GETTING STARTED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 SOME SIMPLE PROGRAMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 THE OUTPUT OPERATOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 CHARACTERS AND LITERALS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 VARIABLES AND THEIR DECLARATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 PROGRAM TOKENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 INITIALIZING VARIABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 OBJECTS, VARIABLES, AND CONSTANTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 THE INPUT OPERATOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
NUMERIC DATA TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 THE BOOLEAN TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ENUMERATION TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 CHARACTER TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 INTEGER TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ARITHMETIC OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 THE INCREMENT AND DECREMENT OPERATORS . . . . . . . . . . . . . . . . . . . . . 21 COMPOSITE ASSIGNMENT OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 FLOATING-POINT TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 TYPE CONVERSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 NUMERIC OVERFLOW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 ROUND-OFF ERROR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 THE E-FORMAT FOR FLOATING-POINT VALUES . . . . . . . . . . . . . . . . . . . . . . 30 SCOPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12
THE if STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 THE if..else STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 KEYWORDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 COMPARISON OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 STATEMENT BLOCKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 COMPOUND CONDITIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 SHORT-CIRCUITING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 BOOLEAN EXPRESSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 NESTED SELECTION STATEMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 THE else if CONSTRUCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 THE switch STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 THE CONDITIONAL EXPRESSION OPERATOR . . . . . . . . . . . . . . . . . . . . . . . . . 49
v Copyright 2000 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.
TeamLRN
vi Chapter 4
Chapter 5
Chapter 6
CONTENTS Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.1
THE while STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.2
TERMINATING A LOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.3
THE do..while STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.4
THE for STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.5
THE break STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.6
THE continue STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.7
THE goto STATEMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.8
GENERATING PSEUDO-RANDOM NUMBERS . . . . . . . . . . . . . . . . . . . . . . . . . 75
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.1
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.2
STANDARD C++ LIBRARY FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.3
USER-DEFINED FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
5.4
TEST DRIVERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
5.5
FUNCTION DECLARATIONS AND DEFINITIONS . . . . . . . . . . . . . . . . . . . . . . . 92
5.6
LOCAL VARIABLES AND FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.7
void FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.8
BOOLEAN FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
5.9
I/O FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.10
PASSING BY REFERENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5.11
PASSING BY CONSTANT REFERENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.12
INLINE FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
5.13
SCOPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.14
OVERLOADING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.15
THE main() FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.16
DEFAULT ARGUMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 6.1
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
6.2
PROCESSING ARRAYS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
6.3
INITIALIZING AN ARRAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.4
ARRAY INDEX OUT OF BOUNDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6.5
PASSING AN ARRAY TO A FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
6.6
THE LINEAR SEARCH ALGORITHM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.7
THE BUBBLE SORT ALGORITHM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.8
THE BINARY SEARCH ALGORITHM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.9
USING ARRAYS WITH ENUMERATION TYPES . . . . . . . . . . . . . . . . . . . . . . . 137
6.10
TYPE DEFINITIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
6.11
MULTIDIMENSIONAL ARRAYS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
CONTENTS Chapter 7
Pointers and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 7.11 7.12 7.13 7.14
Chapter 8
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 REVIEW OF POINTERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 C-STRINGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 STRING I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 SOME cin MEMBER FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 STANDARD C CHARACTER FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 ARRAYS OF STRINGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 STANDARD C STRING FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Standard C++ Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 9.1 9.2 9.3 9.4 9.5 9.6
Chapter 10
THE REFERENCE OPERATOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 REFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 POINTERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 THE DEREFERENCE OPERATOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 DERIVED TYPES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 OBJECTS AND LVALUES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 RETURNING A REFERENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 ARRAYS AND POINTERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 DYNAMIC ARRAYS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 USING const WITH POINTERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 ARRAYS OF POINTERS AND POINTERS TO ARRAYS . . . . . . . . . . . . . . . . . . 170 POINTERS TO POINTERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 POINTERS TO FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 NUL, NULL, AND void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
C-Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8
Chapter 9
vii
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 FORMATTED INPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 UNFORMATTED INPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 THE STANDARD C++ string TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 FILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 STRING STREAMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 10.10 10.11 10.12 10.13
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 CLASS DECLARATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 CONSTRUCTORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 CONSTRUCTOR INITIALIZATION LISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 ACCESS FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 PRIVATE MEMBER FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 THE COPY CONSTRUCTOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 THE CLASS DESTRUCTOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 CONSTANT OBJECTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 STRUCTURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 POINTERS TO OBJECTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 STATIC DATA MEMBERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 static FUNCTION MEMBERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
TeamLRN
viii Chapter 11
CONTENTS Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10
Chapter 12
273 273 275 276 278 281 282 285 286 290
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . FUNCTION TEMPLATES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CLASS TEMPLATES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CONTAINER CLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . SUBCLASS TEMPLATES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . PASSING TEMPLATE CLASSES TO TEMPLATE PARAMETERS . . . . . . . . . A CLASS TEMPLATE FOR LINKED LISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . ITERATOR CLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
300 300 302 304 306 307 309 312
Standard C++ Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 14.1 14.2 14.3 14.4 14.5 14.6 14.7
Chapter 15
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . COMPOSITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . INHERITANCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . protected CLASS MEMBERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . OVERRIDING AND DOMINATING INHERITED MEMBERS . . . . . . . . . . . . . private ACCESS VERSUS protected ACCESS . . . . . . . . . . . . . . . . . . . virtual FUNCTIONS AND POLYMORPHISM . . . . . . . . . . . . . . . . . . . . . . . . VIRTUAL DESTRUCTORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ABSTRACT BASE CLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . OBJECT-ORIENTED PROGRAMMING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Templates and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 13.1 13.2 13.3 13.4 13.5 13.6 13.7 13.8
Chapter 14
256 256 256 258 260 260 261 263 264 266
Composition and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 12.1 12.2 12.3 12.4 12.5 12.6 12.7 12.8 12.9 12.10
Chapter 13
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . OVERLOADING THE ASSIGNMENT OPERATOR . . . . . . . . . . . . . . . . . . . . . . THE this POINTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . OVERLOADING ARITHMETIC OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . OVERLOADING THE ARITHMETIC ASSIGNMENT OPERATORS . . . . . . . . OVERLOADING THE RELATIONAL OPERATORS . . . . . . . . . . . . . . . . . . . . . OVERLOADING THE STREAM OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . CONVERSION OPERATORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . OVERLOADING THE INCREMENT AND DECREMENT OPERATORS . . . . . OVERLOADING THE SUBSCRIPT OPERATOR . . . . . . . . . . . . . . . . . . . . . . . .
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ITERATORS ON VECTORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ASSIGNING VECTORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE erase() and insert() FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . THE find() FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE C++ STANDARD vector CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . RANGE CHECKING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
324 326 327 328 329 331 332
Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 15.1 15.2 15.3 15.4 15.5
ANSI/ISO STANDARD C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE STANDARD TEMPLATE LIBRARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . STANDARD C++ CONTAINER CLASS TEMPLATES . . . . . . . . . . . . . . . . . . . . STANDARD C++ GENERIC ALGORITHMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . HEADER FILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
338 338 338 339 340
CONTENTS
ix
Appendix A Character Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 A.1 A.2
The ASCII Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Appendix B Standard C++ Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Appendix C Standard C++ Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Appendix D Standard C++ Container Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 D.1 D.2 D.3 D.4 D.5 D.6 D.7 D.8
THE vector CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE deque CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE stack CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE queue CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE priority_queue CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . THE list CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE map CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . THE set CLASS TEMPLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
353 358 359 359 360 361 363 365
Appendix E Standard C++ Generic Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Appendix F
The Standard C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Appendix G Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Appendix H References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
TeamLRN
This page intentionally left blank.
Dedicated to Anita H. Hubbard
TeamLRN
Chapter 1 Programming is best regarded as the process of creating works of literature, which are meant to be read. —Donald E. Knuth
Elementary C++ Programming A program is a sequence of instructions that can be executed by a computer. Every program is written in some programming language. C++ (pronounced “see-plus-plus”) is one of the most powerful programming languages available. It gives the programmer the power to write efficient, structured, object-oriented programs. 1.1 GETTING STARTED To write and run C++ programs, you need to have a text editor and a C++ compiler installed on your computer. A text editor is a software system that allows you to create and edit text files on your computer. Programmers use text editors to write programs in a programming language such as C++. A compiler is a software system that translates programs into the machine language (called binary code) that the computer’s operating system can then run. That translation process is called compiling the program. A C++ compiler compiles C++ programs into machine language. If your computer is running a version of the Microsoft Windows operating system (e.g., Windows 98 or Windows 2000), then it already has two text editors: WordPad and Notepad. These can be started from the Start key. In Windows 98, they are listed under Accessories. Windows does not come with a built-in C++ compiler. So unless someone has installed a C++ compiler on the machine you are using, you will have to do that yourself. If you are using a Windows computer that is maintained by someone else (e.g., an Information Services department at your school or company), you may find a C++ compiler already installed. Use the Start key to look under Programs for Borland C++Builder, Metrowerks CodeWarrior, Microsoft Visual C++, or any other program with “C++” in its name. If you have to buy your own C++ compiler, browse the Web for inexpensive versions of any of the compilers mentioned above. These are usually referred to as IDEs (Integrated Development Environments) because they include their own specialized text editors and debuggers. If your computer is running a proprietary version of the UNIX operating system on a workstation (e.g., Sun Solaris on a SPARCstation), it may already have a C++ compiler installed. An easy way to find out is to create the program shown in Example 1.1 on page 2, name it hello.C, and then try to compile it with the command CC hello
The Free Software Foundation has a suite of UNIX software, named “GNU” software that can be downloaded for free from http://www.gnu.org/software/software.html
1 Copyright 2000 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.
2
ELEMENTARY C++ PROGRAMMING
[CHAP. 1
Use their GCC package which includes a C++ compiler and their Emacs editor. For DOS systems, use their DJGPP which includes a C++ compiler. 1.2 SOME SIMPLE PROGRAMS Now you have a text editor for writing C++ programs and a C++ compiler for compiling them. If you are using an IDE such as Borland C++Builder on a PC, then you can compile and run your programs by clicking on the appropriate buttons. Other systems may require you to use the command line to run your programs. In that case, you do so by entering the file name as a command. For example, if your source code is in a file named hello.cpp, type hello
at the command line to run the program after it has been compiled. When writing C++ programs, remember that C++ is case-sensitive. That means that main() is different from Main(). The safest policy is to type everything in lower-case except when you have a compelling reason to capitalize something. EXAMPLE 1.1 The “Hello, World” Program This program simply prints “Hello, World!”: #include int main() { std::cout are required to indicate that the word “iostream” (which stands for “input/output stream”) is the name of a Standard C++ Library file. The expression is called a standard header. The second line is also required in every C++ program. It tells where the program begins. The identifier main is the name of a function, called the main function of the program. Every C++ program must have one and only one main() function. The required parentheses that follow the word “main” indicate that it is a function. The keyword int is the name of a data type in C++. It stands for “integer”. It is used here to indicate the return type for the main() function. When the program has finished running, it can return an integer value to the operating system to signal some resulting status. The last two lines constitute the actual body of the program. A program body is a sequence of program statements enclosed in braces { }. In this example there is only one statement: std::cout