C++ FOR DUMmIES ‰ 5TH EDITION by Stephen Randy Davis C++ FOR DUMmIES ‰ 5TH EDITION by Stephen Randy Davis C++ For Dummies®, 5th Edition Published by Wiley Publishing, Inc 111 River Street Hoboken, NJ 07030-5774 Copyright © 2004 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, e-mail: permcoordinator@wiley.com Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates in the United States and other countries, and may not be used without written permission All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE NO WARRANTY MAY BE CRE ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS THE ADVICE AND STRATEGIES CON TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S at 800-762-2974, outside the U.S at 317-572-3993, or fax 317-572-4002 Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books Library of Congress Control Number: 2004102365 ISBN: 0-7645-6852-3 Manufactured in the United States of America 10 5B/SW/QU/QU/IN Index starting Dev-C++, 17 statement, executing single See single-stepping statements, basing programs on, 23–24 static objects complex data member, constructing, 228–232 constant data member, constructing, 232–233 data, referencing, 253–255 functions, declaring, 255–258, 277 naming, 171 order of construction, 234–235 protecting, 204 reasons for using, 251–252 this keyword, 258 using, 252–253 static variable, 91, 233 STL (Standard Template Library) See template library storing Dev-C++, 14 logical values with simple operators, 49–51 results of expressions, 26 stream I/O endl, terminating with, 322 fstream subclasses, 315–320 manipulators, 325–328 mechanics, 313–315 reading directly from, 320–322 subclasses (strstream), 322–325 string arrays, manipulating with characters, 103–106 characters, arrays of, 101–103 container, template library, 364–366 expanding pointer variables to, 129–131 length of, 153 manipulation, justifying, 131 variable, described, 33, 34 variables, 106–108 students, sample programs about courses, associating, 170–177, 182 GPA, 186–192, 193 graduate school, 263–268, 292–298 ID number, maps and, 371–374 needs with protected members, 202–208, 217–220 tuition calculation, 269–275 style, importance of clear and consistent, 379 subclass abstract classes, 287–289 constructing, 265–266 described, 159 destructing, 267 stream I/O (strstream), 322–325 using, 265 subscripts, 55 switch statement, 74–75, 135 syntax, defined, 10 system requirements backof-the-book CD, 393 •T• tab character (‘t’), 35, 385–386 tail pointer, 196 “tell me your address” pointer operator (&), 111–113 temperature conversion sample program, 17–21, 26 template library container classes objects linked to, 200 described, 363 iterators, 368–370 list containers, 366–368 maps, 371–374 sort() method, 371 string container, 364–366 templates BUDGET4, BC31–BC37 BUDGET5, BC37–BC44 classes, 355–361 function, generalizing into, 353–355 limitations, 361 maximum() functions, variants of, 351–353 sample, 22 temporary object avoiding, 249–250 described, 248–249 referential argument, referring to, 250 terminator character string to, returning (getline()), 320–321 endl, 322 newline character (‘n’), 322 null terminated array, 129 string, 103 test program, running debugger, 148–149 text editor, entering instructions with, 17–18 text, string displaying, 364–366 this keyword, 258 throw keyword, 335–337 true/false See boolean variable (bool) truncation problem integers (int variable), 29–30 solving, 30–31 try block, 332–335 typing errors, 20 417 418 C++ For Dummies, 5th Edition •U• unary mathematical operators, 43–44 unary operator (+), 40 Unix, origins of, 10 unwinding the stack, 332 user-defined objects operators, 306, 307 prompting for, 199 references, passing, 191–192 • V • value container for, 24 passing pointers to functions by, 117–118 variable See also array; pointer functions returning, 84 mixing in functions, 89 scope, limiting, 120–121 storage types, functions, 91 variable declaring See declaring variables variable size, 109–110 vector, 355 virtual inheritance, 346–348 virtual member functions described, 269–272 exception to identical declaration rule, 276 limitations, 277–278 polymorphism, 272–276 visibility, limiting access to class internals, 379–381 Visual C++.NET (Microsoft) coexisting on same machine as Dev-C++, 13 debugger commands, listed, 148 described, 11, BC45 Dev-C++ programs, compatibility with, 12 managed mode, 119, 123 new program project, creating, BC49–BC50 program, adding to project, BC47–BC49 project, building, BC45–BC47 void return type of function, 84 • W • warnings See error messages while condition is true (while loop), 64–65 whitespace, 24 whole numbers See integers (int variable) width parameter stream manipulator, 327 Windows 98 For Dummies (Rathbone), 393 Windows 2000 Professional For Dummies (Rathbone), 393 Windows (Microsoft) CD, back-of-the-book, 394–395 Dev-C++ development and, 12, 21 program arguments, accessing, 137–138 versions other than 9x and ME (Win32), BC46 WRITE debugging technique average, ridiculous value for, 143–146 divide by zero problem, 142–143 limitations, 146–147 writing declarations, 24–25 functions, 79–83 program, 11–12 • X • XOR bitwise operator (^), 56, 58 • Z • zero division error, 142–143 as non-pointer, 196 string terminated by, 103 .. .C+ + FOR DUMmIES ‰ 5TH EDITION by Stephen Randy Davis C+ + FOR DUMmIES ‰ 5TH EDITION by Stephen Randy Davis C+ + For Dummies? ?, 5th Edition Published by Wiley Publishing, Inc 111 River... reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections... on same machine as Dev -C+ +, 13 debugger commands, listed, 148 described, 11, BC45 Dev -C+ + programs, compatibility with, 12 managed mode, 119, 123 new program project, creating, BC49–BC50 program,