Contents i What’s New with This Edition Teach Yourself ANSI C++ in 21 Days, Premier Edition, is

875 460 1
Contents i What’s New with This Edition Teach Yourself ANSI C++ in 21 Days, Premier Edition, is

Đ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

Contents i What’s New with This Edition Teach Yourself ANSI C++ in 21 Days, Premier Edition, is a new edition of the international bestseller, Teach Yourself C++ in 21 Days This Premier Edition offers more than just 21 days of learning ANSI/ISO C++ Major changes throughout this edition have been made to reflect the latest ANSI/ ISO C++ draft standard In addition, seven new lessons, referred to as “Bonus Days,” have been added to provide you with additional information for creating more sophisticated programs The Bonus Day lessons are not required reading in order for you to learn to program in C++ As a matter of fact, on the very first day you will write a simple C++ program On Bonus Day 22, “Advanced C++ Features,” you learn to use a collection of specialized ANSI C++ tools that many programmers don’t even realize exist These tools are not essential for every C++ programmer to know but are indispensable in many circumstances Bonus Day 23, “More About Streams,” covers some of the more advanced streams programming techniques After completing this lesson, you will have all you need to create new manipulators for the stream classes you learn to create on Day 16 You will also learn a great deal about other streams programming techniques that space would not allow us to cover on Day 16 On Bonus Day 24, “Object-Oriented Design,” you learn more about this important subject Without object-oriented design techniques, C++ is “just another C” with few advantages over its predecessor This lesson helps you think in a way that builds on the object-oriented design paradigm Bonus Days 25 and 26, “Data Structures” and “Simple Sorting and Searching Algorithms,” respectively, introduce you to the inner sanctum of computer science On these two days you learn how to solve several common categories of problems “Common Mistakes and Basic Debugging,” on Bonus Day 27, is designed to help you avoid some of the common mistakes that many beginning programmers make Not all errors can be avoided, but with this lesson you should more quickly recognize many errors by their categories and more quickly find their solutions The final day of the bonus week, “What’s Next?,” gives you some insight on where to turn when you run into a question that this book doesn’t seem to answer It also provides the resources you need to further improve your C++ programming skills P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 ii Teach Yourself ANSI C++ in 21 Days P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents iii teach yourself ANSI C++ in 21 days, Premier Edition P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 iv Teach Yourself ANSI C++ in 21 Days P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents v teach yourself ANSI C++ in 21 days, Premier Edition Jesse Liberty J Mark Hord S MS PUBLISHING 201 West 103rd Street Indianapolis, Indiana 46290 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Teach Yourself ANSI C++ in 21 Days vi Copyright © 1996 by Sams Publishing Acquisitions Editor Bradley L Jones PREMIER EDITION All rights reserved No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher No patent liability is assumed with respect to the use of the information contained herein Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions Neither is any liability assumed for damages resulting from the use of the information contained herein For information, address Sams Publishing, 201 W 103rd St., Indianapolis, IN 46290 International Standard Book Number: 0-672-30887-6 Library of Congress Catalog Card Number: 95-72920 99 98 97 96 Development Editor Anthony Amico Production Editor Ryan Rader Copy Editors Howard Jones Marla Reece Technical Reviewer Justin Bell John W Charlesworth Editorial Coordinator Bill Whitmer Interpretation of the printing code: the rightmost double-digit number is the year of the book’s printing; the rightmost single-digit, the number of the book’s printing For example, a printing code of 96-1 shows that the first printing of the book occurred in 1996 Technical Edit Coordinator Lynette Quinn Composed in Agaramond and MCPdigital by Macmillan Computer Publishing Editorial Assistants Sharon Cox Andi Richter Rhonda Tinch-Mize Printed in the United States of America All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized Sams Publishing cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark Formatter Frank Sinclair Cover Designer Tim Amrhein Book Designer Gary Adair Copy Writer Peter Fuller Production Team Supervisor Brad Chinn Publisher and President Acquisitions Manager Development Manager Managing Editor Marketing Manager Richard K Swadley Greg Wiegand Dean Miller Cindy Morrow Gregg Bushyeager P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Production Mary Ann Abramson, Mona Brown, Georgiana Briggs, Michael Brummitt, Jeanne Clark, Terri Edwards, George Hamlin, Sonya Hart, Mike Henry, Ayanna Lacey, Kevin Laseau, Paula Lowell, Donna Martin, Casey Price, Nancy Price, Brian-Kent Proffitt, Beth Rago, SA Springer, Tim Taylor, Andrew Stone, Mark Walchle Todd Wente, Colleen Williams Casey 2.23.96 FM LP#3 Contents vii Overview Week At A Glance Getting Started The Parts of a Program Variables and Constants Expressions and Statements Functions Basic Classes More Program Flow 19 31 55 87 123 155 Week In Review 187 Week At A Glance 193 10 11 12 13 14 Pointers References Advanced Functions Arrays Inheritance Multiple Inheritance Special Classes and Functions 195 223 257 293 329 361 395 Week In Review 421 Week At A Glance 431 15 16 17 18 19 20 21 Advanced Inheritance Streams The Preprocessor Creating Reusable Code Templates Exceptions and Error Handling Working with Multiple Files for Large Programs Week In Review P2/V3/sqc5 TY ANSI C++ in 21 433 469 509 537 551 581 609 625 30887-6 Casey 2.23.96 FM LP#3 Teach Yourself ANSI C++ in 21 Days viii Bonus Days At A Glance 635 22 23 24 25 26 27 28 Coding Styles and Idioms More About Streams Object-Oriented Design Data Structures Simple Sorting and Searching Algorithms Common Mistakes and Basic Debugging What’s Next? 637 659 673 683 701 721 733 Operator Precedence C++ Keywords Binary and Hexadecimal Answers Index 741 745 747 757 831 Appendixes A B C D P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents ix Contents Week At A Glance A Note to C Programmers Where You Are Going Day Getting Started A Brief History of C++ ANSI/ISO C++ What Is a Program? Sophisticated Tools for Sophisticated Programming Procedural, Structured, and Object-Oriented Programming C++ and Object-Oriented Programming How C++ Evolved Should I Learn C First? 10 Preparing to Program 10 Your Development Environment 11 Compiling the Source Code 12 Creating an Executable File with the Linker 12 The Development Cycle 13 HELLO.CPP: Your First C++ Program 14 Compile Errors 15 Summary 16 Q&A 16 Quiz 17 Exercises 17 The Parts of a C++ Program 19 The Parts of a Simple Program 19 A Brief Look at cout 21 Comments 22 Types of Comments 23 Using Comments 23 Comments at the Top of Each File 24 A Final Word of Caution About Comments 25 Functions 26 Using Functions 27 Summary 29 Q&A 29 Quiz 29 Exercises 30 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Teach Yourself ANSI C++ in 21 Days x Variables and Constants 31 What Is a Variable? 31 Setting Aside Memory 32 Size of Integers 33 Signed and Unsigned 34 Fundamental Variable Types 35 Defining a Variable 36 Case Sensitivity 37 Keywords 38 Creating More Than One Variable at a Time 39 Assigning Values to Your Variables 39 typedef 40 Choosing Between short and long 41 Wrapping Around in Unsigned Integers 42 Wrapping Around a Signed Integer 43 Characters 43 chars and Numbers 44 Special Printing Characters 44 Constants 46 Literal Constants 46 Symbolic Constants 46 Enumerated Constants 49 Summary 50 Q&A 51 Quiz 52 Exercises 53 Expressions and Statements 55 Statements 56 Whitespace 56 Blocks and Compound Statements 57 Expressions 57 Operators 59 Assignment Operator 59 Mathematical Operators 60 Integer Division and Modulus 60 Combining the Assignment and Mathematical Operators 61 Increment and Decrement 62 Prefix and Postfix 62 Precedence 64 Nesting Parentheses 64 The Bitwise Operators 65 Operator AND 66 Operator OR 67 Operator Exclusive OR 67 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 exceptions D data alignment, 652-654 arrays, 293-294, 683-684 bubble sort, 703-706 quick sort, 710-711 selection sort, 706-707 shell sort, 707-710 sorting, 702-712 classes, 12 loops, 156-157 manipulating with pointers, 201-202 member access, 211 on free store, 212-213 static, 395-400 queues, 684-689 circular, 688 example, 684-688 slicing, 351-353 stacks, 684-689 storage of variables, 31-36 streams, 469-471 buffering, 471 encapsulation, 470 structures, 320-325 trees (listing 25.2), 689-698 data abstract, 381-384 hiding, 8-9 slicing (listing 12.10), 351-353 debugging, 603-605 assembler, 605 breakpoints, 604 memory, 604 watch points, 604 declarations abstract data types, 385 arrays, 293-294, 299-300, 306-307 p2v3/Index#3 classes, 125, 379 friends classes, 459-460 functions, 463 in template classes, 558-564 functions, 88-93, 143-144 in classes, 141-143 interface demonstration (listing 6.5), 141-142 multiple, avoiding (listing 21.5), 620-621 multiple inheritance, 368 overloaded operators, 279 pointers, 204 references, 229 structure, 150-151 variables in functions, 93-95 operator placement, 248-249 decrement errors, 726 operators ( ), 62-63 defaults constructor, 262 parameters (listing 5.7), 103-105 values, compared to overloaded functions, 260-262 definitions of functions, 88-93 member functions, 134-136 object, 126 templates, 552-557 delegation, 444-467 dereference operator, see indirection derivation in inheritance, 330 syntax, 331-333 TY ANSI C++ in 21 Days 835 destructors default, 137-140 in inheritance, 335-341 listing 12.3, 335-337 listing 6.4, 138-139 virtual, 353-354 destructors (class member functions), 136-140 development cycle, 13 direct incursion, 111-114 division of integers, 60 while loop (listing 7.7), 164-167 downcasting (pointers), 365 dynamic casting, 646-649 E elements, arrays as, 294-295 else keyword (listing 4.5), 73-74 encapsulation overview, 8-9 of streams, 470 endl manipulator, 22 enumerated constants (listing 3.7), 49-50 equals operator, 284-286 errors, fence post (arrays), 298 exception objects, 584-589 exceptions catch blocks, implementing, 583-589 catching, 590 as classes, 593-594 compared to assert( ) macro, 524 data, 594-600 hierarchies, 593-594 implementing, 584-589 multiple (listing 20.2), 590-592 naming, 594-600 30887-6 Casey 2.23.96 Index LP#2 836 exceptions raising (listing 20.1), 586-587 with templates, 600-603 try block implementation, 589 without errors, 603 exclusive OR operator, 67 explicit constructors, 651-652 expressions, 57-58 Boolean, 69 evaluating (listing 4.1), 58 nested parentheses, 64-65 value precedence, 64 F fence post errors, 298 files appending to the end (listing 16.17), 498-499 binary compared to text, 500-502 comments, 24-25 implementation, 613-617 interdependency between variables and, 615-623 mulitple manipulation, 610-612 obj, 12 opening for input/output (listing 16.16), 496-498 sorting, 712 fill( ) member function, 491 flags, 489-493 format specification, 664 setf( ) member function, 491-493 floating point variables, 35 flush( ) member function, 487-489 for loops, 167-174 advanced, 169-172 p2v3/Index#3 demonstration (listing 7.9), 168 empty statements (listing 7.11), 171-172 nested, 173-174 null statements, 172-173 null statements in, 170-172 forever loops (listing 7.17), 180-181 format specification flags, 664 free store arrays declaration, 306-307 deleting, 309-310 storage, 305-306 data member access (listing 8.6), 211 member data, 212-213 new keyword, 206 object creation, 209 object deletions, 210-211 for pointers, 205 friend classes, 453-460, 558-564 functions, 26-28, 88 arguments (passing value with references), 99-100, 230-234 body, 27-30 call demonstration (listing 2.4), 26 callback, 408 compared to macros, 519 constant casting, 645-646 declarations, 88-93 default parameters, 103-105 defining, 91-93 definitions, 88-93 demonstration (listing 2.5), 28 dynamic casting, 646-649 executing, 93 TY ANSI C++ in 21 Days friend, 460 declaring, 463 general template classes, 561-563 nontemplate, 559-561 operator overloading, 460-463 type-specific templates, 563-564 headers, 27-30, 234-235 implementation, 143-144 increment (lisitng 10.7), 271 indentation styles, 543-545 inline, 109-110, 519-520 local variable parameters, 100-101 main( ), 20 maintenance strategy, 545-546 member getline( ), 483 overloaded, 257-260 pointer arrays to, 415-417 pointers to, 413-417 static, 400-402 str( ), 660-667 multiple same name creation, 106-108 name styles, 542-543 overloading (listing 5.8), 107-108, 262 overriding (listing 12.5), 341-346 parameters to functions, 27, 99-100 passing pointers, 410-411 pointers arrays (listing 14.7), 408-410 downcasting, 365 to (listings 14.5, 14.6), 402-413 30887-6 Casey 2.23.96 Index LP#2 insertion operators prototypes, 89-92, 234-235, 613 recursion (stop conditions), 111-114 reinterpret cast, 650-651 return values, 101-103 specialized in template classes, 567-573 statements, 99 static casting, 649-650 storage in precompiled header files, 610-612 strncpy( ) (listing 11.11), 312-313 swap( ), 230 implementing with references, 233 with pointers, 231-233 type definitions with pointers to, 412-413 values, multiple return (listing 9.8), 235-238 variable declarations, 93-95 virtual listing 12.8, 347-348 polymorphism, 643-644 G H header files function prototypes, 613 function storage, 610-612 precompiled, 612 variable names, 613 headers (in functions), 27-30, 234-235 Hello World program (listing 1.1), 14 hexadecimal arithmetic, 748-755 hierarchies inheritance, 330 streams, 659-660 I general template classes, 561-563 get( ) member function with character arrays), 482-485 extraction operator supplementation, 488-489 parameter overview, 480-485 p2v3/Index#3 getline( ) member function, 483 parameter overview, 485 global variables, 95-97 demonstration (listing 5.3), 96 goto keyword disadvantages of, 157 listing 7.1, 156 goto statement, 157 guards (inclusion), 513-516 IDE (integrated development environment), 510 if statements, 70-78 advanced (listing 4.6), 75-76 brace usage (listing 4.8), 76-78 ignore( ) member function, 485-486 inclusion guards, 513-516 increment errors, 726 TY ANSI C++ in 21 Days 837 functions (listing 10.7), 271 operators postfix, 62-63 prefix, 62-63 incursion Fibonacci solution (listing 7.15), 174-176 in loops, 174 indentation styles, 543-545 indirect incursion, 111-114 indirection (pointer access), 199-200 inheritance, 329-333 constructors, 335-341 derivation syntax, 330 destructors, 335-341 multiple declaring, 368 disadvantages, 379-380 listing 13.1, 364 private, 449-453 shared base class, 372-376 virtual class declarations, 376-379 inheritance (objects), initializing arrays, 298-299 globals (listing 21.3), 616 objects, 264-265 inline class methods, 145-147 functions (listing 5.9), 109-110, 519-520 implementation (listing 6.6, 6.7), 145-146 input condition states, 496 file preparation for writing, 496-498 input/output objects, 474 insertion operators overloading, 463-464 supplementing with get( ), 488-489 30887-6 Casey 2.23.96 Index LP#2 838 integer division integer division, 60-61 integer variables size evaluation, 33-34 wraparound, 42 integers, signed (value wrap around), 43 interface, establishing for OOD, 677-678 interim values, printing, 529-530 Internet online resources, 738-739 invariants (in classes), 525-529 irstream class, 660-667 ISO (International Standards Organization), iteration, see loops K keywords, 38, 746 const (as a pointer), 218 else (listing 4.5), 73-74 goto disadvantages of, 157 listing 7.1, 156 inline, 145-147 new, 206 protected, 333-335 L libraries algorithms, 735-736 classes creating, 638-645 namespaces, 638-640 containers, 735-736 iterators, 735-736 linked files, 12 library source file (listing 22.3), 642 p2v3/Index#3 localization, 735 numerics, 737 standard, 734-737 standard templates, 575-576 string, 735 linked lists, 320-325 delegating to (listing 15.5), 445-448 implementing (listing 11.13), 321-325 listings 1.1 Hello World application, 14 1.2 compiler error example, 15 2.1 C++ program parts overview, 19-21 2.2 using cout object, 21 2.3 comment demonstration, 23-24 2.4 function call demonstration, 26 2.5 simple function demonstration, 28 3.1 integer variable type size evaluation, 33-34 3.2 variable use demonstration, 40 3.3 typedef demonstration, 40-41 3.4 value wrap around in variables, 42 3.5 value wrap around in signed integer variable, 43 3.6 printing characters based on numbers, 44 3.7 enumerated constant demonstration, 49-50 4.1 complex expression evaluation, 58 4.2 subtraction and integer overflow, 60 TY ANSI C++ in 21 Days 4.3 prefix/postfix operators, 62-63 4.4 branching with relational operators, 71-72 4.5 else keyword demonstration, 73 4.6 complex nested if statement, 75-76 4.7 brace clarification, 76-77 4.8 brace usage in if statement, 78 4.9 conditional operator demonstration, 82-83 5.1 declaring, defining, and using functions, 90-91 5.2 local variables and parameters, 94 5.3 global variable demonstration, 96 5.4 scoped variables within block, 97-99 5.5 passing by value, 100-101 5.6 multiple return values, 101-103 5.7 default parameter values, 104-105 5.8 function overloading, 107-108 5.9 inline functions, 109-110 5.10 recursion using Fibonacci series, 112-113 6.1 public member access, 129-130 6.2 class with accessor methods, 131 6.3 class method implementation, 134-135 6.4 using constructors/ destructors, 138-139 30887-6 Casey 2.23.96 Index LP#2 listings 6.5 declaration demonstration, 141-142 6.6 Cat class declaration, 145-146 6.7 Cat class implementation, 146 6.8 complete class declaration, 147-148 6.9 complete class declaration, 148-149 7.1 looping with goto keyword, 156 7.2 while loops, 157-158 7.3 complex while loops, 159 7.4 break and continue statements, 160-161 7.5 while (true) loops, 163 7.6 skipping the body of while loops, 164-165 7.7 while loop demonstration, 165-166 7.8 while loop reexamined, 167-168 7.9 for loop demonstration, 168 7.10 advanced for loops, 169-170 7.11 null statements in for loops, 170-171 7.12 empty for loop statement, 171 7.13 null statements in for loops, 172 7.14 nested for loops, 173 7.15 solving the Fibonacci number using iteration, 174-175 7.16 switch statement demonstration, 178 7.17 forever loop, 180-181 8.1 variable addresses, 196 8.2 data manipulation with pointers, 201 p2v3/Index#3 8.3 pointer examination, 202 8.4 pointer process, 206-208 8.5 creating and deleting objects from free store, 210 8.6 data member access, 211-212 8.7 pointers as member data, 212-213 8.8 this pointer, 213-214 8.9 creating a stray pointer, 215-216 8.10 pointers to const objects, 218-219 9.1 demonstration of references, 224 9.2 reference addresses, 225-226 9.3 reassigning references, 227 9.4 references to objects, 228-229 9.5 passing by value demonstration, 231 9.6 bypassing references with pointers, 231-232 9.7 swap( ) rewritten with references, 233 9.8 returning values with pointers, 235-236 9.9 rewrite of listing 9.8, with references, 237 9.10 passing objects by reference, 239 9.11 passing const pointers, 241-242 9.12 passing references to objects, 243-244 9.13 swap rewritten with pointer references, 246-247 TY ANSI C++ in 21 Days 839 9.14 reference return to nonexistent object, 249-250 9.15 memory leaks, 251 10.1 overloading member functions, 258-259 10.2 default values, 260-261 10.3 overloading the constructor, 263-264 10.4 member function initialization, 265 10.5 copy constructor, 266-268 10.6 Counter class, 270 10.7 increment operators, 271 10.8 overloading the prefix operator, 272 10.9 returning a temporary object, 273-274 10.11 returning the this pointer, 276-277 10.12 prefix and postfix operators, 277-278 10.13 Add( ) function, 280 10.14 addition operator, 281-282 10.15 assignment operator, 285 10.16 attempting to assign a counter to USHORT, 286-287 10.17 converting USHORT to Counter, 287-288 10.18 converting from Counter to unsigned short( ), 288-292 11.1 using an integer array, 294 11.2 writing past an array, 295-296 30887-6 Casey 2.23.96 Index LP#2 840 listings 11.3 using consts and enums in arrays, 300 11.4 creating an object array, 301 11.5 creating multidimensional arrays, 303-304 11.6 storing arrays on the free store, 305-306 11.7 array creation with new, 307-308 11.8 filling an array, 310-311 11.9 filling an array, 311 11.10 using strcpy( ) function, 312 11.11 strncpy( ) function, 313 11.12 string classes, 314-317 11.13 linked list implementation, 321-323 12.1 inheritance, 331-333 12.2 derived objects, 334-335 12.3 constructors and destructors called, 335-337 12.4 overloading constructors in derived classes, 337-340 12.5 overriding a base class method, 342-343 12.6 hiding methods, 343-344 12.7 calling a base method, 345-346 12.8 virtual methods, 347-348 12.9 multiple virtual functions called in turn, 349-350 12.10 data slicing when passing by value, 351-353 p2v3/Index#3 12.11 virtual copy constructor, 354-356 13.1 multiple inheritance example, 364 13.2 multiple inheritance, 366-367 13.3 multiple constructors, 369-370 13.4 common base classes, 372-374 13.5 virtual inheritance, 377-378 13.6 shape classes, 381-383 13.7 abstract data types, 384-385 13.8 implementing pure virtual functions, 385-388 13.9 deriving ADTs from other ADTs, 389-392 14.1 static member data, 396 14.2 accessing static members without objects, 398 14.3 accessing static members using nonstatic member functions, 399-400 14.4 static member functions, 400-401 14.5 pointers to functions, 403-405 14.6 rewrite of listing 14.5 without pointer to function, 406-407 14.7 array of pointers to functions, 408-409 14.8 passing pointers to functions, 410-411 14.9 type definitions in pointers to functions, 412-415 TY ANSI C++ in 21 Days 14.10 pointers to member functions, 413-415 14.11 array of pointers to member functions, 415-416 15.1 String class, 433-436 15.2 employee class and driver program, 437-438 15.3 contained class constructors, 440-441 15.4 passing by value (contained classes), 442-443 15.5 delegating to a contained linked list, 445-448 15.6 private inheritance, 449-451 15.7 friend class illustration, 454-458 15.8 friendly operator+, 461-462 16.1 cin object data handling, 475-476 16.2 string handling by cin object, 477 16.3 character strings and cin object, 477-478 16.4 using get( ) without parameters, 480-482 16.5 using get( ) with parameters, 481 16.6 using get( ) with a character array, 482-483 16.7 using getline( ), 483-484 16.8 using ignore( ), 485 16.9 using peek( ) and putback( ), 486-487 16.10 using put( ), 488 16.11 using write( ), 488-489 16.12 output stream width adjustment, 490 16.13 using fill( ), 491 30887-6 Casey 2.23.96 Index LP#2 literal constants 16.14 using setf( ), 492 16.15 printing with printf( ), 494-495 16.16 opening files for input/output, 496-497 16.17 appending to the end of a file, 498-499 16.18 writing a class to a file, 501-502 16.19 command-line arguments, 503, 504 17.1 using #define command, 511-512 17.2 #undef command, 514-515 17.3 using parentheses, 517-518 17.4 using inline instead of macros, 519-520 17.5 simple assert macro, 522-523 17.6 using invariants( ), 525-529 17.7 printing values in DEBUG mode, 529-530 17.8 levels of debugging, 531-533 18.1 literal constants, 538 18.2 tic-tac-toe move function, 539-540 18.3 maintenance string encoding method, 545-546 19.1 array class template, 553 19.2 implementation of the template class, 555-557 19.3 nontemplate friend function, 559-560 19.4 implementing an iostream operator, 561-563 p2v3/Index#3 19.5 passing template objects to and from functions, 564-566 19.6 specialized template functions, 567-571 19.7 using static member data and functions with templates, 573-575 20.1 raising an exception, 586-587 20.2 multiple exceptions, 590-592 20.3 class hierarchies and exceptions, 593-594 20.4 getting data out of an exception object, 594-597 20.5 passing by reference in exceptions, 598-600 20.6 using exceptions with templates, 600-602 21.1 global variable scope in multiple files, 613-614 21.2 interdependency bugs, 615 21.3 safe initialization of globals, 616-617 21.4 scope resolution operators, 618-619 21.5 multiple declaration error, 620 22.1 namespace construct, 638-639 22.2 Dog class header, 640-641 22.3 library source file, 642 22.4 class libraries, 643 22.5 dynamic casting, 646-648 22.6 static casting example, 649-650 23.1 irstream class and str( ), 660 TY ANSI C++ in 21 Days 841 23.2 freezing the ostrstream class, 662-663 23.3 simple manipulators, 664-667 23.4 advanced manipulator example, 665-667 23.5 multiple strings program, 667-670 25.1 simple queue example, 684-687 25.2 binary tree example, 691-696 26.1 bubble sort example, 703-704 26.2 selection sort example, 706-707 26.3 shell sort example, 708-709 26.4 quick sort example, 710-711 26.5 straightforward string search, 713-714 26.6 binary search example, 715-716 27.1 music class example, 721-723 27.2 off-by-one error, 725-726 27.3 boundary violation example, 725-726 27.4 pointer bugs, 726-727 27.5 bracket mismatch error, 727 28.1 algorithms library sort, 735-736 lists delegating to (listing 15.5), 445-448 linked, 320-325 literal constants, 46 defining with #define, 47 defining with const, 47-48 in code reuse, 538-539 30887-6 Casey 2.23.96 Index LP#2 842 local scope (variables) local scope (variables), 97-99 localization libraries, 735 logical operators, 79-80 AND statement, 79 NOT statement, 80 OR statement, 80 long type variable definition, 41-43 loops complex while (listing 7.3), 159 while, 164-167 eternal, see loops for, 167-174 advanced, 169-172 demonstration (listing 7.9), 168 empty statements (listing 7.12), 171-172 null statements, 172-173 null statements in, 170-172 forever (listing 7.17), 180-181 goto keyword, disadvantages of, 157 incursion, 174 nested, 173-174 scoping in, 174 while, 157-164 break statement, 160-163 continue statement, 160-163 listing 7.8, 167-168 skipping the body of (listing 7.6), 164-165 LSB (least significant bit), 32 p2v3/Index#3 M macros assert( ), 522-534 compared to exceptions, 524 debugging, 524 compared to functions and templates, 519 compared to templates, 519 functions, creating with #define command, 516-519 parentheses usage, 517 predefined, 522 main( ) function, 20 manipulators, 489-493 advanced example (listing 23.4), 665-667 creating, 664-667 endl, 22 simple example (listing 23.3), 664-667 masking operators bit clearing, 68 bit settings, 67 flipping bits, 68 mathematical operators, 60-61 member data classes as, 147-150 on the free store, 212-213 pointers (listing 8.7), 212-213 static, 395-400 accessing using nonstatic member functions, 399-400 accessing without an object (listing 14.2), 398 TY ANSI C++ in 21 Days member functions cin object (single character input), 480 in classes constructors, 136-140 destructors, 136-140 constant (listing 8.10), 218 definitions, 134-136 fill( ), 491 flush( ), 487 get( ) without parameters, 480-482 getline( ), 483-485 ignore( ), 485-486 overloaded, 257-260 peek( ), 486-487 pointers to, 413-417 printf( ), 494 put( ), 488-489 putback( ), 486-487 setf( ), 491-493 static, 400-402 str( ), 660-667 write( ), 488-489 member variables accessing in classes, 126-128 in classes, 125 initializing (listing 10.4), 264-265 naming, 128 private, 128-133 public, 128-133 members accessing in contained classes, 439 filtered access to contained class, 439-440 memory accidental leaks, 208-209 allocation for arrays, 304-305 30887-6 Casey 2.23.96 Index LP#2 OOD (object-oriented design) pointer return, 253 leaks (listing 9.15), 251 menus (with switch statements), 180-183 methods base class calling (listing 12.7), 345-346 hiding (listing 12.6), 343-345 class implementation (listing 6.3), 134-140 inline, 145-147 OOD identification, 676 virtual, 346-357 Mixins (capability class), 380-381 MSB (most significant bit), 32 multidimensional arrays, 302-303 creating (listing 11.5), 303-304 initializing, 303-304 multiple constructors, 369-371 multiple declarations, avoiding (listing 21.5), 620-621 multiple inheritance compared to single inheritance, 361-381 declaring, 368 disadvantages of, 379 listing 13.1, 364 listing 13.2, 366-367 multiple streams, 667-671 N namespaces, 638-640 nested loops, 173-174 nested parentheses (in expressions), 64-65 p2v3/Index#3 nontemplate classes, 559-561 NOT operator, logical, 80 null pointers, 230 numerics libraries, 737 O obj files, 12 objects arrays, creating (listing 11.4), 301-302 casting, 644-651 cin character string handling, 477-478 input with, 475-479 istream object references, 479 constant (listing 8.10), 218-220 cout, 21 creating on free store, 209 defining, 126 deleting from free store, 210-211 derived (listing 12.2), 334-335 design maintenance (OOD), 678 exceptions, 584-589 catching, 590 data, 594-600 hierarchies, 593-594 implementing, 584-589 multiple (listing 20.2), 590-592 naming, 594-600 raising an exception (listing 20.1), 586-587 without errors, 603 implementing in OOD, 678 TY ANSI C++ in 21 Days 843 inheritance, initializing, 264-265 input/output redirection, 474 multiple inheritance, constructors in, 369-371 nameless returns, 274-276 nonexistent reference return, 249-250 ofstream, 495-500 OOD (object-oriented design) identification, 675-676 interface establishment, 677-678 visibility establishment, 677 passing by reference, 238 reference assignment, 228-229 reuse, temporary returns, 273-274 versus classes, 126 off-by-one errors, 725-726 ofstream default behavior modifications, 498-500 ofstream objects, 495-500 OMT (object modeling technique), 679 OOD (object-oriented design), 673 design maintenance, 678 interface establishment, 677-678 method identification, 676 object identification, 675-676 object implementation, 678 process overview, 675-678 troubleshooting, 679-680 visibility, 677 30887-6 Casey 2.23.96 Index LP#2 844 OOP (object-oriented programming) OOP (object-oriented programming), 6-8 data hiding overview, 8-9 encapsulation, 8-9 inheritance, reuse, operator+, friendly (listing 15.8), 461-462 operators, 59-61 addition overloading, 279-282 bitwise, 65-69 AND, 66 exclusive OR, 67 OR, 67 branching, 73-74 combining assignment and mathematical, 61-62 concatenation, 521-522 conditional, 82-83 conversion, 286-289 decrement ( ), 62-63 equals, 284-286 increment (++), 62-63, 271 insertion (overloading), 463-464 logical, 79 masking bit clearing, 68 bit settings, 67 flipping bits, 68 mathematical, 60-61 overloading, 270-286 placement in variable declarations, 248-249 postfix (overloading), 277 precedence list, 742-744 precedence value, 64 prefix overloading, 272-273 relational, 69-72, 80-81 p2v3/Index#3 scope resolution (listing 21.4), 618-620 shift right/left, 67 typeid, 651 unsigned short( ), 288-292 OR operator, 67, 80 ostrstream class, 662 output condition states, 496 flushing with cout object, 487-489 redirection operator (

Ngày đăng: 20/10/2013, 17:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan