1. Trang chủ
  2. » Khoa Học Tự Nhiên

C++ how to program, 5th ed 2005

3,2K 187 0
Tài liệu đã được kiểm tra trùng lặp

Đ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 3.186
Dung lượng 16,73 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Software Engineering Case Study: Introduction to Object Section 2.7.. Optional Software Engineering Case Study: Examining the... Optional Software Engineering Case Study: Identifying...

Trang 1

Pages : 1536

Best-selling C++ text significantly revised to include new early objects coverage and new streamlined case studies.

Trang 3

Copying and Organizing Files lix Copying the Book Examples from the CD lx Changing the Read-Only Property of Files lx Chapter 1 Introduction to Computers, the Internet and World Wide Web 1

Section 1.2 What Is a Computer? 3 Section 1.3 Computer Organization 4 Section 1.4 Early Operating Systems 5 Section 1.5 Personal, Distributed and Client/Server Computing 5 Section 1.6 The Internet and the World Wide Web 6 Section 1.7 Machine Languages, Assembly Languages and High-Level

Section 1.8 History of C and C++ 8 Section 1.9 C++ Standard Library 8

Section 1.11 FORTRAN, COBOL, Pascal and Ada 10 Section 1.12 Basic, Visual Basic, Visual C++, C# and NET 11 Section 1.13 Key Software Trend: Object Technology 11 Section 1.14 Typical C++ Development Environment 12 Section 1.15 Notes About C++ and C++ How to Program, 5/e 15 Section 1.16 Test-Driving a C++ Application 16 Section 1.17 Software Engineering Case Study: Introduction to Object

Section 2.7 Decision Making: Equality and Relational Operators 51 Section 2.8 (Optional) Software Engineering Case Study: Examining the

Trang 5

Section 4.12 Increment and Decrement Operators 161 Section 4.13 (Optional) Software Engineering Case Study: Identifying

Trang 6

Section 6.11 Function Call Stack and Activation Records 268 Section 6.12 Functions with Empty Parameter Lists 272 Section 6.13 Inline Functions 273 Section 6.14 References and Reference Parameters 275 Section 6.15 Default Arguments 280 Section 6.16 Unary Scope Resolution Operator 282 Section 6.17 Function Overloading 283 Section 6.18 Function Templates 286

Section 6.20 Example Using Recursion: Fibonacci Series 292 Section 6.21 Recursion vs Iteration 295 Section 6.22 (Optional) Software Engineering Case Study: Identifying

Section 7.7 Searching Arrays with Linear Search 358 Section 7.8 Sorting Arrays with Insertion Sort 359 Section 7.9 Multidimensional Arrays 362 Section 7.10 Case Study: Class GradeBook Using a Two-Dimensional

Section 7.11 Introduction to C++ Standard Library Class Template vector 372 Section 7.12 (Optional) Software Engineering Case Study: Collaboration

Trang 7

Section 9.2 Time Class Case Study 482 Section 9.3 Class Scope and Accessing Class Members 487 Section 9.4 Separating Interface from Implementation 489 Section 9.5 Access Functions and Utility Functions 491 Section 9.6 Time Class Case Study: Constructors with Default Arguments 493

Section 9.8 When Constructors and Destructors Are Called 500

Trang 8

Section 9.9 Time Class Case Study: A Subtle TrapReturning a Reference

to a private Data Member

503 Section 9.10 Default Memberwise Assignment 506 Section 9.11 Software Reusability 508 Section 9.12 (Optional) Software Engineering Case Study: Starting to

Section 10.7 static Class Members 552 Section 10.8 Data Abstraction and Information Hiding 558 Section 10.9 Container Classes and Iterators 561

Section 11.6 Overloading Unary Operators 581 Section 11.7 Overloading Binary Operators 581

Trang 9

Section 11.8 Case Study: Array Class 582 Section 11.9 Converting between Types 594 Section 11.10 Case Study: String Class 595 Section 11.11 Overloading ++ and 607 Section 11.12 Case Study: A Date Class 609 Section 11.13 Standard Library Class string 613 Section 11.14 explicit Constructors 617

Time Type Information with Downcasting, dynamic_cast, typeid and

Section 13.8 Case Study: Payroll System Using Polymorphism and Run-type_info

732

Trang 10

Section 13.9 Virtual Destructors 735 Section 13.10 (Optional) Software Engineering Case Study: Incorporating

Trang 11

Exercises 808

Section 16.2 Exception-Handling Overview 812 Section 16.3 Example: Handling an Attempt to Divide by Zero 812 Section 16.4 When to Use Exception Handling 819 Section 16.5 Rethrowing an Exception 820 Section 16.6 Exception Specifications 821 Section 16.7 Processing Unexpected Exceptions 822 Section 16.8 Stack Unwinding 823 Section 16.9 Constructors, Destructors and Exception Handling 824 Section 16.10 Exceptions and Inheritance 825 Section 16.11 Processing new Failures 825 Section 16.12 Class auto_ptr and Dynamic Memory Allocation 829 Section 16.13 Standard Library Exception Hierarchy 832 Section 16.14 Other Error-Handling Techniques 834

Trang 12

Section 18.5 Swapping strings 891 Section 18.6 string Characteristics 892 Section 18.7 Finding Strings and Characters in a string 894 Section 18.8 Replacing Characters in a string 896 Section 18.9 Inserting Characters into a string 898 Section 18.10 Conversion to C-Style Pointer-Based char * Strings 899

Section 19.13 Case Study: An Interactive Web Page 939

Section 19.15 Server-Side Files 949 Section 19.16 Case Study: Shopping Cart 954

Trang 13

Section 19.17 Wrap-Up 969 Section 19.18 Internet and Web Resources 969

Section 22.2 Structure Definitions 1058 Section 22.3 Initializing Structures 1061 Section 22.4 Using Structures with Functions 1061

Trang 15

Section 24.6 Pointers to Class Members (.* and ->*) 1211 Section 24.7 Multiple Inheritance 1213 Section 24.8 Multiple Inheritance and virtual Base Classes 1218

Trang 16

Section E.10 Dynamic Memory Allocation with calloc and realloc 1260 Section E.11 The Unconditional Branch: goto 1261

Section G.8 Class BankDatabase 1300 Section G.9 Class Transaction 1304 Section G.10 Class BalanceInquiry 1306 Section G.11 Class Withdrawal 1308

Trang 17

Section G.12 Class Deposit 1313 Section G.13 Test Program ATMCaseStudy.cpp 1316

Appendix H UML 2: Additional Diagram Types 1318

Section H.2 Additional Diagram Types 1318 Appendix I C++ Internet and Web Resources 1320

Section L.5 The Autos Window 1377

Trang 18

Section M.2 Breakpoints and the run, stop, continue and print Commands 1382 Section M.3 The print and set Commands 1389 Section M.4 Controlling Execution Using the step, finish and next

3 Index

Trang 19

[Page iv]

Trang 21

programs to determine their effectiveness The authors andpublisher make no warranty of any kind, expressed or implied,with regard to these programs or to the documentation

contained in this book The authors and publisher shall not beliable in any event for incidental or consequential damages inconnection with, or arising out of, the furnishing, performance,

or use of these programs

Many of the designations used by manufacturers and sellers todistinguish their products are claimed as trademarks and

registered trademarks Where those designations appear in thisbook, and Prentice Hall and the authors were aware of a

trademark claim, the designations have been printed in initialcaps or all caps All product names mentioned remain

Trang 22

All rights reserved No part of this book may be reproduced, inany form or by any means, without permission in writing fromthe publisher

Dive Into is a registered trademark of Deitel & Associates, Inc.GNU is a trademark of the Free Software Foundation

Java and all Java-based marks are trademarks or registeredtrademarks of Sun Microsystems, Inc in the United States and

Trang 23

Microsystems, Inc

Linux is a registered trademark of Linus Torvalds

Microsoft, Microsoft® Internet Explorer and the Windows logoare either registered trademarks or trademarks of MicrosoftCorporation in the United States and/or other countries

Netscape browser window © 2004 Netscape CommunicationsCorporation Used with permission Netscape Communicationshas not authorized, sponsored, endorsed, or approved thispublication and is not responsible for its content

Object Management Group, OMG, Unified Modeling Languageand UML are trademarks of Object Management Group, Inc

Trang 24

For your mentorship, friendship, and tireless devotion to insisting that we "get it right" and helping us do so.

It is a privilege to work with such consummate C++

professionals.

Harvey M Deitel and Paul J Deitel

Trang 25

[Page ii]

Trang 26

Deitel® Books, Cyber Classrooms, Complete Training Courses and Web- Based Training Courses published by Prentice Hall

Trang 27

Simply Visual Basic® NET: An Application Driven TutorialApproach (Visual Studio NET 2003 Edition)

Trang 28

C# Multimedia Cyber Classroom

e-Business and e-Commerce Multimedia Cyber Classroom

Trang 29

The Complete Perl Training Course

Trang 31

[Page xxiii]

Trang 32

materials that help instructors maximize their students' learning

experience, including the Instructor's Resource CD,

PowerPoint® Slide lecture notes, course management systems,SafariX (Pearson Education's WebBook publications) and more

Trang 33

smaller, more manageable chapters (e.g., Chapter 1 of theFourth Edition has been split into Chapters 12; Chapter 2 ofthe Fourth Edition is now Chapters 45)

Early Classes and Objects Approach We changed to an

early classes and objects pedagogy Students are

introduced to the basic concepts and terminology of objecttechnology in Chapter 1 In the previous edition, studentsbegan developing customized, reusable classes and objects

in Chapter 6, but in this edition, they do so in our

completely new Chapter 3 Chapters 47 have been carefullyrewritten from an "early classes and objects" perspective.This new edition is object oriented, where appropriate, fromthe start and throughout the text Moving the discussion ofobjects and classes to earlier chapters gets students

"thinking about objects" immediately and mastering theseconcepts more completely Object-oriented programming isnot trivial by any means, but it's fun to write object-

Trang 34

[Page xxiv]

Integrated Case Studies We have added several case

studies spanning multiple sections and chapters that oftenbuild on a class introduced earlier in the book to

demonstrate new programming concepts later in the book.These case studies include the development of the GradeBook

class in Chapters 37, the Time class in several sections ofChapters 910, the Employee class in Chapters 1213, and theoptional OOD/UML ATM case study in chapters 1-7, 9, 13and Appendix G

Integrated GradeBook Case Study We added a new GradeBook

case study to reinforce our early classes presentation Ituses classes and objects in Chapters 37 to incrementallybuild a GradeBook class that represents an instructor's gradebook and performs various calculations based on a set ofstudent grades, such as calculating the average grade,

relationships, and we use UML activity diagrams to

demonstrate the flow of control in each of C++'s controlstatements We make especially heavy use of the UML inthe optional OOD/UML ATM case study

Trang 35

optional elevator simulator case study from the previousedition with a new optional OOD/UML automated teller

machine (ATM) case study in the Software Engineering CaseStudy sections of Chapters 17, 9 and 13 The new case

study is simpler, smaller, more "real world" and more

appropriate for first and second programming courses Thenine case study sections present a carefully paced

introduction to object-oriented design using the UML Weintroduce a concise, simplified subset of the UML 2.0, thenguide the reader through a first design experience intendedfor the novice object-oriented designer/programmer Ourgoal in this case study is to help students develop an

object-oriented design to complement the object-orientedprogramming concepts they begin learning in Chapter 1 andimplementing in Chapter 3 The case study was reviewed by

a distinguished team of OOD/ UML academic and industryprofessionals The case study is not an exercise; rather, it is

a fully developed end-to-end learning experience that

line C++ code implementation We take a detailed tour ofthe nine sections of this case study later in the Preface

concludes with a detailed walkthrough of the complete 877- File Programs Chapter 3 includes a detailed diagram anddiscussion of the compilation and linking process that

Compilation and Linking Process for Multiple-Source-produces an executable application

Function Call Stack Explanation In Chapter 6, we

provide a detailed discussion (with illustrations) of the

function call stack and activation records to explain howC++ is able to keep track of which function is currently

executing, how automatic variables of functions are

maintained in memory and how a function knows where toreturn after it completes execution

Trang 36

Early Introduction of C++ Standard Library string and

vector Objects The string and vector classes are used tomake earlier examples more object-oriented

Class string We use class stringbased char * strings for most string manipulations

instead of C-like pointer-throughout the book We continue to include discussions of

char * strings in Chapter 8, 10, 11 and 22 to give studentspractice with pointer manipulations, to illustrate dynamicmemory allocation with new and delete, to build our own

String class, and to prepare students for assignments inindustry where they will work with char * strings in C andC++ legacy code

Class Template vector We use class template vector

instead of C-like pointer-based array manipulations

like pointer-based arrays in Chapter 7 to prepare studentsfor working with C and C++ legacy code in industry and touse as a basis for building our own customized Array class inChapter 11, Operating Overloading

throughout the book However, we begin by discussing C-Tuned Treatment of Inheritance and Polymorphism.

Chapters 1213 have been carefully tuned, making the

treatment of inheritance and polymorphism clearer andmore accessible for students who are new to OOP An

Employee hierarchy replaces the Point/Circle/Cylinder hierarchyused in prior editions to introduce inheritance and

polymorphism The new hierarchy is more natural

Discussion and Illustration of How Polymorphism

Works "Under the Hood." Chapter 13 contains a detaileddiagram and explanation of how C++ can implement

Trang 37

internally This gives students a solid understanding of howthese capabilities really work More importantly, it helpsstudents appreciate the overhead of polymorphismin terms

of additional memory consumption and processor time Thishelps students determine when to use polymorphism andwhen to avoid it

Web Programming Chapter 19, Web Programming, haseverything readers need to begin developing their own

Web-based applications that will run on the Internet!

Students will learn how to build so-called n-tier applications,

in which the functionality provided by each tier can be

distributed to separate computers across the Internet orexecuted on the same computer Using the popular ApacheHTTP server (which is available free for download from

Interface) protocol and discuss how CGI allows a Web

server to communicate with the top tier (e.g., a Web

browser running on the user's computer) and CGI scripts(i.e., our C++ programs) executing on a remote system.The chapter examples conclude with an e-business casestudy of an online bookstore that allows users to add books

to an electronic shopping cart

Standard Template Library (STL) This might be one of

the most important topic in the book in terms of your

appreciation of software reuse The STL defines powerful,template-based, reusable components that implement manycommon data structures and algorithms used to processthose data structures Chapter 23 introduces the STL anddiscusses its three key componentscontainers, iterators andalgorithms We show that using STL components providestremendous expressive power and can reduce many lines ofcode to a single statement

Trang 38

XHTML The World Wide Web Consortium (W3C) has

declared HyperText Markup Language (HTML) to be a legacytechnology that will undergo no further development HTML

is being replaced by the Extensible HyperText Markup

Language (XHTML)an XML-based technology that rapidly isbecoming the standard for describing Web content We useXHTML in Chapter 19, Web Programming; Appendix J andAppendix K introduce XHTML

ANSI/ISO C++ Standard Compliance We have audited

our presentation against the most recent ANSI/ISO C++

standard document for completeness and accuracy [Note:

If you need additional technical details on C++, you maywant to read the C++ standard document An electronicPDF copy of the C++ standard document, number

INCITS/ISO/IEC 14882-2003, is available for $18 at

webstore.ansi.org/ansidocstore/default.asp.]

New Debugger Appendices We include two new Using

the Debugger appendicesAppendix L, Using the Visual

Studio NET Debugger, and Appendix M, Using the GNU

occurrence of each key term both in the text and in the

index in blue, bold style text for easier reference We

emphasize on-screen components in the bold Helvetica font (e.g., the File menu) and emphasize C++ program text in

the Lucida font (e.g., int x= 5)

Trang 39

is consistent with most C++ integrated development

environments and code editors This greatly improves codereadabilityan especially important goal, given that this bookcontains 17,292 lines of code Our syntax-coloring

"Code washing." This is our term for using extensive and

meaningful comments, using meaningful identifiers,

applying uniform indentation conventions, aligning curlybraces vertically, using a // end comment on every linewith a right curly brace and using vertical spacing to

highlight significant program units such as control

statements and functions This process results in programsthat are easy to read and self-documenting We have

extensively "code washed" all of the source-code programs

in both the text and the book's ancillaries We have workedhard to make our code exemplary

[Page xxvii]

Code Testing on Multiple Platforms We tested the code

examples on various popular C++ platforms For the mostpart, all of the book's examples port easily to all popularANSI/ISO standard-compliant compilers We will post any

Trang 40

Errors and Warnings Shown for Multiple Platforms.

For programs that intentionally contain errors to illustrate akey concept, we show the error messages that result onseveral popular platforms

classes

Free Lab Manual We've converted our Lab Manual, C++

in the Lab, from a for-sale softcover book to a free online supplement included with the Cyber Classroom, available

with new books purchased from Prentice Hall for fall 2005classes

As you read this book, if you have questions, send an e-mail to

deitel&deitel.com; we will respond promptly Please visit our Website, www.deitel.com and be sure to sign up for the free

DEITEL® Buzz Online e-mail newsletter at

www.deitel.com/newsletter/subscribe.html for updates to this

Ngày đăng: 25/03/2019, 16:04

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN