intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Object Oriented Programming Using C++

Chia sẻ: Phung Tuyet | Ngày: | Loại File: PDF | Số trang:817

142
lượt xem
21
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Object-Oriented Programming Using C++ contains 14 chapters and five appendices that present clear text explanations, directed hands-on instruction, and a wealth of exercises. In these chapters, readers learn about programming logic in general, C++ syntax in particular, and gain an appreciation for and under- standing of the object-oriented approach.

Chủ đề:
Lưu

Nội dung Text: Object Oriented Programming Using C++

  1. Object-Oriented Programming Using C++ Fourth Edition Joyce Farrell Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
  2. Object-Oriented Programming © 2009 Course Technology, Cengage Learning Using C++, Fourth Edition ALL RIGHTS RESERVED. No part of this work covered by the copyright Joyce Farrell herein may be reproduced, transmitted, stored or used in any form or by any means—graphic, electronic, or mechanical, including but not limited Executive Editor: Marie Lee to photocopying, recording, scanning, digitizing, taping, Web distribution, Acquisitions Editor: Amy Jollymore information networks, or information storage and retrieval systems, except Managing Editor: Tricia Coia as permitted under Section 107 or 108 of the 1976 United States Copyright Act—without the prior written permission of the publisher. Developmental Editor: Lisa Ruffolo Editorial Assistant: Patrick Frank For product information and technology assistance, contact us at Marketing Manager: Bryant Chrzan Cengage Learning Customer & Sales Support, 1-800-354-9706 Content Project Manager: Erin Dowler For permission to use material from this text or product, submit all requests online at cengage.com/permissions Art Director: Bruce Bond Further permissions questions can be e-mailed to Manufacturing Coordinator: permissionrequest@cengage.com Julio Esperas Proofreader: Wendy Benedetto ISBN-13: 978-1-4239-0257-7 Cover Designer: Bruce Bond ISBN-10: 1-4239-0257-2 Cover Photo: © iStockphoto.com/ Storman Course Technology 25 Thomson Place Compositor: International Typesetting Boston, MA 02210 and Composition USA 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: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit course.cengage.com Purchase any of our products at your local college store or at our preferred online store www.ichapters.com 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. Course Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice. Printed in the United States of America 1 2 3 4 5 6 7 12 11 10 09 08
  3. BRIEF CONTENTS PREFACE xvii READ THIS BEFORE YOU BEGIN xxi CHAPTER 1 AN OVERVIEW OF OBJECT-ORIENTED PROGRAMMING AND C++ 1 CHAPTER 2 EVALUATING C++ EXPRESSIONS 51 CHAPTER 3 MAKING DECISIONS 81 CHAPTER 4 PERFORMING LOOPS 123 CHAPTER 5 UNDERSTANDING ARRAYS, STRINGS, AND POINTERS 165 CHAPTER 6 USING C++ FUNCTIONS 223 CHAPTER 7 USING CLASSES 283 CHAPTER 8 CLASS FEATURES AND DESIGN ISSUES 333 CHAPTER 9 UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS 385 CHAPTER 10 UNDERSTANDING INHERITANCE 451 CHAPTER 11 USING TEMPLATES 501 CHAPTER 12 HANDLING EXCEPTIONS 557 CHAPTER 13 ADVANCED INPUT AND OUTPUT 615 CHAPTER 14 ADVANCED TOPICS 673 APPENDIX A GETTING STARTED WITH MICROSOFT VISUAL STUDIO 2008 731 APPENDIX B GETTING STARTED WITH OTHER C++ COMPILERS 737 APPENDIX C OPERATOR PRECEDENCE AND ASSOCIATIVITY 745 APPENDIX D FORMATTING OUTPUT 749 APPENDIX E GENERATING RANDOM NUMBERS 755 GLOSSARY 761 INDEX 777 iii
  4. This page intentionally left blank
  5. CONTENTS PREFACE xvii READ THIS BEFORE YOU BEGIN xxi CHAPTER 1 AN OVERVIEW OF OBJECT-ORIENTED PROGRAMMING AND C++ 1 THE TASK OF PROGRAMMING 2 PROGRAMMING UNIVERSALS 3 PROCEDURAL PROGRAMMING 5 5 Early Procedural Programs 7 Modularity and Abstraction 10 Encapsulation OBJECT-ORIENTED PROGRAMMING 11 11 Objects and Classes 12 Inheritance 13 Polymorphism GETTING STARTED IN THE C++ PROGRAMMING ENVIRONMENT 13 14 Creating a main() Function WORKING WITH VARIABLES AND THE const QUALIFIER 16 18 The int Data Type 18 The char Data Type 19 The bool Data Type 19 Floating-Point Data Types 19 Declaring Variables 21 The const Qualifier CREATING COMMENTS 22 ANSI/ISO STANDARD C++ 23 24 Using Libraries, Preprocessor Directives, and namespace PRODUCING C++ OUTPUT 25 PROVIDING C++ INPUT 27 A FIRST LOOK AT DATA STRUCTURES AND CLASSES 29 YOU DO IT 32 32 Creating a Program That Displays Variable Values 34 Introducing Errors into a Program 35 Modifying a Program to Accept Input Values 36 Creating a Simple Structure v
  6. CONTENTS CHAPTER SUMMARY 37 KEY TERMS 38 REVIEW QUESTIONS 43 EXERCISES 45 CASE PROJECT 1 48 CASE PROJECT 2 48 UP FOR DISCUSSION 49 CHAPTER 2 EVALUATING C++ EXPRESSIONS 51 USING C++ BINARY ARITHMETIC OPERATORS 52 56 Using Modulus PRECEDENCE AND ASSOCIATIVITY OF ARITHMETIC OPERATIONS 58 SHORTCUT ARITHMETIC OPERATORS 59 59 Compound Assignment Operators 60 Increment and Decrement Operators OTHER UNARY OPERATORS 61 EVALUATING BOOLEAN EXPRESSIONS 63 PERFORMING OPERATIONS ON STRUCT FIELDS 65 YOU DO IT 67 67 Using Arithmetic Operators 68 Using Prefix and Postfix Increment and Decrement Operators 69 Using Operators with struct Fields CHAPTER SUMMARY 71 KEY TERMS 71 REVIEW QUESTIONS 73 EXERCISES 75 CASE PROJECT 1 78 CASE PROJECT 2 78 UP FOR DISCUSSION 79 CHAPTER 3 MAKING DECISIONS 81 USING THE IF STATEMENT 82 82 The Single-Alternative if 85 The Dual-Alternative if USING A NESTED IF 87 AVOIDING COMMON PITFALLS WITH IF STATEMENTS 89 89 Pitfall: Forgetting that C++ Comparisons are Case Sensitive Pitfalls: Assuming that indentation has a logical purpose, adding 90 an Unwanted Semicolon, and Forgetting Curly Braces vi
  7. CONTENTS 91 Pitfall: Using = Instead of == 93 Pitfall: Making Unnecessary Comparisons 94 Pitfall: Creating Unreachable Code USING THE SWITCH STATEMENT 96 USING THE CONDITIONAL OPERATOR 99 USING THE LOGICAL AND AND OR OPERATORS 100 100 Using the Logical AND Operator 102 Using the Logical OR Operator 104 Pitfall: Using OR When You Mean AND 104 Combining AND and OR Selections MAKING DECISIONS WITH STRUCTURE FIELDS 105 YOU DO IT 107 107 Using a Single-Alternative if 108 Using a Dual-Alternative if 109 Using a Compound Condition and Nested ifs CHAPTER SUMMARY 111 KEY TERMS 112 REVIEW QUESTIONS 113 EXERCISES 117 CASE PROJECT 1 121 CASE PROJECT 2 121 UP FOR DISCUSSION 122 CHAPTER 4 PERFORMING LOOPS 123 THE while LOOP 124 WRITING TYPICAL LOOPS 127 127 A Typical Loop: Input Verification 128 A Typical Loop: Reading Input Records AVOIDING COMMON PITFALLS WITH LOOPS 130 131 Pitfall: Adding an Unwanted Semicolon 132 Pitfalls: Forgetting Curly Braces or Forgetting to Alter a Loop Control Variable 133 Pitfall: Failing to Initialize a Loop Control Variable ACCUMULATING TOTALS 135 THE FOR LOOP 136 PRETEST VS. POSTTEST LOOPS 139 NESTED LOOPS 143 USING LOOPS WITH STRUCTURE FIELDS 145 YOU DO IT 149 149 Using a Loop to Validate User Data Entry 150 Using a Structure in an Application Containing Several Loops vii
  8. CONTENTS CHAPTER SUMMARY 154 KEY TERMS 155 REVIEW QUESTIONS 155 EXERCISES 159 CASE PROJECT 1 161 CASE PROJECT 2 162 UP FOR DISCUSSION 163 CHAPTER 5 UNDERSTANDING ARRAYS, STRINGS, AND POINTERS 165 UNDERSTANDING MEMORY ADDRESSES 166 UNDERSTANDING ARRAYS 167 STORING VALUES IN AN ARRAY 170 ACCESSING AND USING ARRAY VALUES 172 AVOIDING COMMON ARRAY ERRORS 175 175 Pitfall: Forgetting that Arrays are Zero-Based 176 Pitfall: Accessing Locations Beyond the Array USING PART OF AN ARRAY 177 USING PARALLEL ARRAYS 180 CREATING ARRAYS OF STRUCTURE OBJECTS 183 USING TWO-DIMENSIONAL ARRAYS 185 USING CHARACTER ARRAY STRINGS 188 189 Strings Created as Arrays of Characters Special String-Handling Problems When Using 190 Character Arrays AN INTRODUCTION TO THE STRING CLASS 197 USING POINTERS 200 USING A POINTER INSTEAD OF AN ARRAY NAME 201 YOU DO IT 205 205 Using an Array 208 Understanding Memory Addresses CHAPTER SUMMARY 210 KEY TERMS 211 REVIEW QUESTIONS 212 EXERCISES 215 CASE PROJECT 1 220 CASE PROJECT 2 220 UP FOR DISCUSSION 221 viii
  9. CONTENTS CHAPTER 6 USING C++ FUNCTIONS 223 WRITING SIMPLE FUNCTIONS 224 PLACING FUNCTIONS WITHIN FILES 226 226 Placing a Function as Part of the Same File, Before main() 228 Placing a Function as Part of the Same File, After main() 230 Placing a Function in Its Own File UNDERSTANDING PROCEDURAL ABSTRACTION 232 UNDERSTANDING SCOPE 234 234 Distinguishing Between Local and Global Variables 237 Using the Scope Resolution Operator RETURNING VALUES FROM FUNCTIONS 239 PASSING VALUES TO FUNCTIONS 243 AVOIDING COMMON ERRORS WHEN USING FUNCTIONS 246 246 Pitfall: Neglecting to Make Sure the Function Declaration, Header, and Call Agree 247 Pitfall: Indicating an Argument Type in a Function Call 247 Pitfall: Indicating a Return Type in a Function Call 247 Pitfall: Ignoring the Order of Parameters 248 Pitfall: Assuming that an Unused Return Value Has an Effect USING OBJECTS AS PARAMETERS TO, AND AS RETURN TYPES OF, FUNCTIONS 248 PASSING ADDRESSES TO FUNCTIONS 250 USING REFERENCE VARIABLES WITH FUNCTIONS 253 253 Declaring Reference Variables 255 Passing Variable Addresses to Reference Variables PASSING ARRAYS TO FUNCTIONS 258 USING INLINE FUNCTIONS 260 USING DEFAULT PARAMETERS 262 OVERLOADING FUNCTIONS 264 266 Avoiding Ambiguity YOU DO IT 267 267 Writing Functions That Return Values 269 Writing a Function That Requires a Parameter CHAPTER SUMMARY 270 KEY TERMS 271 REVIEW QUESTIONS 273 EXERCISES 276 CASE PROJECT 1 281 CASE PROJECT 2 281 UP FOR DISCUSSION 282 ix
  10. CONTENTS CHAPTER 7 USING CLASSES 283 CREATING CLASSES 284 ENCAPSULATING CLASS COMPONENTS 286 287 Designing Classes IMPLEMENTING FUNCTIONS IN A CLASS 289 290 Using Public Functions to Alter Private Data UNUSUAL USE: USING PRIVATE FUNCTIONS AND PUBLIC DATA 295 CONSIDERING SCOPE WHEN DEFINING MEMBER FUNCTIONS 298 USING STATIC CLASS MEMBERS 301 301 Defining Static Data Members 305 Using Static Functions UNDERSTANDING THE THIS POINTER 307 311 Using the this Pointer Explicitly 311 Using the Pointer-to-Member Operator UNDERSTANDING POLYMORPHISM 313 YOU DO IT 314 314 Creating and Using a Class 317 Using a static Field 320 Understanding How static and Non-static Fields are Stored CHAPTER SUMMARY 321 KEY TERMS 322 REVIEW QUESTIONS 323 EXERCISES 327 CASE PROJECT 1 330 CASE PROJECT 2 331 UP FOR DISCUSSION 331 CHAPTER 8 CLASS FEATURES AND DESIGN ISSUES 333 CLASSIFYING THE ROLES OF MEMBER FUNCTIONS 334 UNDERSTANDING CONSTRUCTORS 335 WRITING CONSTRUCTORS WITHOUT PARAMETERS 336 WRITING CONSTRUCTORS WITH PARAMETERS 341 344 Pitfall: Using Parentheses When Instantiating an Object with a Default Constructor OVERLOADING CONSTRUCTORS 345 USING DESTRUCTORS 347 UNDERSTANDING COMPOSITION 352 355 Using Composition When Member Classes Contain Non-default Constructors USING #IFNDEF, #DEFINE, AND #ENDIF 358 IMPROVING CLASSES 361 x
  11. CONTENTS 361 Selecting Member Data and Function Names 363 Reducing Coupling Between Functions 363 Increasing Cohesion in a Function YOU DO IT 365 365 Creating a Class with a Constructor 368 Using Constructor Parameters 369 Understanding Composition CHAPTER SUMMARY 372 KEY TERMS 374 REVIEW QUESTIONS 375 EXERCISES 378 CASE PROJECT 1 382 CASE PROJECT 2 383 UP FOR DISCUSSION 384 CHAPTER 9 UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS 385 WHAT ARE FRIENDS? 386 HOW TO DECLARE A FUNCTION AS A FRIEND 387 UNDERSTANDING THE BENEFITS OF OVERLOADING AND POLYMORPHISM 391 USING A FRIEND FUNCTION TO ACCESS DATA FROM TWO CLASSES 393 395 Using a Forward Declaration OVERLOADING OPERATORS—THE GENERAL RULES 397 OVERLOADING AN ARITHMETIC OPERATOR 402 405 Paying Attention to the Order of the Operands OVERLOADING AN OPERATOR TO WORK WITH AN OBJECT AND A PRIMITIVE TYPE 406 USING MULTIPLE OPERATIONS IN AN EXPRESSION 409 OVERLOADING OUTPUT 412 OVERLOADING INPUT 416 OVERLOADING THE PREFIX ++ AND – – OPERATORS 418 USING POSTFIX INCREMENT AND DECREMENT OPERATORS 421 OVERLOADING THE == OPERATOR 422 OVERLOADING THE = OPERATOR 424 OVERLOADING [ ] AND ( ) 430 YOU DO IT 434 434 Overloading an Arithmetic Operator 435 Overloading an Output Operator CHAPTER SUMMARY 436 KEY TERMS 438 xi
  12. CONTENTS REVIEW QUESTIONS 439 EXERCISES 442 CASE PROJECT 1 447 CASE PROJECT 2 448 UP FOR DISCUSSION 449 CHAPTER 10 UNDERSTANDING INHERITANCE 451 UNDERSTANDING INHERITANCE 452 UNDERSTANDING THE ADVANTAGES PROVIDED BY INHERITANCE 453 CREATING A DERIVED CLASS 454 UNDERSTANDING INHERITANCE RESTRICTIONS 458 CHOOSING THE CLASS ACCESS SPECIFIER 462 OVERRIDING INHERITED ACCESS 463 OVERRIDING AND OVERLOADING PARENT CLASS FUNCTIONS 467 PROVIDING FOR BASE CLASS CONSTRUCTION 474 USING MULTIPLE INHERITANCE 478 DISADVANTAGES OF USING MULTIPLE INHERITANCE 481 USING VIRTUAL BASE CLASSES 482 YOU DO IT 484 484 Creating a Base Class 486 Creating a Child Class 488 Creating Another Child Class 489 Using Multiple Inheritance CHAPTER SUMMARY 491 KEY TERMS 492 REVIEW QUESTIONS 493 EXERCISES 496 CASE PROJECT 1 498 CASE PROJECT 2 499 UP FOR DISCUSSION 500 CHAPTER 11 USING TEMPLATES 501 UNDERSTANDING THE USEFULNESS OF FUNCTION TEMPLATES 502 CREATING FUNCTION TEMPLATES 504 USING MULTIPLE PARAMETERS IN FUNCTION TEMPLATES 506 OVERLOADING FUNCTION TEMPLATES 509 USING MORE THAN ONE TYPE IN A FUNCTION TEMPLATE 511 USING MORE THAN ONE PARAMETERIZED TYPE IN A FUNCTION TEMPLATE 513 xii
  13. CONTENTS EXPLICITLY SPECIFYING THE TYPE IN A FUNCTION TEMPLATE 516 518 Using Multiple Explicit Types in a Function Template USING CLASS TEMPLATES 519 CREATING A COMPLETE CLASS TEMPLATE 521 UNDERSTANDING THE USEFULNESS OF CONTAINER CLASSES 523 CREATING AN ARRAY TEMPLATE CLASS 525 INTRODUCTION TO THE STANDARD TEMPLATE LIBRARY 531 536 Inserting a New Element into a Vector Using Iterators and the insert() Method 536 Sorting Vector Elements Using the sort() Algorithm YOU DO IT 540 540 Creating a Function Template 541 Proving the Template Function Works with Class Objects CHAPTER SUMMARY 544 KEY TERMS 546 REVIEW QUESTIONS 547 EXERCISES 550 CASE PROJECT 1 553 CASE PROJECT 2 554 UP FOR DISCUSSION 555 CHAPTER 12 HANDLING EXCEPTIONS 557 UNDERSTANDING THE LIMITATIONS OF TRADITIONAL ERROR HANDLING METHODS 558 THROWING EXCEPTIONS 560 USING TRY BLOCKS 563 CATCHING EXCEPTIONS 564 USING MULTIPLE THROW STATEMENTS AND MULTIPLE CATCH BLOCKS 566 568 Determining the Order of catch Blocks USING THE DEFAULT EXCEPTION HANDLER 569 UNDERSTANDING EXCEPTION CLASSES IN THE STANDARD RUNTIME LIBRARY 570 571 An Example of an Automatically Thrown logic_error 573 Using the what() Function 573 Explicitly Throwing a Built-in exception DERIVING YOUR OWN EXCEPTIONS FROM THE EXCEPTION CLASS 576 580 Overriding the Exception Class what() Function USING EXCEPTION SPECIFICATIONS 582 582 Exception Specifications in ANSI C++ 583 How Visual C++ Departs from the ANSI Standard UNWINDING THE STACK 583 RETHROWING EXCEPTIONS 589 xiii
  14. CONTENTS HANDLING MEMORY ALLOCATION EXCEPTIONS 591 WHEN TO USE EXCEPTION HANDLING 593 YOU DO IT 594 594 Creating a Typical Data Entry Application 595 Modifying the SimpleDataEntry Program to Throw an Exception 597 Creating a Custom Exception Class 599 Throwing and Catching Multiple Exceptions 602 Using a Generic catch Block CHAPTER SUMMARY 603 KEY TERMS 605 REVIEW QUESTIONS 606 EXERCISES 609 CASE PROJECT 1 613 CASE PROJECT 2 613 UP FOR DISCUSSION 614 CHAPTER 13 ADVANCED INPUT AND OUTPUT 615 UNDERSTANDING CIN AND COUT AS OBJECTS 616 USING ISTREAM MEMBER FUNCTIONS 618 618 Using the get() Function 621 Using the ignore() Function 623 Using the getline() Function 624 Other istream Member Functions USING OSTREAM MEMBER FUNCTIONS 625 625 Using Format Flags with setf() and unsetf() 626 Using the width() Function 627 Using the precision() Function 627 Other ostream Member Functions USING MANIPULATORS 628 629 Using the setprecision() Manipulator 630 Using the setw() Manipulator 631 Using the setiosflags() and resetiosflags() Manipulators 631 Using the oct, hex, and showbase manipulators CREATING MANIPULATOR FUNCTIONS 632 UNDERSTANDING COMPUTER FILES 634 SIMPLE FILE OUTPUT 636 SIMPLE FILE INPUT 642 WRITING AND READING OBJECTS 644 WORKING WITH RANDOM ACCESS FILES 648 650 Setting up a File for Direct Access xiv
  15. CONTENTS 652 Writing Records to a File Randomly 653 Randomly Reading the File YOU DO IT 655 655 Using stream Functions and Manipulators 657 Creating a Manipulator 658 Writing to and Reading from a File CHAPTER SUMMARY 660 KEY TERMS 662 REVIEW QUESTIONS 664 EXERCISES 667 CASE PROJECT 1 669 CASE PROJECT 2 670 UP FOR DISCUSSION 671 CHAPTER 14 ADVANCED TOPICS 673 USING ENUMERATIONS 674 UNDERSTANDING THE BINARY SYSTEM 676 UNDERSTANDING WHY COMPUTERS USE THE BINARY SYSTEM 679 USING INDIVIDUAL BITS TO STORE DATA 681 CONVERTING A GROUP OF BIT FIELDS TO AN INTEGER VALUE 684 USING THE BITWISE AND OPERATOR WITH A MASK 691 USING THE BITWISE INCLUSIVE OR OPERATOR 696 SHIFTING BITS 702 UNDERSTANDING RECURSION 704 USING A RECURSIVE FUNCTION TO SORT A LIST 708 YOU DO IT 712 712 Working with an Enumeration 713 Working with Bits 715 Using a Multi-Bit Field Using a Mask to Convert Letters from Lowercase 716 to Uppercase 717 Working with Recursion CHAPTER SUMMARY 719 KEY TERMS 720 REVIEW QUESTIONS 721 EXERCISES 724 CASE PROJECT 1 728 CASE PROJECT 2 728 UP FOR DISCUSSION 729 xv
  16. CONTENTS APPENDIX A GETTING STARTED WITH MICROSOFT VISUAL STUDIO 2008 731 APPENDIX B GETTING STARTED WITH OTHER C++ COMPILERS 737 APPENDIX C OPERATOR PRECEDENCE AND ASSOCIATIVITY 745 APPENDIX D FORMATTING OUTPUT 749 APPENDIX E GENERATING RANDOM NUMBERS 755 GLOSSARY 761 INDEX 777 xvi
  17. PREFACE Object-Oriented Programming Using C++, Fourth Edition is designed for many levels of programming stu- dents and a variety of programming teaching styles. Readers who are new to programming will find the basics of programming logic and the C++ programming language covered thoroughly and clearly. Comprehensive, engaging explanations, multiple programming examples, and step-by-step programming lessons provide beginning readers with a solid C++ background. Users who know some C++ syntax, but are new to object-oriented programming, will find objects explored thoroughly from the first chapters. Objects are introduced early, so those who want to learn objects at the start of their programming experience can do so. Users who want to postpone objects can simply omit the later sections of each chapter and cover the basic programming structures with simple data types before returning to the more complex objects later on. ORGANIZATION AND COVERAGE Object-Oriented Programming Using C++ contains 14 chapters and five appendices that present clear text explanations, directed hands-on instruction, and a wealth of exercises. In these chapters, readers learn about programming logic in general, C++ syntax in particular, and gain an appreciation for and under- standing of the object-oriented approach. When readers complete the book, they will have an under- standing of object-oriented concepts as they apply to programming, and the ability to use these concepts to develop C++ programs. Chapter 1 provides an overview of programming in general and C++ in particular. You work with vari- ables, comments, input and output, and data structures. This book distinguishes itself from other C++ books by introducing structure objects in Chapter 1 so that students start thinking in an object-oriented manner from the beginning of the course. Chapter 2 focuses on evaluating C++ expressions. Chapters 3, 4, and 5 discuss decisions, loops, arrays, strings, and pointers—all fundamental building blocks of C++ programs. Chapter 6 provides a solid foun- dation in writing and using functions including passing parameters by value and by reference and return- ing values from functions. Once students understand C++ basics they are ready for Chapters 7 and 8, which delve more completely into the object-oriented aspects of C++, featuring classes, objects, and design issues. Friend functions and operator overloading are covered in Chapter 9, and inheritance, another important OO feature, is explained in Chapter 10. Advanced C++ features such as templates, exception handling, and advanced input and output techniques, including writing objects to files, are covered in Chapters 11, 12, and 13. Chapter 14 presents some inter- esting adjuncts to C++ that make it such a powerful language, including creating enumerations, working with bits, and understanding recursion. Five appendices offer further explanation to topics mentioned in the chapters. Appendices A and B describe how to get started with various C++ compilers. Appendix C contains a handy table of precedence and associativity. Appendix D contains information on formatting output, and Appendix E is a lesson in generating random numbers—an important skill in creating scientific simulations and games. xvii
  18. PREFACE APPROACH Object-Oriented Programming Using C++ teaches object-oriented concepts using C++ as a tool to demonstrate these concepts. This book teaches programming concepts using a task-driven rather than a command-driven approach. Structures are introduced in Chapter 1 so that students start thinking about objects right from the start. However, discussion of objects is reserved for the last sections of the first six chapters, so that instructors who prefer to start with a procedural approach can omit these sections at first, then go back to cover them after the first six chapters have been completed. FEATURES Object-Oriented Programming Using C++ is an exceptional textbook because it also includes the following features: » Objectives. A brief list of objectives appears at the beginning of each chapter so the student has an overview of the main topics to be covered. » Notes. These provide additional information about a procedure or topic, such as an alternative method of performing a procedure. » Figures. Each chapter averages over 35 figures that contain diagrams, code, working programs, or screen shots of the programs’ execution. » NEW! Color. Besides adding visual interest to the text, the new two-color design is used to highlight C++ keywords each time they appear in a figure. » NEW! Don’t Do It Icon. It is sometimes illustrative to show an example of how NOT to do something—for example, having a dead code path in a program. However, students do not always read carefully and sometimes use logic similar to that shown in what is intended to be a “bad” example. When the instructor is critical, the frustrated student says, “But that’s how they did it in the book!” Therefore, although the text will continue to describe bad examples, and the captions for the related figures will mention that they are bad examples, the book also includes a “Don’t Do It” icon near the offend- ing section of logic. This icon provides a visual jolt to the student, emphasizing that particular figures are NOT to be emulated. » NEW! Section Quiz. “Two truths and a lie” appears after each chapter section, with answers provided. This quiz contains three statements from the preceding section of text—two true and one false. Over the years, students have requested answers to problems, but we have hesitated to distribute them in case instructors want to use problems as assignments or test questions. These true-false mini- quizzes provide students with immediate feedback as they read, without “giving away” answers to the existing multiple choice and programming problem questions. » You Do It. After students study each chapter’s concepts, they are invited to create small applications that illustrate the concepts. Each application is explained step-by-step as the students add appropriate code to interesting applications. » Summaries. A summary that recaps the programming concepts and commands covered follows each chapter. » Key Terms. Each chapter contains a list of all the key terms defined in the chapter, along with explanations and presented in the order covered in the chapter. The list of Key Terms serves as an additional chapter summary. xviii
  19. PREFACE » Review Questions. Each chapter includes 20 multiple choice review questions that test students’ understanding of what they learned in the chapter. » Programming Exercises. Each chapter contains interesting exercises that provide students with the opportunity to apply the concepts they have mastered by writing C++ programs. » Gaming Exercises. In addition to many business-oriented programming exercises at the end of each NEW! chapter, most chapters now also contain at least one game-oriented exercise. » Debugging Exercises. Each chapter ends with debugging exercises—programs with a few syntax or logical errors. The student can find the errors and fix them, developing crucial skills of reading others’ programs, analyzing error messages and probable cause of errors, and solving problems. » Running Case. The book contains two running cases in which the student develops large classes, adding appropriate features as each new concept is introduced. By the end of the book, the student has created two substantial working classes. » Up For Discussion. Each chapter ends with open-ended, frequently thought-provoking questions NEW! that are appropriate for classroom or online discussion sessions. » Syntax Improvements. Three minor, but important improvements have been made in the C++ syn- NEW! tax shown throughout the book. First, the main() function of a program always returns 0, consistent with common business and ANSI standards. Second, spaces have been inserted surrounding all the insertion and extraction operators, making code easier to read. Third, the asterisks for pointers and the ampersands for references are shown next to the data type in declarations, with a space follow- ing. For example : int* ptr;. This makes it clearer that the data type of ptr is int pointer. » Glossary. All of the Key Terms presented at the end of each chapter are listed in alphabetical order in a Glossary at the end of the book. INSTRUCTOR RESOURCES The following supplemental materials are available when this book is used in a classroom setting. All of the instructor resources for this book are provided to the instructor on a single CD-ROM. Electronic Instructor’s Manual. The Instructor’s Manual that accompanies this textbook includes additional instructional material to assist in class preparation, including suggestions for lecture topics. ExamView®. This textbook is accompanied by ExamView, a powerful testing software package that allows instructors to create and administer printed, computer (LAN-based), and Internet exams. ExamView includes hundreds of questions that correspond to the topics covered in this text, enabling students to generate detailed study guides that include page references for further review. The computer- based and Internet testing components allow students to take exams at their computers, and save the instructor time by grading each exam automatically. PowerPoint Presentations. This book comes with Microsoft PowerPoint slides for each chapter. These slides are included as a teaching aid for classroom presentation; teachers can make them available on the network for chapter review, or print them for classroom distribution. Instructors can add their own slides for additional topics they introduce to the class. Solution Files. Password-protected solutions to all “You Do It” exercises and end-of-chapter exercises are provided on the Instructor Resources CD-ROM and on the Course Technology Web site at www.course.com. xix
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2