Đây là quyển sách tiếng anh về lĩnh vực công nghệ thông tin cho sinh viên và những ai có đam mê. Quyển sách này trình về lý thuyết ,phương pháp lập trình cho ngôn ngữ C và C++.
Practical C++ Programming Steve Oualline O'Reilly & Associates, Inc. Beijing · Cambridge · Köln · Paris · Sebastopol · Taipei · Tokyo Page iv Practical C++ Programming by Steve Oualline Copyright © 1995 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Editors: Adrian Nye and Dale Dougherty Production Editor: Nicole Gipson Printing History: August 1995 First Edition. January 1997: Minor corrections. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks and The Java Series is a trademark of O'Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book is printed on acid-free paper with 85% recycled content, 15% post-consumer waste. O'Reilly & Associates is committed to using paper with the highest recycled content available consistent with high quality. ISBN. 1-56592-139-9 [12/98] Page v Table of Contents Preface xv I: The Basics 1 1: What Is C++? 3 3 A Brief History of C++ 3 C++ Organization 4 How to Learn C++ 6 2: The Basics of Program Writing 9 Programs from Conception to Execution 12 Creating a Real Program 13 Creating a Program Using a Command-Line Compiler 13 Creating a Program Using an Integrated Development Environment 16 Getting Help in UNIX 32 Getting Help in an Integrated Development Environment 33 Programming Exercises 33 3: Style 35 Comments 36 C++ Code 4 41 Naming Style 42 Coding Religion 43 Indentation and Code Format 43 Page vi Clarity 44 44 Simplicity 45 Consistency and Organization 46 Further Reading 46 Summary 46 4: Basic Declarations and Expressions 49 The Elements of a Program 49 Basic Program Structure 50 Simple Expressions 51 The cout Output Class 53 Variables and Storage 53 Variable Declarations 54 Integers 55 Assignment Statements 56 Floating Point Numbers 57 Floating Point Versus Integer Divide 58 Characters 59 Programming Exercises 60 Answers Chapter Questions 61 5: Arrays, Qualifiers, and Reading Numbers 63 Arrays 63 Strings 64 Reading Data 67 Initializing Variables 69 Multidimensional Arrays 70 Types of Integers 72 Types of Floats 74 74 Constant and Reference Declarations 74 Qualifiers 76 Hexadecimal and Octal Constants 78 Operators for Performing Shortcuts 78 Side Effects 79 Programming Exercises 82 Answers to Chapter Questions 82 Page vii 6: Decision and Control Statements 85 if Statement 85 else Statement 87 How Not to Use strcmp 88 Looping Statements 88 while Statement 88 Break Statement 91 continue Statement 92 The Assignment Anywhere Side Effect 92 Programming Exercises 94 Answers to Chapter Questions 95 7. The Programming Process 97 Setting Up 99 The Specification 100 Code Design 101 The Prototype 102 The Makefile 103 103 Testing 105 Debugging 106 Maintenance 108 Revisions 108 Electronic Archaeology 109 Mark Up the Program 109 Use the Debugger 110 Use the Text Editor as a Browser 110 Add Comments 110 Programming Exercises 113 II: Simple Programming 115 8: More Control Statements 117 for Statement 117 switch Statement 120 switch, break, and continue 125 Programming Exercises 127 Answers to Chapter Questions 128 Page vii 9: Variable Scope and Functions 129 Scope and Storage Class 129 Functions 133 Summary of Parameter Types 146 Structured Programming Basics 146 Recursion 148 148 Programming Exercises 149 Answers to Chapter Questions 149 10. The C++ Preprocessor 151 #define Statement 151 Conditional Compilation 157 #include Files 159 Parameterized Macros 160 Advanced Features 162 Summary 163 Programming Exercises 163 Answers to Chapter Questions 164 11: Bit Operations 167 Bit Operators 168 The AND Operator (&) 168 Bitwise OR ( | ) 171 The Bitwise Exclusive OR (^) 171 The Ones Complement Operator (NOT) (-) 171 The Left and Right Shift Operators (<<, >>) 172 Setting, Clearing, and Testing Bits 173 Bitmapped Graphics 176 Programming Exercises 181 Answers to Chapter Questions 182 III: Advanced Types and Classes 183 12: Advanced Types 185 Structures 185 185 Unions 188 typedef 190 Page ix enum Type 191 Bit Fields or Packed Structures 193 Arrays of Structures 195 Programming Exercises 196 13: Simple Classes 197 Stacks 197 Improved Stack 201 Using a Class 203 Introduction to Constructors and Destructors 205 Automatically Generated Member Functions 210 Shortcuts 211 Style 212 Programming Exercises 214 14: More on Classes 217 Friends 217 Constant Functions 219 Constant Members 220 Static Member Variables 222 Static Member Functions 223 The Meaning of static 224 Programming Exercises 225 225 15: Simple Pointers 227 Constant Pointers 232 Pointers and Printing 233 Pointers and Arrays 233 Splitting Strings 237 Pointers and Structures 240 Command-Line Arguments 241 Programming Exercises 245 Answers to Chapter Questions 245 Page x IV: Advanced Programming Concepts 249 16: File Input/Output 251 C++ File I/O 252 Conversion Routines 256 Binary and ASCII Files 260 The End-of-Line Puzzle 261 Binary I/O 262 Buffering Problems 263 Unbuffered I/O 264 Designing File Formats 268 C-Style I/O Routines 270 C-Style Conversion Routines 273 C-Style Binary I/O 276 Programming Exercises 278 278 Answers to Chapter Questions 278 17: Debugging and Optimization 281 Debugging 281 Serial Debugging 289 Divide and Conquer 290 Debug-Only Code 290 Debug Command-Line Switch 290 Going Through the Output 292 Interactive Debuggers 292 Debugging a Binary Search 296 Runtime Errors 307 The Confessional Method of Debugging 309 Optimization 309 The Power of Powers of 2 311 How to Optimize 314 Case Study: Inline Functions Versus Normal Functions 316 Case Study: Optimizing a Color-Rendering Algorithm 316 Programming Exercises 317 Answers to Chapter Questions 317 Page xi 18: Operator Overloading 319 Operator Functions 322 Operator Member Functions 330 Full Definition of the Complex Class 332 332 Programming Exercises 341 Answers to Chapter Questions 342 19 : Floating Point 343 Floating-Point Format 343 Floating Addition/Subtraction 344 Multiplication 345 Division 346 Overflow and Underflow 346 Roundoff Error 347 Accuracy 347 Minimizing Roundoff Error 348 Determining Accuracy 348 Precision and Speed 350 Power Series 351 Programming Exercises 352 20: Advanced Pointers 355 Pointers, Structures, and Classes 355 delete Operator 358 Linked List 359 Ordered Linked Lists 362 Double-linked List 365 Trees 368 Printing a Tree 373 The Rest of the Program 373 Data Structures for a Chess Program 377 Programming Exercises 378 [...]... hiding and modular programming techniques, as well as object-oriented programming, are stressed Chapter 27, From C to C++, describes how to turn C code into C++ code, and addresses many of the traps lurking in C code that bite the C++ programmer Chapter 28, C++' s Dustier Corners, describes the do/while statement, the comma operator, and the ?: operators Chapter 29, Programming Adages, lists programming adages... That's where C++ comes in C++ is a high-level programming language that allows a software engineer to efficiently communicate with a computer C++ is a highly flexible and adaptable language Since its creation in 1980, it has been used for a wide variety of programs including firmware for micro-controllers, operating systems, applications, and graphics programming C++ is quickly becoming the programming. .. Borland C++ in MS-DOS and Windows In addition to Turbo -C++, Borland International also makes a full-featured, professional compiler for MS-DOS/Windows called Borland C++ Its command line is: C:> bcc -ml -v -N -P -w -ehello hello.cpp The command-line options are the same for both Turbo -C++ and Borland C++ Microsoft Visual C++ Microsoft Visual C++ is another C++ compiler for MS-DOS/Windows It is not as robust... tells Turbo -C++ to put debugging information in the program Warnings are turned on by -w; stack checking by -N The compiler will actually compile both C and C++ We force a C++ compile using the -P switch Finally, -ehello tells Turbo -C++ to create a program named hello, and hello.cpp is the name of the source file See the Turbo -C++ reference manual for a complete list of options Borland C++ in MS-DOS... located at the end of this chapter.) Several dialects of C++ are presented: • A "generic" UNIX compiler that should work on most UNIX systems • The GNU C++ compiler, named g++ (available for most UNIX systems *) • Borland's Turbo C++ compiler for MS-DOS • Borland C++ for MS-DOS/Windows • Microsoft's Visual C++ for MS-DOS/Windows As far as standard C++ is concerned there are only minor differences among... -Wall -ohello hello.cc The additional switch -Wall turns on all the warnings Borland's Turbo C++ in MS-DOS Borland International makes a low-cost MS-DOS C++ compiler called Turbo -C++ This compiler is ideal for learning The command line for Turbo -C++ is: C:> tcc -ml -v -N -P -w -ehello hello.cpp The -ml tells Turbo -C++ to use the large memory model (This PC has a large number of different memory models... enough to create very simple programs; therefore, in Chapter 7, The Programming Process, you embark on a complete tour of the programming process that shows you how real programs are created Chapter 1, What Is C++? , gives you an overvie ins the basic programming process and gives you enough information to write a very simple program.w of C++, describes its history and uses, and explains how the language... finally became C++ C++ owes its success to the fact that it allows the programmer to organize and process information more effectively than most other languages Also, it builds on the work already done with the C language In fact, most C programs can be transformed into C++ programs with little trouble These programs usually don't use all the new features of C++, but they do work In this way, C++ allows... design implementor Finally, special thanks go to all the hard-working programmers out there whose code has taught me so much Page 1 I The Basics Page 3 1 What Is C++? In This Chapter: • A Brief Histoty of C++ • C++ Organization • How to Learn C++ Profanity is the one language that all programmers understand —Anonymous The ability to organize and process information is the key to success in the modern... Description 459 Testing 463 Revisions 464 A Final Warning 464 Program Files 464 Programming Exercises 483 From C to C++ 485 Overview 485 K&R-Style Functions 485 struct 486 malloc and free 486 Turning Structures into Classes 488 ssetjmp and longjmp 489 Summary 491 Programming Exercise 27: 453 Requirements 26: 452 491 Page xiv 28 C++' s Dustier Corners 493 do/while 493 493 goto 493 The ?:Construct 495 The . Borland's Turbo C++ compiler for MS-DOS • Borland C++ for MS-DOS/Windows • Microsoft's Visual C++ for MS-DOS/Windows As far as standard C++ is concerned. quality. ISBN. 1-5 659 2-1 3 9-9 [12/98] Page v Table of Contents Preface xv I: The Basics 1 1: What Is C++? 3 3 A Brief History of C++ 3 C++ Organization