C++: The Complete Reference Third Edition About the Author… Herb Schildt is the leading authority on C and C++ and a best-selling author whose books have sold more than 1.5 million copies His acclaimed C and C++books include Teach Yourself C, C++ from the Ground Up, Teach Yourself C++, C++: The Complete Reference, Borland C++: The Complete Reference, and C++ Programmer's Reference to name a few C++: The Complete Reference Third Edition Herbert Schildt Osborne McGraw-Hill Berkeley New York St Louis San Francisco Auckland Bogotá Hamburg London Madrid Mexico City Milan Montreal New Delhi Panama City Paris São Paulo Singapore Sydney Tokyo Toronto abc McGraw-Hill Copyright © 1998 by 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-213293-0 The material in this eBook also appears in the print version of this title: 0-07-882476-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 george_hoare@mcgraw-hill.com or (212) 9044069 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, 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 “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 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE McGraw-Hill and its licensors 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/0072132930 Index classes, Standard C++, 94, 626, 679-691, 878-892 in console I/O, 192-194 constant, 33, 94, 125-126 in file I/O, 222-223 limitations of null-terminated, 679-680 manipulation functions, 94-95 substring subtraction from, 930, 941-942 table, 125 string class, 94, 626, 679-691, 878, 880 and containers, 689-691 dynamic aspect of, 683 member functions, 683-688 operators defined for, 681 header, 680 string.h header file, 720 stringbuf class, 786 stringstream class, 786 stringstream( ), 802-803 strlen( ), 24, 79, 94-95, 727-728 strncat( ), 728 strncmp( ), 728 strncpy( ), 729 Stroustrup, Bjarne, 256, 266 strpbrk( ), 729 strrchr( ), 729 strspn( ), 730 strstr( ), 94-95, 730 strstream class, 616, 620, 622 header, 616 strtod( ), 767-768 strtok( ), 730 strtol( ), 768 strtoul( ), 768-769 struct keyword, 162, 163, 295 Structure(s), 162-174 arrays of, 166 arrays and structures within, 173-174 assignments, 165-166 and classes, 293-295 declaration, 162, 164 members, accessing, 165, 171, 173 passing to functions, 166-169 pointers, 169-173 variable, declaring, 163-164 strxfrm( ), 731 swap( ) algorithm, 853 swap_ranges( ) algorithm, 854 switch statement, 67-70 declaring variable within, 81 sync_with_stdio( ), 803 system( ), 769 Systems program, 8-9 T tan( ), 740 tanh( ), 740 tellg( ), 563, 804 tellp( ), 563, 804 Template function See Generic function(s) template keyword, 462 template< > syntax, 467, 485 Templates, 462-487 advantages to using, 487 definition of, 462 See also Generic class; Generic function(s) terminate( ), 491, 505-507 terminate_handler type, 506, 923 Ternary operator (?), 47, 63-66 this pointer, 315, 334-336, 388, 393 Thompson, Ken, throw statement, 491-492, 504-505 throw( ) clause, 502, 504 Time and date functions, 744-751 Time delay loops, 77 time.h header file, 744 time( ), 750-751 _ _TIME_ _ predefined macro, 250 time_t data type, 744 tm structure, 744 TMP_MAX, 717 tmpfile( ), 716-717 tmpnam( ), 717 tolower( ), 731 toupper( ), 731 towctrans( ), 774-775 transform( ) algorithm, 663, 669-670, 672-674, 854 true, 39, 58-59, 266 True and false in C and C++, 39, 58-59 trunc, 789 try statement, 490-495, 503-504 Two's complement, 16 Type checking and C++ pointers, 551 Type conversion and ambiguity in function overloading, 380-382 in assignment, 35-36 in expressions, 53-54 using unions for nonstandard, 178-180 Type promotion, 53 typedef statement, 162, 184-185 typeid, 34-596, 922-923 using dynamic_cast to replace, 584-586 type_info class, 570, 927 header, 570, 927 typename keyword, 462, 486-487 Types See Data types U ULONG_MAX, 769 unary_function class, 674, 869-870 unary_negate class, 871, 872 uncaught_exception( ), 507 #undef directive, 246-247 underflow_error exception, 924 unexpected( ), 502, 503, 505-506, 922 unexpected_handler type, 506, 923 ungetc( ), 717 union keyword, 177 Unions, 162, 176-180, 184 anonymous, 180, 297 and classes, 295-297 for nonstandard type conversions, 178-180 unique( ) algorithm, 854-855 1007 1008 C++: The Complete Reference unique_copy( ) algorithm, 854-855 unitbuf format flag, 516 Unix, UnPred type, 628, 808 unsetf( ), 517-518, 804 unsigned modifier, 14-16 upper_bound( ) algorithm, 855 uppercase format flag, 516 using statement, 261, 270, 436, 439, 598-600 header, 628, 927 V va_arg( ), 769-770 va_end( ), 769-770 va_list type, 718, 770 va_start( ), 769-770 valarray class, 898-916 member functions, table of, 899-903 nonmember operator functions defined for, table of, 904-909 transcendental functions defined for, table of, 910-911 header, 898 Variables, 17-23 access modifiers for, 23-25 automatic, 17 declaration vs definition of, 25-26 declaring, 17, 81-82 as formal parameters, 21 initializing, 31 member See Member variables placement in memory, 133 pointer, 47-49, 115 reference See Reference(s) storage class specifiers for, 25-31 structure, 163-164 Variables, global, 6, 21-23 declarations, difference between C and C++, 624 and encapsulation, 315 extern used with, 25-27 static, 28-29, 600-601 Variables, local, 6, 17-20, 21, 22 declarations, differences between C and C++, 19-20, 264-265, 623 initializing, 20 static used with, 20, 27-28, 29, 139 vector container, 626, 629, 630, 631-641, 809, 830-833 member functions, table of, 633, 831-833 header, 629, 809 vfprintf( ), 718 Virtual functions, 446-460 and class libraries, 460 hierarchical nature of, 452-455 and inheritance of virtual attribute, 450-452 and late binding, 460 overloading versus overriding and, 448-449 pure, 455-457 using, 457-460 virtual keyword, 442, 446, 447 Visual C++ compiler, 191 void data type, 14, 15, 153, 261, 623 volatile access modifier, 24-25, 611 vprintf( ), 718 vsprintf( ), 718 W wchar.h header file, 772 WCHAR_MAX, 772 WCHAR_MIN, 772 wchar_t data type, 14, 32, 515, 772 wcstombs( ), 770 wctomb( ), 770 wctrans( ), 774-775 wctrans_t type, 772 wctype.h header file, 772 wctype( ), 772-774 wctype_t type, 772 WEOF macro, 772 werr, 515 what( ), 790, 922 while loop, 6, 77-79 declaring variable within, 81 Wide character(s), 513, 515, 547 functions, 772-780 I/O classes, 514, 785-786 width( ), 522, 523-524, 804-805 win, 515 wint_t type, 772 wlog, 515 wout, 515 write( ), 550-553, 623, 805 wstreampos type, 789 wstring class, 679, 858, 860 X XOR bitwise operator (^), 42, 43, 44 logical operation, 41