Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 1.442 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
1.442
Dung lượng
8,24 MB
Nội dung
C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN SIXTH EDITION D.S MALIK Australia Brazil Japan Korea Mexico Singapore Spain United Kingdom United States This is an electronic version of the print textbook Due to electronic rights restrictions, some third party content may be suppressed Editorial review has deemed that any suppressed content does not materially affect the overall learning experience The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest C++ Programming: From Problem Analysis to Program Design, Sixth Edition D.S Malik Executive Editor: Marie Lee For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, www.cengage.com/support For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Acquisitions Editor: Brandi Shailer Further permissions questions can be emailed to permissionrequest@cengage.com Senior Product Manager: Alyssa Pratt Associate Product Manager: Stephanie Lorenz Content Project Manager: Matthew Hutchinson Art Director: Faith Brosnan Print Buyer: Julio Esperas Cover Designer: Roycroft Design/ www.roycroftdesign.com Cover Photo: ª Masterfile Royalty Free Proofreader: Andrea Schein Indexer: Elizabeth Cunningham Compositor: Integra Software Services ª Cengage Learning ALL RIGHTS RESERVED No part of this work covered by the copyright herein may be reproduced, transmitted, stored or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section or of the United States Copyright Act, without the prior written permission of the publisher Library of Congress Control Number: ISBN- : - - Cengage Learning Channel Center Street Boston, MA USA Some of the product names and company names used in this book have been used for identification purposes only and may be trademarks or registered trademarks of their respective manufacturers and sellers Any fictional data related to persons or companies or URLs used throughout this book is intended for instructional purposes only At the time this book was printed, any such data was fictional and not belonging to any real persons or companies Cengage Learning reserves the right to revise this publication and make changes from time to time in its content without notice The programs in this book are for instructional purposes only They have been tested with care, but are not guaranteed for any particular intent beyond educational purposes The author and the publisher not offer any warranties or representations, nor they accept any liabilities with respect to the programs Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil and Japan Locate your local office at: www.cengage.com/global Cengage Learning products are represented in Canada by Nelson Education, Ltd To learn more about Cengage Learning, visit www.cengage.com Purchase any of our products at your local college store or at our preferred online store www.CengageBrain.com Printed in the United States of America 16 17 16 15 14 13 12 TO My Daughter Shelly Malik This page intentionally left blank B RIEF C ONTENTS PREFACE An Overview of Computers and Programming Languages Basic Elements of C++ xxix 27 Input/Output 121 Control Structures I (Selection) 183 Control Structures II (Repetition) 259 User-Defined Functions 335 User-Defined Simple Data Types, Namespaces, and the string Type 451 Arrays and Strings 505 Records (structs) 591 10 Classes and Data Abstraction 629 11 Inheritance and Composition 709 12 Pointers, Classes, Virtual Functions, and Abstract Classes 781 13 Overloading and Templates 853 14 Exception Handling 943 15 Recursion 985 16 Searching, Sorting, and the vector Type 1015 17 Linked Lists 1057 18 Stacks and Queues 1149 vi | C++ Programming: From Problem Analysis to Program Design, Sixth Edition APPENDIX A Reserved Words 1249 APPENDIX B Operator Precedence 1251 APPENDIX C Character Sets 1253 APPENDIX D Operator Overloading 1257 APPENDIX E Additional C++ Topics 1259 APPENDIX F Header Files 1281 APPENDIX G Memory Size on a System and Random Number Generator 1291 APPENDIX H Standard Template Library (STL) 1293 APPENDIX I Answers to Odd-Numbered Exercises 1335 INDEX 1371 TABLE OF C ONTENTS Preface xxix AN OVERVIEW OF COMPUTERS AND PROGRAMMING LANGUAGES Introduction A Brief Overview of the History of Computers Elements of a Computer System Hardware Central Processing Unit and Main Memory Input /Output Devices Software The Language of a Computer The Evolution of Programming Languages Processing a C++ Program 10 Programming with the Problem Analysis–Coding–Execution Cycle 12 Programming Methodologies Structured Programming 20 20 Object-Oriented Programming 20 ANSI/ISO Standard C++ 22 Quick Review 22 Exercises 24 viii | C++ Programming: From Problem Analysis to Program Design, Sixth Edition BASIC ELEMENTS OF C++ 27 A Quick Look at a C++ Program 28 The Basics of a C++ Program 34 Comments Special Symbols 34 35 Reserved Words (Keywords) Identifiers 36 36 Whitespaces 37 Data Types 37 Simple Data Types Floating-Point Data Types 38 41 Data Types and Variables 42 Arithmetic Operators, Operator Precedence, and Expressions 43 Order of Precedence Expressions 46 48 Mixed Expressions 49 Type Conversion (Casting) 51 string Type 53 Variables, Assignment Statements, and Input Statements 54 Allocating Memory with Constants and Variables 54 Putting Data into Variables Assignment Statement 57 57 Saving and Using the Value of an Expression Declaring and Initializing Variables 61 62 Input (Read) Statement 63 Variable Initialization 66 Increment and Decrement Operators 70 Output 72 Preprocessor Directives namespace and Using cin and cout in a Program 79 80 Using the string Data Type in a Program 81 1384 | Index H h filename extension, 1278 hardware, overview of, 4–5 head, of linked lists, 1058, 1060, 1062 header file(s) control structures and, 232 derived classes and, 729–730 described, 662 file input/output and, 158 inheritance and, 729–731 linked lists and, 1091–1092, 1100–1103 multiple inclusions of, 730–731 naming conventions, 1278–1279 overview, 1282–1289 predefined functions and, 129 stacks and, 1164–1168, 1182 strings and, 556 templates and, 820 headerText.cpp.h, 731 height variable, 139, 140, 141, 146, 718, 720, 721, 722, 724 Hello! output, 289, 290, 538 hexadecimal (base 16) numbers, 1262–1264 high-level languages, Hollerith, Herman, 2–3 horizontal tab character (\t), 41 hr variable, 632, 633–634, 640, 643, 649–650, 654, 656, 854 hyphen (-), 35 I IBM (International Business Machines) EBCDIC encoding and, 8, 40 history of computing and, PCs (personal computers), introduction of, identifiers See also specific identifiers case sensitivity of, 36 compilers and, 57 described, 36 enumeration types and, 452–454 global, 386, 387, 471 illegal, 37 legal, 37 local, 386, 387 namespaces and, 471 naming, 90–91 overview, 36–37 scope of, 386, 387–390 self-documenting, 91 using, 80–81 IDEs (integrated development environments) control structures and, 313, 314 exception handling and, 959 identifiers and, 37 indentation and, 220 input/output and, 142, 159 management of files by, 666 overview, 11 preprocessor directives and, 79 warning messages and, 67 well-known, 12 if keyword, 197 if statements, 196–220, 351 functions and, 351 if .else statements and, comparing, 206–207 input failure and, 214–217 nested, 204, 205–206 operators and, 217–219, 880, 881 pairing else statements with, 204, 205 if .else statements, 196–220, 282 conditional operators and, 219 debugging and, 222 errors and, 212–214 if statements and, comparing, 206–207 incorporating multiple selections in, 204–206 switch statements and, 229 ifstream header file, 158, 732, 215, 216, 1270, 1271 ignore function, 133–134, 137, 140, 731 illustrate class, 673–675, 678–679 implementation files, 662, 820 linked, 1172–1185 implicit type coercion, 51 inches, calculating length in, 44–45 includes algorithm, 1324 increment (++) operator described, 70 operator overloading and, 854, 886–887 overview, 70–72 pointers and, 799 STL and, 1318 incrementHours function, 631, 632, 633, 644 incrementMinutes function, 631, 632, 633, 642, 644 incrementSeconds function, 631, 632, 633, 642, 644 incrementY function, 678 indentation, 93–94, 205, 219–220, 290 index(es), 513, 546 in bounds, 575, 576 described, 509 integral data type and, 526–527 out of bounds, 575, 576 indexLargestElement function, 524 indirection operator See dereferencing (*) operator Index | indirectly recursive functions, 988 infile variable, 215, 278, 282, 309, 542 infix notation, 1185 information hiding, 661–665 inheritance described, 743 hierarchy diagram, 711 overview, 710–737 pointers and, 819–826 pure virtual functions and, 828 initial statement, 288–290 initialize function, 404, 405, 519, 566, 611, 617, 1038–1039 initializeList function, 1079, 1110 initializeQueue operation, 1200, 1206, 1213–1216 initializeStack operation, 1157–1158, 1162, 1176 inner_product algorithm, 1326 inplace_merge algorithm, 1325 input See also input/output; input streams described, 63 devices, failure, 128, 138–141, 214–217 overview, 63–66 input stream(s), 122–128, 137, 731–732 described, 122 variables, 123 input/output See also file input/output; input; output debugging and, 154–157 devices, dot notation and, 137 files, specifying, at execution time, 541 input failure and 138–141 overview, 121–181 predefined functions and, 128–137 strings and, 153–154, 477, 541–542 structs and, 598 insert function, 481, 486–487, 920, 1113, 1300–1301, 1304, 1305 insertFirst function, 1072, 1078, 1083, 1092, 1098–1099, 1184, 1216 insertion (++) operator operator overloading and, 854, 865 setprecision manipulator and, 143 strings and, 540–541 insertion sort, 1021–1025 insertLast function, 1072, 1078, 1083, 1086, 1092, 1098–1099 insertNode function, 1099 insertNode operation, 1114 instance variables, 643, 679 int data type, 38, 60, 452, 631, 798–800, 803 arrays and, 508, 518, 524, 529 containers and, 1313 1385 control structures and, 195–196, 205, 228, 260, 262, 301 debugging and, 87 enumeration types and, 456 exception handling and, 968 initializing variables and, 62 input/output and, 125, 126, 127, 138, 140, 132, 147 logical expressions and, 195–196 operators and, 53, 70, 71, 72 overview, 39–40 pointers and, 782, 783–787, 794, 807 structs and, 605, 596 templates and, 918 typedef statements and, 462 variables and, 57 int keyword, 36 integer data type, 39 integers adding large, 587 classifying, 295–298 finding the sum of, 294 integral data type, 38, 43–44, 526–527 integral expressions, 48 integrated development environments (IDEs) control structures and, 313, 314 exception handling and, 959 identifiers and, 37 indentation and, 220 input/output and, 142, 159 management of files by, 666 overview, 11 preprocessor directives and, 79 warning messages and, 67 well-known, 12 interestRate variable, 43, 204 interface files See header files International Standards Organization See ISO (International Standards Organization) intList variable, 600 invalid_argument class, 956 inventory tracking, 543–544, 546–548 iomanip (iomanip.h) header file, 81, 146–147, 150, 153, 163, 476, 1278 ios class, 732 ios::app option, 161 iostream (iostream.h) header file, 79, 85, 97, 1278, 1279, 1291 functions and, 337 inheritance and, 730, 731–732 input/output and, 122, 153, 158 namespaces and, 80–81, 471, 474, 476 iSEmptyList function, 1078 1386 | Index isEmptyQueue operation, 1216, 1200, 1231 isEmptyStack operation, 1158, 1162, 1175–1176 isFullQueue function, 1213, 1231 isFullQueue operation, 1200 isFullStack operation, 1158, 1162–1163, 1174–1176 isGuessed variable, 275 islower function, 338, 340 isNegative variable, 303, 304 ISO (International Standards Organization), 22 isPalindrome function, 355–356 istream class, 732 istream data type, 122, 123, 137, 278 istream iterator, 1319 istream member functions, 128 istreamVar variable, 135, 140, 154 isupper function, 338 isVowel function, 491 iter_swap algorithm, 1325 iterative control structures, 999, 1000 See also loops iterator(s) declaring, 1299–1300 described, 1073 linked lists and, 1073–1076 overview, 1318–1323 STL and, 1298–1300, 1318–1323, 1293, 1298 use of the term, 1298 J Jacquard, Joseph, Java, 9, 744 Johns, Steven, juice machine program, 679–692 juiceMachine.h, 692 K key comparisons, 1016, 1017 keyboard shortcuts, 278 keywords described, 36 lists, 1249 keywords (listed by name) break keyword, 223 case keyword, 223 char keyword, 36 class keyword, 631 const keyword, 36, 602, 633, 638 default keyword, 223 keyword, 299 double keyword, 36 else keyword, 201 enum keyword, 453 extern keyword, 390 false keyword, 188, 190–191, 196 float keyword, 36 for keyword, 288 if keyword, 197 int keyword, 36 namespace keyword, 472 private keyword, 631, 632, 633, 636, 643 protected keyword, 631 public keyword, 631 return keyword, 345 static keyword, 673 static_cast keyword, 51, 52, 53 struct keyword, 592 switch keyword, 223 template keyword, 916 this keyword, 857 throw keyword, 950 true keyword, 188, 190–191, 196, 262 typedef keyword, 461 virtual keyword, 821 void keyword, 36 while keyword, 261 kilobytes (KB), 6, L larger function, 345–350, 362–363, 400, 916, 917–918 largerChar function, 400 largest function, 990–992 largest number calculations, 362–363 largestInRows function, 553, 555 last pointer, 1082 last variable, 1077 Last In First Out data structure See LIFO (Last In First Out) data structure LCVs (loop control variables) described, 262 incrementing/decrementing, 291 initializing, 263 while loops and, 262–263 left manipulator, 151–153 legalAge variable, 196 Leibniz, Gottfried von, length calculating, 44–45, 64, 66–70 converting, 96–99 length function, 129, 131, 481, 482, 484 length identifier, 36 Index | length variable, 33, 718, 720, 721, 722, 724, 798, 864, 867, 884 length_error class, 956 less than () operator, 63, 64–66 stream insertion (