1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Starting out with C ++ early objects doc

1,2K 4,5K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 1.198
Dung lượng 6,91 MB

Nội dung

LOCATION OF VIDEONOTES IN THE TEXT Chapter Designing a Program with Pseudocode, p 19 Designing the Account Balance Program, p 24 Predicting the Output of Problem 30, p 24 Solving the Candy Bar Sales Problem, p 25 Chapter Using cout to Display Output, p 32 Assignment Statements, p 59 Arithmetic Operators, p 61 Solving the Restaurant Bill Problem, p 72 Chapter Using cin to Read Input, p 75 Evaluating Mathematical Expressions, p 81 Combined Assignment Operators, p 102 Solving the Stadium Seating Problem, p 151 Chapter Using an if Statement, p 162 Using an if/else Statement, p 172 Using an if/else if Statement, p 175 Solving the Time Calculator Problem, p 236 Chapter The while Loop, p 249 The for Loop, p 263 Nested Loops, p 277 Solving the Ocean Levels Problem, p 299 Chapter Defining and Calling Functions, p 306 Using Function Arguments, p 316 Value-Returning Functions, p 326 Solving the Markup Problem, p 380 Chapter Creating a Class, p 391 Creating and Using Class Objects, p 393 Creating and Using Structures, p 436 Solving the Car Class Problem, p 480 Chapter Accessing Array Elements, p 487 Passing an Array to a Function, p 517 Two-Dimensional Arrays, p 526 Solving the Chips and Salsa Problem, p 567 Chapter Performing a Binary Search, p 580 Sorting a Set of Data, p 587 Solving the Lottery Winners Problem, p 616 (continued on next page) LOCATION OF VIDEONOTES IN THE TEXT (continued) Chapter 10 Pointer Variables, p 621 Dynamically Allocating an Array, p 645 Solving the Days in Current Month Problem, p 676 Chapter 11 Operator Overloading, p 704 Aggregation and Composition, p 734 Overriding Base Class Functions, p 755 Solving the Number of Days Worked Problem, p 768 Chapter 12 Converting Strings to Numbers, p 788 Writing a C-String Handling Function, p 800 Solving the Backward String Problem, p 823 Chapter 13 Passing File Stream Objects to Functions, p 845 Rewinding a File, p 858 Solving the File Encryption Filter Problem, p 897 Chapter 14 Recursive Binary Search, p 913 QuickSort, p 915 Solving the Recursive Multiplication Problem, p 933 Chapter 15 Polymorphism, p 941 Composition Versus Inheritance, p 952 Solving the Sequence Sum Problem, p 970 Chapter 16 Throwing and Handling Exceptions, p 975 Writing a Function Template, p 986 Iterators, p 1004 Solving the Arithmetic Exceptions Problem, p 1020 Chapter 17 Adding an Element to a Linked List, p 1031 Removing an Element from a Linked List, p 1038 Solving the Member Insertion by Position Problem, p 1069 Chapter 18 Storing Objects in an STL Stack, p 1083 Storing Objects in an STL Queue, p 1097 Solving the File Reverser Problem, p 1109 Chapter 19 Inserting an Element into a Binary Tree, p 1118 Removing an Element from a Binary Tree, p 1122 Solving the Tree Size Problem, p 1138 Seventh Edition Starting Out with C++ Early Objects Tony Gaddis Judy Walters Godfrey Muganda Addison-Wesley Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo Editor-in-Chief: Michael Hirsch Editorial Assistant: Stephanie Sellinger Director of Marketing: Margaret Whaples Marketing Coordinator: Kathryn Ferranti Managing Editor: Jeffrey Holcomb Production Project Manager: Heather McNally Senior Manufacturing Buyer: Carol Melville Media Manufacturing Buyer: Ginny Michaud Art Director: Linda Knowles Cover and Interior Designer: Joyce Cosentino Wells Cover Art: © 2010 Dmitriy Ustyujanin/iStockphoto Media Project Manager: Katelyn Boller Full-Service Project Management: Peggy Kellar, Aptara®, Inc Composition: Aptara®, Inc Copyeditor: Evelyn Perricone Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on appropriate page within text Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A and other countries Screen shots and icons reprinted with permission from the Microsoft Corporation This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation The programs and applications presented in this book have been included for their instructional value They have been tested with care, but are not guaranteed for any particular purpose The publisher does not offer any warranties or representations, nor does it accept any liabilities with respect to the programs or applications Copyright © 2011, 2008, 2006, 2005 Pearson Education, Inc., publishing as Addison-Wesley, 501 Boylston Street, Suite 900, Boston, Massachusetts 02116 All rights reserved Manufactured in the United States of America This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 501 Boylston Street, Suite 900, Boston, Massachusetts 02116 Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in initial caps or all caps Library of Congress Cataloging-in-Publication Data Gaddis, Tony Starting out with C++ : early objects / Tony Gaddis, Judy Walters, Godfrey Muganda.—Seventh ed p cm Includes bibliographical references and index ISBN 978-0-13-607774-9 (alk paper) C++ (Computer program language) I Walters, Judy II Muganda, Godfrey III Title QA76.73.C153G33 2010 005.13'3—dc22 2010004498 10 1—EB—14 13 12 11 10 ISBN 10: 0-13-607774-9 ISBN 13: 978-0-13-607774-9 Contents at a Glance CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER 10 CHAPTER 11 CHAPTER 12 CHAPTER 13 CHAPTER 14 CHAPTER 15 CHAPTER 16 CHAPTER 17 CHAPTER 18 CHAPTER 19 Preface xv Introduction to Computers and Programming Introduction to C++ 27 Expressions and Interactivity 75 Making Decisions 157 Looping 243 Functions 305 Introduction to Classes and Objects 387 Arrays 485 Searching, Sorting, and Algorithm Analysis 577 Pointers 619 More About Classes and Object-Oriented Programming 677 More About Characters, Strings, and the string Class 771 Advanced File and I/O Operations 829 Recursion 901 Polymorphism and Virtual Functions 935 Exceptions, Templates, and the Standard Template Library (STL) Linked Lists 1023 Stacks and Queues 1071 Binary Trees 1111 Appendix A: The ASCII Character Set 1141 Appendix B: Operator Precedence and Associativity Index 1147 973 1145 v vi Contents at a Glance Student CD The following appendices are on the accompanying Student CD Appendix C: A Brief Introduction to Object-Oriented Programming Appendix D: Using UML in Class Design Appendix E: Namespaces Appendix F: Passing Command Line Arguments Appendix G: Header File and Library Function Reference Appendix H: Binary Numbers and Bitwise Operations Appendix I: C++ Casts and Run-Time Type Identification Appendix J: Multi-Source File Programs Appendix K: Multiple and Virtual Inheritance Appendix L: Introduction to the MinGW C++ Compiler and the wxDev-C++ IDE Appendix M: Introduction to Microsoft Visual C++ 2008 Express Edition Appendix N: NET and Managed C++ Appendix O: Introduction to Flowcharting Appendix P: Answers to Checkpoints Appendix Q: Answers to Odd-Numbered Review Questions Contents Preface xv CHAPTER 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Introduction to Computers and Programming Why Program? Computer Systems: Hardware and Software Programs and Programming Languages What Is a Program Made of? 12 Input, Processing, and Output 16 The Programming Process 17 Tying It All Together: Hi! It’s Me 22 CHAPTER 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 2.15 2.16 2.17 2.18 Introduction to C++ 27 The Parts of a C++ Program 27 The cout Object 31 The #include Directive 35 Standard and Prestandard C++ 37 Variables, Constants, and the Assignment Statement 37 Identifiers 41 Integer Data Types 43 The char Data Type 48 The C++ string Class 52 Floating-Point Data Types 54 The bool Data Type 57 Determining the Size of a Data Type 58 More on Variable Assignments and Initialization 59 Scope 60 Arithmetic Operators 61 Comments 64 Focus on Software Engineering: Programming Style 65 Tying It All Together: Smile! 67 vii viii Contents CHAPTER 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 Expressions and Interactivity 75 The cin Object 75 Mathematical Expressions 81 Implicit Type Conversion 89 Explicit Type Conversion 90 Overflow and Underflow 94 Named Constants 97 Multiple and Combined Assignment 101 Formatting Output 105 Working with Characters and String Objects 115 Using C-Strings 120 More Mathematical Library Functions 126 Introduction to Files 130 Focus on Debugging: Hand Tracing a Program 138 Green Fields Landscaping Case Study—Part 140 Tying It All Together: Word Game 142 CHAPTER 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.13 4.14 4.15 4.16 4.17 Making Decisions 157 Relational Operators 157 The if Statement 162 The if/else Statement 172 The if/else if Statement 175 Menu-Driven Programs 181 Nested if Statements 183 Logical Operators 187 Validating User Input 196 More About Variable Definitions and Scope 197 Comparing Characters and Strings 202 The Conditional Operator 206 The switch Statement 210 Enumerated Data Types 219 Testing for File Open Errors 222 Focus on Testing and Debugging: Validating Output Results 223 Green Fields Landscaping Case Study—Part 225 Tying It All Together: Fortune Teller 229 CHAPTER 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 Looping 243 The Increment and Decrement Operators 243 Introduction to Loops: The while Loop 249 Using the while Loop for Input Validation 253 Counters 256 The do-while Loop 258 The for Loop 263 Keeping a Running Total 269 Sentinels 271 Using a Loop to Read Data from a File 273 Focus on Software Engineering: Deciding Which Loop to Use 276 Nested Loops 277 Breaking Out of a Loop 279 The continue Statement 281 Contents 5.14 5.15 5.16 Focus on Testing and Debugging: Creating Good Test Data 284 Central Mountain Credit Union Case Study 287 Tying It All Together: What a Colorful World 291 CHAPTER 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 6.13 6.14 6.15 6.16 6.17 6.18 Functions 305 Modular Programming 305 Defining and Calling Functions 306 Function Prototypes 314 Sending Data into a Function 316 Passing Data by Value 321 The return Statement 325 Returning a Value from a Function 326 Returning a Boolean Value 332 Using Functions in a Menu-Driven Program 334 Local and Global Variables 337 Static Local Variables 344 Default Arguments 347 Using Reference Variables as Parameters 350 Overloading Functions 359 The exit() Function 363 Stubs and Drivers 366 Little Lotto Case Study 368 Tying It All Together: Glowing Jack-o-lantern 373 CHAPTER 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 7.11 Introduction to Classes and Objects 387 Abstract Data Types 387 Object-Oriented Programming 389 Introduction to Classes 391 Introduction to Objects 393 Defining Member Functions 395 Constructors 402 Destructors 407 Private Member Functions 410 Passing Objects to Functions 413 Object Composition 420 Focus on Software Engineering: Separating Class Specification, Implementation, and Client Code 424 Input Validation Objects 431 Structures 435 Home Software Company OOP Case Study 449 Introduction to Object-Oriented Analysis and Design 455 Screen Control 464 Tying It All Together: Yoyo Animation 469 7.12 7.13 7.14 7.15 7.16 7.17 CHAPTER 8.1 8.2 8.3 8.4 8.5 Arrays 485 Arrays Hold Multiple Values 485 Accessing Array Elements 487 Inputting and Displaying Array Contents Array Initialization 496 Processing Array Contents 502 489 ix INDEX path, 132 program location, 831–832 random-access, 871–878 reading, 134–137, 832–835 reading data into arrays, 491–492 reading, member functions, 849–862 reading with loops, 273–276 rewinding, 858–860 source, 10 text, 863 use steps, 130–131 using, 133–137, 830–831 writing, 133–134, 832–835 writing array contents to, 493 writing, member functions, 849–862 fill character, 843 find algorithm (STL), 1008, 1013 find function, 810 finding classes, 456, 458–461 first-in, first-out (FIFO) order, 1084–1085 fixed manipulator, 111, 841 fixed-point notation, 111 flags bit, 847 defined, 169 file open modes, 832 file positioning, 872 format, 843, 844 manipulator, 843 mode, 872 flash memory, float data type, 54, 55 floating-point arguments, 85 constants, 55–56 data types, 43, 54–57 division, 710 output, 111 representions, 54 values, assigning to integer variables, 56–57 floating-point numbers comparing, 169–171 defined, 54 double-precision, 16 field width, 108 identifying, 44 precision, 843 floppy disk drives, flowcharts, 19, Appendix O decision structure, 163 do-while loop, 259 for loop, 264 if/else if statement, 176 if/else statement, 172 nested if statements, 183 while loop, 249 flush manipulator, 841, 843 flushing buffers, 843 fmod function, 126 for loops, 263–269 See also loops body, 250 counter, 264–265 defined, 263 event sequence, 264 expressions, omitting, 268 flowchart, 264 format, 263 header, 263 initialization expression, 263, 267–268 defining variable in, 266 as pretest loop, 265 test expression, 263 update expression, 263, 266 use decision, 277 user-controlled, 267 for_each algorithm (STL), 1008, 1014–1015 formal arguments, 317 formal parameters, 317 format flags, 843, 844 formatting defined, 105, 863 member functions, 843–844 output, 105–114, 838–844 FORTRAN, forward iterator, 1004 friend classes, 693 friend functions, 689–693 declaration, 689 defined, 689 list, 689 front member function deque container, 1096 list container, 1055 queue container, 1097 vector class, 1006 fstream class data type, 878–882 objects, 831, 832 fstream header file, 131 function calls defined, 306 overloaded functions, 360 recursive, 902 statements, 307, 309 static member functions, 688 syntax, 321 value-returning function, 328–331 function prototypes defined, 314, 315 dummy names, 321 main and, 316 parameter data type, 320 placement, 315 value-returning functions, 328 function templates defined, 985 defining, 993 example, 986 as function specification, 988 with multiple types, 991 operators in, 990–991 overloading with, 992–993 placement, 988 prefix, 986 type parameters, 985–986, 987, 990, 992 uses, 985 functional notation, 93 functions abstract, 946 arguments, 316, 319, 320–321, 337, 347–350, 354–356, 517–526 array, 523–525 average-case complexity, 607 base class overriding, 755–757 body, 307 calling, 306–313 character conversion, 797 character testing, 793–797 comments, 309 constructor, 402–407 C-string, writing, 800–805 default arguments, 347–350 defined, 29, 305 defining, 306–307 drivers, 367–368 dummy, 366 execution, 307 1155 1156 INDEX functions (continued) friend, 689–693 global variables, 339–340, 343–344 header, 307, 308 implementation section, 395 inline, 395, 400–401 library, 85, 126–130, Appendix G local variables, 337–339, 343–347 member, 120, 389, 395–401 in menu-driven programs, 334–337 name, 306 operator, 705, 706 overloading, 359–363 parameter list, 306 parameters, 314, 316, 635–639 passing data by reference, 350–358 passing data by value, 321–324, 354–358, 444 passing file stream objects to, 845–846 passing files to, 356–357 passing objects to, 413–420 passing structures to, 444–446 passing two-dimensional arrays to, 530–532 polymorphic, 941 recursive, 901–907, 1046–1049 return type, 307 return values, 326–331 returning Boolean values, 332–334 returning objects from, 417–419 returning pointers from, 648–650 returning structures from, 446–447 sending data into, 316–321 signature, 360 static local variables, 344–347 static member, 685–688 stubs, 366–367 termination, 325 uses, 305–306 value-returning, 326–331 virtual, 941–946 void, 307 worst-case complexity, 605–607 G gcd recursive function, 910–911 General Crates, Inc case study, 142 generalization, 740 get (getter) functions, 395 get member function, 854–855 cin object, 118 peek member function versus, 855 versions, 855 getArea function, 391, 395, 397, 400 getBasePay function, 341, 343 getChar function, 434 getChoice function, 336 getFeet function, 712 getHighest function, 523 getInches function, 712 getInput function, 979 getLength function, 397 getline library function arguments, 125 cin object, 124–125 defined, 116 file stream objects and, 832 getline member function, 774, 851–854 arguments, 851, 852 delimiter use, 854 file stream objects and, 832 function call, 851 getLowest function, 523 getName function, 649, 755, 942 getNum function, 352 getNumber function, 637 getOvertimePay function, 341 getPrice method, 584 getQuality function, 682 getRadius function, 329, 331 getRandomNumbers function, 649 getSales function, 639 getValue function, 678 getWidth function, 397 global constants, 340–343 global variables, 339–340 See also variables defined, 337 initializing, 340 problems, 341 with same name, 343–344 scope, 339 good member function, 847 greater than operator (>), 158 greater than or equal to operator (>=), 158, 159, 815–817 Green Fields Landscaping case study part 1, 140–142 part 2, 225–229 grouping, with parentheses, 84 H h file, 424–429 hand tracing, 138–140 hard copy, hardware components, CPU, 3–4 defined, input devices, main memory, output devices, secondary storage, 4–5 has-a relationship class composition, 953 defined, 734 head, linked list, 1024, 1045 header files, Appendix G algorithm, 989, 1009 cctype, 431 cmath, 86, 126 cstdlib, 128, 364 ctime, 129 defined, 28 for file input/output, 131 fstream, 131 h, 424–429 iomanip, 107, 110 iostream, 78 older style, 37 string, 52, 806 table, Appendix G vector, 538 heap, 645 hex manipulator, 841 hexadecimal constants, 47 hierarchies, inheritance, 935–936 hierarchy charts, 18, 19, 369 High Adventure Travel Agency case study, 373 high-level languages, Hoare, C A R., 915 Home Software Company case study, 449–455 I identifiers defined, 41 legal, 42–43 rules, 42–43 INDEX IDEs (integrated development environments), 11, 132 Microsoft Visual C++ IDE tutorial, Appendix M wxDev-C++ IDE tutorial, Appendix L if statements, 162–172 braces, 164, 165, 167 conditions, 165 defined, 163 errors, 166–168 example, 164, 165 flags and, 169 floating-point number comparison, 169–171 nested, 183–187 programming style and, 166 relational expressions and, 168–169 semicolons and, 166–167 if/else if statement, 175–181 braces, 177 characteristics, 177–178 construction, 176 defined, 175 flowchart, 176 nested, 185 paths, 178 trailing else, 179–181 if/else statements, 172–175 braces, 173 defined, 172 flowchart, 172 paths of execution, 173 programming style, 173 #ifndef directive, 425 ifstream class See also file stream objects data type, 131, 274 file open mode, 834 get member function, 854 getline member function, 851 objects, 831 peek member function, 853–857, 928 read member function, 865 seekg member function, 859, 878 tellg member function, 876–878 ignore function, 119 image maps, 1060–1061 implementation file, class, 424, 430 implicit array sizing, 501–502 implicit pointers, 870 implicit type conversion, 89–90 #include directive, 28, 35–36 in multi-file programs, 424–429, Appendix J preprocessor and, 36 include file directory, 427 include guard, 425 increment, 243 increment operator (++) with arrays, 502 in cout statements, 246 defined, 244 in mathematical expressions, 247–248 postfix mode, 25–27, 244 prefix mode, 244, 245–247 in relational expressions, 248 indentation, 167 index variables, 581 indirect recursion, 907 indirection operator (*) defined, 14 in pointer declaration, 621–622, 637 infinite loops, 252 infix expressions, 927, 1103 inheritance, 740–745 base class, 740 class access, 745–748 class relationship, 458 class templates and, 999–1001 composition versus, 952–957 constructors, destructors, and, 750–754 defined, 952 defining classes by, 741 derived class, 740–741 hierarchies, 935–936 is-a relationship, 740–744 multiple, 744, Appendix K overriding base class functions, 756–757 private base class, 747–748 protected base class, 747–748 public base class, 747–748 type compatibility in, 936–939 virtual, Appendix K initialization array, 488, 496–498, 529–530 array of objects, 552–553 array of structures, 557–558 array, partial, 500–501 assignment versus, 695 constant pointer, 642 with constructors, 402–407 expression, 263–264 global variable, 340 with initialization list, 438–440, 497–498, 529–530 linked list node, 1026–1027 local variable, 339 member variable, 440 named constant, 97 object, 402–407 pointer, 630–631 static local variable, 346 structure, 438–440 two-dimensional array, 529 variable, 59–60 vector, 538–539 initialization expressions defined, 263 multiple statements in, 267–268 omitting, 268 variable definition in, 266 initialization lists array, 497, 502 array of objects, 552 defined, 438 drawbacks, 440 items, 439 member, 734–736 specifying, 502 structure, 438–440 two-dimensional array, 530 inline expansion, 401 inline member functions, 395, 400–401 inner loops, 277, 279 inorder traversal, 1119, 1120 input buffer, 77 cin object, 77 in class object, 430 console, 75 defined, 16 devices, file, 131, 831 iterator, 1004 opening files for, 878–882 reading line of, 124–125 test data, 20 1157 1158 INDEX input validation, 183 class, 432–435 defined, 196, 253 examples, 196 logic, 254 objects, 431–435 user, 196–197 while loops for, 253–256 inputFile object, 132 input-output stream library, 36 inputting array contents, 489–492 characters, 117 C-strings, 121 strings, 115–117 insert function, 810, 1055 instance members, 678, 685 instances computational problem, 602 defined, 393, 678 member variable, 420 instantiation, 393 instructions, 3, 4, int data type, 45, 729 IntArray class, 724 IntBinarySearchTree class, 1121 IntBinaryTree class, 1115–1117, 1120 IntClass class, 731–732 integers constants, 40, 47 data types, 43–48 division, 62, 710 variables, 56–57 integrated development environments (IDEs), 11, 132 wxDev-C++ IDE tutorial, Appendix L Microsoft Visual C++ IDE tutorial, Appendix M IntQueue class, 1089–1092 IntRange class, 981 IntStack class, 1073–1079 I/O manipulators See manipulators iomanip header file, 107, 110 ios::app, 834, 835 ios::ate, 835 ios::badbit, 847 ios::beg, 872 ios::binary, 835, 864, 867 ios::cur, 872 ios::end, 872 ios::eofbit, 847 ios::failbit, 847 ios::goodbit, 847 ios::hardfail, 847 ios::in, 832, 833, 835, 878 ios::nocreate, 878 ios::out, 833, 834, 835, 864, 878 ios::trunc, 835 ios class, 832 iostream header file, 28, 36 is-a relationship, 740–744 class inheritance, 952–953 defined, 740 examples, 740 isalnum library function, 793 isalpha library function, 192–193, 793, 796 isdigit library function, 793, 797 isEmpty function, 1074, 1078 isEven function, 332, 333 isFull function, 1079 islower library function, 793 isprint library function, 793 ispunct library function, 794 isspace library function, 794 istream class, 865 istringstream class, 788 isupper library function, 794 isValid function, 332 iteration defined, 251 loop, 251, 259, 279 overhead, 926 recursion versus, 926 iterators See also Standard Template Library (STL) defined, 1002 defining, 1004–1005 types, 1004 use of, 1004–1005 itoa library function, 790, 791 J Java, JavaScript, K key fields, 584 key words See also specific key words defined, 13 list of, 42 use of, 13–14 keyboard buffers, 77 L last-in-first-out (LIFO) structures, 1071 leaf nodes, 1111 leaks, memory defined, 646, 657 in loops, 646 stopping, 657 left child node, 1111 left manipulator, 112–113, 841 left to right associativity, 84 left-justified output, 113 length function, 120, 400, 512, 810 less than operator ( operator, 717–721 > operator, 815 [] operator, 721–727 behavior, 710 as member function, 711 types of, 711 overloading constructors, 405–406 defined, 757 with function templates, 992–993 overriding versus, 756–757 overridden functions base class versus derived class versions, 756 gaining access to, 757 overriding base class functions, 755–757 default arguments, 348 overloading versus, 756–757 ownership, class relationship, 458 P padding, 106 parallel arrays, 513–516 See also arrays defined, 513 subscript, 514 parameter list, 306, 317 parameters actual, 317 const, 703 constant reference, 416–417 INDEX copy constructor, 702 data type, 320 default arguments, 348 default constructor, 407 defined, 314, 316, 321 formal, 317 initializing local variables with, 339 multiple, 321 number of, 314 to operator functions, 706 order of, 321 pointers as, 635–639 pointers to class objects as, 655–657 reference, 706 reference variables as, 350–358 scope, 321 storage location, 323 type, 985–986 types of, 314 parent node, 1111 parentheses (), 30 arguments in, 318 conditional expressions and, 160, 209 data passed into functions, 309 grouping with, 84 if statements and, 164, 165 logical operators and, 194 loops and, 249, 259, 263 pointers and, 626 relational expressions and, 160 parsing, 863 partial array initialization, 500–501 partially-filled arrays, 508 partition function, 916–917 Pascal, passing file stream objects to functions, 845–846 files to functions, 356–357 objects to functions, 413–417 structures to functions, 444–446 two-dimensional arrays to functions, 530–532 passing by reference arguments, 351 defined, 635 files, 845–846 objects, 416–417 pointers versus, 635 structures, 444 when to use, 354–356 passing by value, 321–324 copy constructors and, 702 defined, 321 when to use, 354–356 peek member function, 855–857 pivot, 915 plain text, 957 pointer constants, 629 pointers addresses, 628, 632–633 aggregation through, 736–737 arithmetic, 629–630 array addresses as arguments, 637 array relationship, 625–629 asterisk (*), 621, 637 base class, 936, 937, 939–940 to char, 772, 775–776 to class objects, 655–657 comparing, 632–634 constant, 641–643 to constants, 639–640 dangling, 646 declaration, 621–622 decrementing, 629, 630 defined, 619, 621 dereferencing, 623 dereferencing to structures, 660 derived class, 936, 937 as function parameters, 635–639 identification, 621 implicit, 870 incrementing, 629, 630 initializing, 630–631 as members, 659–660 memory allocation, 700 notation, 626, 627, 637 null, 631 parentheses and, 626 passing by reference versus, 635 passing C-string arguments with, 803–805 returning from functions, 648–650 to structures, 652–655 in structures, 870 successor, 1024 this, 677–680 variables, 621–624 pointer-to-a-pointer, 662 polymorphism behavior, 943 defined, 941 dynamic binding, 946 static binding, 945–946 pop member function dynIntStack class, 1079 IntStack class, 1074, 1077, 1081 queue container, 1097 stack container, 1083 pop operation, 1072, 1073 pop_back function list container, 1055 vector class, 544, 545, 548, 1007 pop_front member function deque container, 1096 list container, 1055 positive offsets, 873 post test loops, 259 postfix (++) operator, 717 postfix expressions, 927, 1103 postfix mode defined, 244 prefix mode versus, 245–247 postorder traversal, 1119, 1120 pound sign (#), 10, 28, 30 pow function, 85, 128, 169, 316, 326 precedence arithmetic operators, 82–83 logical operators, 194 relational operators, 160 table, 1145 precision, 108, 843 precision member function, 843, 844 prefix (++) operator, 716–717 prefix expressions, 927 prefix mode defined, 244 postfix mode versus, 245–247 prefix notation, 93 preorder traversal, 1119, 1120 preprocessor, 10, 11 preprocessor directives, 10, 28, 36 prestandard C++, 37 prestandard compilers, 109 prestandard type cast expressions, 93 pretest loops, 251, 265 priming read, 254 print member function, 739 printable characters, 853 printing queues, 1085 1163 1164 INDEX private access specification, 392, 745, 748 private member functions, 410–413 defined, 410 example, 410–413 private member variables, 392 private members, 392 problem domain, 458–459 procedural programming, 389 processing, 16 program, 6–8 programmer-defined data types, 537 programmer-defined identifiers, 13, 14 programming as art and science, modular, 305 object-based, 463 object-oriented, 389–391 procedural, 389 process, 17–21 programming languages, 6–9 BASIC, C#, C++, 9, 29, 37 C, COBOL, elements, 12–14 FORTRAN, high-level, Java, JavaScript, low-level, Pascal, Python, Ruby, Visual Basic, programming style consistency, 66 defined, 65 if statement, 166 if/else statement, 173 multi-line statements, 67 visual arrangement, 66 while loop and, 253 programs defined, designing and creating, 17–21 elements, 2, 12–16 file location, 831–832 function definition, 18 hand tracing, 138–140 input, 16 language elements, 12–14 lines and statements, 14–15 linking, 20 menu-driven, 181–183, 334–337 model creation, 18–19 multi-file, 424–430, Appendix J older style, 37 output, 16–17 parts of, 27–31 processing, 16 results validation, 21, 223–225 running with test data, 20, 284–286 steps for writing, 17 three-step process, 16–17 top-down design, 18, 21 variable definitions, 15–16 variables, 15 visualization, 18 promoted values, 89–90 prompts, 76 protected access specification, 748 protected members, 745–748 prototypes See function prototypes pseudocode, 19 pseudorandom numbers, 128 public access specification, 392, 742, 745, 748 public member functions, 392 public member variables, 392 punctuation, 13, 14 pure virtual functions, 946–949 push member function IntStack class, 1074, 1075, 1077, 1081 queue container, 1097 stack container, 1083 push operation, 1072 push_back member function deque container, 1096 list container, 1055 vector class, 541–542, 548, 1007 push_front member function, 1055 put member function, 857–858 Pythagorean theorem, 127 Python, Q qSort function, 1099 quadratic time, 609 question mark (?), 488 queue container, 1097–1098 queues applications of, 1085 based on circular arrays, 1088 defined, 1084–1085 dequeue, 1085, 1086 dynamic, 1085, 1092–1095 empty, 1086, 1088 enqueue, 1085 expanding/shrinking, 1092 FIFO order, 1084–1085 full, 1088 introduction to, 1084–1092 linked list implementation, 1092 operations, 1085–1088 printing, 1085 static, 1085, 1092 testing, 1088 QuickSort algorithm defined, 915 functioning of, 915 implementation, 916 partitioning, 915 pivot, 915 recursive calls to, 1099 quotation marks (""), 30, 39, 50 R \r (return escape sequence), 35 RAM (random-access memory), 4, 15, 38 rand function, 128 random access container, 1003 files, 871–878 iterator, 1004 modes, 872 random numbers, 128–130 random-access memory (RAM), 4, 15, 38 random_shuffle algorithm (STL), 1009, 1011 ranking, data types, 89 read position, 135 priming, 254 read member function, 865 reading data with loops, 273–276 files, 134–137, 832–835 files, member functions, 849–862 line of input, 124–125 multiple values, 79–80 single characters, 117 real numbers, 16 records creating with structures, 866–871 INDEX fields, 866 information organization in, 867 recursion, 901–933 depth of, 902 direct, 907 eliminating, 1098–1102 indirect, 907 introduction to, 901–908 iteration versus, 926 overhead, 926, 1098 uses, 905 recursive algorithms binary search, 913–914 enumeration, 922–926 exhaustive, 922–926 factorial, 908–910 gcd, 910–911 linked list operations, 1045–1053 QuickSort, 915–918 Towers of Hanoi, 919–922 recursive functions See also recursive algorithms base case, 904, 913 defined, 901 number of recursive call and, 902 recursively defined problems, solving, 911–912 reference, passing arguments by, 351 reference parameters, 706 reference variables ampersand (&), 351 changes to, 521 defined, 350, 351 definition, 351 as parameters, 350–358 pointers and, 635 use instances, 355 reinterpret_cast, 864 relational expressions array elements in, 503 as Boolean expressions, 158 decrement operator ( ) in, 248 defined, 158 example, 159 if statements and, 168–169 increment operator (++) in, 248 in parentheses, 160 statements including, 161 relational operators, 157–162 as binary operators, 158 in comparing characters, 202–204 in comparing enumerated variables, 220 in comparing pointers, 632–634 in comparing strings, 204–205 defined, 157 function templates and, 990 list of, 158 overloading, 712–715 precedence, 160, 161 string class, 807 relationships access, 458 class, 457–458 composition, 457 has-a, 734, 739 inheritance, 458 is-a, 740–744 ownership, 458 value of, 158–159 Reliable Software Systems, Inc case study, 1056–1060 remainder, 62 remove member function, 1030, 1038–1040 recursive, 1051–1053 sorted/unsorted lists, 1039 replace function, 810 reserved words See key words resize member function string class, 810 vector class, 548, 1007 responsibilities, class, 461–463 rethrowing exceptions, 984–985 return statements, 325–326 defined, 325 format, 327 value-returning functions, 327 return types, 307 char *, 648 structure variable, 446 void, 307, 709 return values assignment operator (=), 709–710 Boolean, 332–334 data types, 329, 331 functions, 326–331 multiple, 326, 327, 447 object as, 417–419 structure as, 446–447 using, 329 reusability, object, 463 reverse member function, 548, 1007, 1055 rewinding files, 858–860 right child node, 1111 right manipulator, 112–113, 842 right to left associativity, 84 right-justified output, 106 root, binary tree, 1111 root nodes, binary search tree, 1114, 1118, 1119 round function, 126 round-off errors, 291 rows, two-dimensional array, 532–533 Ruby, running totals accumulator, 269–270 defined, 269 example use, 271 logic, 269 run-time errors, 20 run-time libraries, 10 run-time type identification, Appendix I rvalue, 59 S scientific manipulator, 842, 843 scientific notation, 54, 843 scope block, 198 coming into, 199 defined, 60, 198 of global variables, 339 leaving, 199 local, 198 of parameters, 321 resolution operator, 396, 757 variable, 60–61, 197–199 scope resolution operator (::), 396, 757 screens clearing, 337 control, 464–469 input form, controlling, 467–469 positioning cursor on, 464–467 search algorithms See also algorithms binary search, 580–584 defined, 577 introduction to, 577–584 linear search, 577–580 1165 1166 INDEX SearchableVector class, 1000, 1001 searching arrays of objects, 584–587 binary search trees, 1114, 1121–1122 vectors, 599–601 searchList function, 578 secondary storage, 4–5 Secure Encryption Systems, Inc case study, 957–960 seekg member function, 859, 872–876 seekp member function, 872–876 selection sort, 591–595 See also sorting algorithms worst case complexity, 607 selectSort function, 663, 664–665 self-referential data types, 1025 semicolons (;), 30, 36, 100 if statements and, 166–167 infinite loops and, 252 sentinels, 271–273 sequence structure, 162–163 sequential containers, 538, 1002–1003 STL, 1003 sequential file access, 871 sequential search See linear search set (setter) functions, 395 Set Intersection case study, 561 set STL container, 1003, 1004 SetConsoleCursorPosition function, 464 SetConsoleTextAttribute function, 464 setf member function, 843, 844 setfill manipulator, 842, 843 setLength function, 397, 427, 712 setName function, 745 setprecision manipulator defined, 108, 842 iomanip header file and, 110 output, 109 value display and, 109 setRadius function, 391, 395 setValue function, 679, 680 setw manipulator, 105, 106, 107, 838, 842 setWidth function, 397, 427 short circuit evaluation, 188, 190 short data type, 45 show member function, 663, 665 showArea function, 348 showArray function, 530, 532 showFees function, 336, 366, 367 showLocal function, 345 showpoint manipulator, 111–112, 842 showpos manipulator, 842 showRec function, 876 showSorted function, 663, 665–666 showState function, 847–848 showStatic function, 346 showSum function, 320–321 showValues function, 414, 417, 517–518, 520 significant digits, 108 sin function, 126 single precision data type, 54 single tasking operating systems, single user operating systems, single-line comments, 64 singly linked lists, 1054 size, data type, 44, 45, 58–59 size declarators See also arrays compiler requirement, 490 defined, 120, 486 example, 487 subscripts versus, 488 two-dimensional arrays, 527 vector, 538 size member function list container, 1055 stack container, 1083 string class, 810 vector class, 543, 548, 601, 1007 sizeof operator, 45, 58, 865, 867 soft copy, software abstraction and, 388 application, defined, engineering, 21 operating system, 5, sort algorithm (STL), 1009, 1011 sorting ranges, 1098 strings, 782–783 sublists, 915–916 sorting algorithms See also algorithms array of objects, 596–599 bubble sort, 587–591 defined, 587 introduction to, 587–595 QuickSort, 915–918 selection sort, 591–595 for vectors, 599–601 source code compiling, 20 defined, 10 writing, 20 source files defined, 10 translation process, 11 spaces blank, 106 file names and, 133 spacing, 167 special characters, 30 specialization, 740 specification file, class, 424, 425, 430 spreadsheets, 829 sqrt function, 126, 127, 388 square function, 329, 331, 359–360, 986 srand function, 128 sstream objects, 839–843 stack container (STL), 1083 stack, memory, 401 stacks applications of, 1072 becoming empty, 1078 Constructor function, 1074 dynamic, 1072, 1079–1082 exception handling, 1078–1079 filling up, 1078 isEmpty function, 1074, 1078 isEmpty operation, 1073 isFull function, 1079 as LIFO structures, 1071 linked list, 1079–1082 operations, 1072–1073 overflow, 1079 pop function, 1074, 1077, 1081 pop operation, 1072, 1073 push function, 1074, 1075, 1077, 1081 push operation, 1072 static, 1072 templates, 1079 underflow, 1079 unwinding, 983–984 stale data, avoiding, 400 stand-alone operator overloading, 715–716 INDEX standard C++, 37 Standard Template Library (STL) algorithms, 1008–1009 containers, 538, 1002–1004, 1054–1056, 1082–1084 defined, 537, 1002 introduction to, 1002–1015 iterators, 1002, 1004–1005 statements assignment, 38–39, 59–60, 81 break, 211–212 case, 210–211, 212 continue, 281–284 converting algebraic expressions to, 84–85 defined, 14–15 definition, 393 function call, 307, 309 if, 162–172 if/else, 172–175 if/else if, 175–181 multi-line, 67 multiple, 267–268 null, 166 return, 325–326 switch, 210–218 typedef, 516–517 variable definition, 16 while loop, 250 static binding, 945–946 static key word, 685 static local variables, 344–347 static members See also members declarations, 685 defined, 681 defining, 685 functions, 685–688 variables, 682–685 static queues See also queues defined, 1085 exceptions, 1092 static stacks See also stacks defined, 1072 isEmpty operation, 1073 static_cast type cast expression, 92, 93 std namespace, 29 STL See Standard Template Library storage See memory store function, 885 strcat library function, 776–777, 783 strcmp library function, 779–782 case sensitivity, 780–781 defined, 779, 783 example use, 780 NOT (!) operator with, 782 prototype, 779 return value, 780, 782, 792 summary, 783 strcpy library function, 122, 778, 783 stream extraction operator (>>) cin object, 76–79 defined, 76 in end of file test, 837 file stream objects and, 832 overloading, 717–721 in reading from files, 134–137 returning value, 275 stream insertion operator (

Ngày đăng: 12/02/2014, 21:20

TỪ KHÓA LIÊN QUAN