Practical C++ Programming

Similar documents
Practical C Programming. Steve Oualline

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Absolute C++ Walter Savitch

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Short Notes of CS201

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CS201 - Introduction to Programming Glossary By

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Contents. Preface. Introduction. Introduction to C Programming

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Problem Solving with C++

C Programming for Electronic Engineers

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.


Contents. 2 Introduction to C++ Programming,

The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Tokens, Expressions and Control Structures

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Mastering BorlandC++ 4.5

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

SOME ASSEMBLY REQUIRED

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Computer Science 306 Study Guide

Computer Programming C++ (wg) CCOs

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Chapter 15 - C++ As A "Better C"

Borland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126

CS3157: Advanced Programming. Outline

C++ (Non for C Programmer) (BT307) 40 Hours

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

CS201 Latest Solved MCQs

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

CS201 Some Important Definitions

Introduction to Programming

CERTIFICATE IN WEB PROGRAMMING

ENGINEERING PROBLEM SOLVING WITH C++

Contents. 1 Introduction to Computers, the Internet and the World Wide Web 1. 2 Introduction to C Programming 26

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Introduction to C++ Systems Programming

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

THINK LIKE CREATIVE PROBLEM SOLVING V. ANTON SPRAUL

Axivion Bauhaus Suite Technical Factsheet MISRA

Variables. Data Types.

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

MPATE-GE 2618: C Programming for Music Technology. Syllabus

C-LANGUAGE CURRICULAM

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

C for Electronic Engineering

Interview Questions of C++


Structure of this course. C and C++ Past Exam Questions. Text books

Appendices E through H are PDF documents posted online at the book s Companion Website (located at

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Table of Contents Preface Bare Necessities... 17

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

C CODING STANDARDS AND PRACTICES ENTC 489 EMBEDDED REAL TIME SOFTWARE DEVELOPMENT REVISION 1.0 CODESTD.DOC

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Programming. Principles and Practice Using C++ Bjarne Stroustrup. / Addison-Wesley. Second Edition


The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Programming in C and C++

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

C Programming Language (Chapter 2 of K&R) Variables and Constants

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Deitel Series Page How To Program Series

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

IBM i Version 7.2. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

Introduction to Programming Using Java (98-388)

Review of the C Programming Language for Principles of Operating Systems

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

WA1278 Introduction to Java Using Eclipse

Overview : Table Of Content :

IBM i Version 7.3. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

Programming in C++ 4. The lexical basis of C++

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

September 10,

Index COPYRIGHTED MATERIAL

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

A Fast Review of C Essentials Part I

AN OVERVIEW OF C++ 1

IS 0020 Program Design and Software Tools

Model Viva Questions for Programming in C lab

APPENDIX A : Example Standard <--Prev page Next page -->

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

SAURASHTRA UNIVERSITY

CHAPTER 1: INTRODUCING C# 3

For Teacher's Use Only Q No Total Q No Q No

Transcription:

SECOND EDITION Practical C++ Programming Steve Oualline O'REILLY' Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo

Preface xv Part I. The Basics 1. What Is C++? 3 A Brief History of C++ 3 C++ Organization 4 How to Learn C++ 6 2. The Basics of Program Writing 7 Programs from Conception to Execution 9 Creating a Real Program 10 Getting Help in Unix 22 Getting Help in an IDE 22 22 3. Style 23 Comments 24 C++ Code 28 Naming Style 30 Coding Religion 31 Indentation and Code Format 31 Clarity 32 Simplicity 33 Consistency and Organization 34 Further Reading 34 Summary 34

4. Basic Declarations and Expressions 35 Basic Program Structure 36 Simple Expressions 37 Thestd::cout Output Object 38 Variables and Storage 39 Variable Declarations 40 Integers 40 Assignment Statements 41 Floating-Point Numbers 42 Floating-Point Divide Versus Integer Divide 43 Characters 44 Wide Characters 46 Boolean Type 46 46 47 5. Arrays, Qualifiers, and Reading Numbers 48 Arrays 48 Strings 49 Reading Data 51 Initializing Variables 53 Multidimensional Arrays 57 C-Style Strings 58 Types of Integers 64 Types of Floats 67 Constant and Reference Declarations 67 Qualifiers 68 Hexadecimal and Octal Constants 70 Operators for Performing Shortcuts 71 Side Effects 72 74 75 i 6. Decision and Control Statements 76 if Statement 76 else Statement 77 How Not to Usestd::strcmp 78 Looping Statements 79 while Statement 79

break Statement 81 continue Statement 82 The Assignment Anywhere Side Effect 83 84 85 7. The Programming Process 87 Setting Up Your Work Area 89 The Specification 90 Code Design 91 The Prototype 92 The Makefile 93 Testing 96 Debugging 96 Maintenance 98 Revisions 99 Electronic Archaeology 99 Mark Up the Program 100 Use the Debugger 100 Use the Text Editor as a Browser 100 Add Comments 101 103 Part II. Simple Programming 8. More Control Statements 107 for Statement 107 switch Statement 110 switch, break, and continue 115 115 117 9. Variable Scope and Functions 118 Scope and Storage Class 118 Namespaces 122 Functions 125 Summary of Parameter Types 138 Recursion 139 vii

Structured Programming Basics 141 Real-World Programming 142 142 143 10. The C++ Preprocessor 145 #define Statement 145 Conditional Compilation 150 #include Files 152 Parameterized Macros 154 Advanced Features 156 Summary 156 157 157 11. Bit Operations 160 Bit Operators 161 The AND Operator (&) 161 Bitwise OR ( ) 163 The Bitwise Exclusive OR ( A ) 164 The Ones Complement Operator (NOT) (~) 164 The Left and Right Shift Operators («,») 165 Setting, Clearing, and Testing Bits 166 Bitmapped Graphics 169 174 175 Part III. Advanced Types and Classes 12. Advanced Types 179 Structures 179 Unions 181 typedef 184 enum Type 185 Bit Members or Packed Structures 186 Arrays of Structures 188 189 189

13. Simple Classes Stacks Improved Stack Using a Class Introduction to Constructors and Destructors Automatically Generated Member Functions Shortcuts Style Structures Versus Classes 14. More on Classes Friends Constant Functions Constant Members Static Member Variables Static Member Functions The Meaning of static 15. Simple Pointers const Pointers Pointers and Printing Pointers and Arrays The reinterpret_cast Pointers and Structures Command-Line Arguments 191 191 195 197 199 204 205 206 208 209 211 211 213 215 216 218 218 219 221 226 226 227 234 234 235 239 240 Part IV. Advanced Programming Concepts 16. File Input/Output C++ File I/O Conversion Routines Binary and ASCII Files The End-of-Line Puzzle Binary I/O 245 245 250 254 255 256

Buffering Problems Unbuffered I/O Designing File Formats C-Style I/O Routines C-Style Conversion Routines C-Style Binary I/O C- Versus C++- Style I/O 17. Debugging and Optimization Code Reviews Serial Debugging Going Through the Output Interactive Debuggers Debugging a Binary Search Interactive Debugging Tips and Tricks Runtime Errors Optimization How to Optimize Case Study: Inline Functions Versus Normal Functions Case Study: Optimizing a Color-Rendering Algorithm 18. Operator Overloading Creating a Simple Fixed-Point Class Operator Functions Operator Member Functions Warts Full Definition of the Fixed-Point Class 19. Floating Point Floating-Point Format Floating Addition/Subtraction Multiplication and Division Overflow and Underflow 257 258 262 264 266 270 272 274 275 277 277 280 282 283 287 298 299 301 306 308 308 309 309 310 310 315 324 326 326 335 336. 337 337 338 339 340

Roundoff Error Accuracy Minimizing Roundoff Error Determining Accuracy Precision and Speed Power Series 20. Advanced Pointers Pointers, Structures, and Classes delete Operator Linked Lists Ordered Linked Lists Doubly Linked Lists Trees Printing a Tree The Rest of the Program Data Structures for a Chess Program 21. Advanced Classes Derived Classes Virtual Functions Virtual Classes Function Hiding in Derived Classes Constructors and Destructors in Derived Classes The dynamic_cast Operator Summary 340 341 342 342 343 344 346 347 348 350 351 354 357 360 364 364 367 369 369 371 371 378 383 385 385 388 389 389 390 Part V. Other Language Features 22. Exceptions Adding Exceptions to the Stack Class Exceptions Versus assert 393 394 400 400

23. Modular Programming 401 Modules 401 Public and Private 402 The extern Storage Class 402 Headers 404 The Body of the Module 406 A Program to Use Infinite Arrays 406 The Makefile for Multiple Files 408 Using the Infinite Array 412 Dividing a Task into Modules 417 Module Design Guidelines 417 418 24. Templates 419 What Is a Template? 419 Templates: The Hard Way 419 Templates: The C++ Way 420 Function Specialization 423 Class Templates 424 Class Specialization 426 Implementation Details 426 Advanced Features 429 Summary 431 431 25. Standard Template Library 432 STL Basics 432 Class List A Set of Students 434 Creating a Waiting List with the STL List 436 Storing Grades in a STL Map 437 Putting It All Together 437 Practical Considerations When Using the STL 445 Getting More Information 446 Exercises 446 26. Program Design 448 Design Goals 448 Design Factors 449 Design Principles 450 xii

27. 28. 29. Coding Objects Real-World Design Techniques Conclusion Putting It All Together. Requirements Code Design Coding Functional Description Testing Revisions A Final Warning Program Files FromCtoC++ K&R-Style Functions struct malloc and free Turning Structures into Classes setjmp and longjmp Mixing C and C++ Code Summary Programming Exercise C++'s Dustier Corners do/while goto The?: Construct The Comma Operator Overloading the () Operator Pointers to Members The asm Statement The mutable Qualifier Run Time Type Identification Trigraphs 451 457 461 467 469 469 471 472 472 477 477 477 477 496 497 497 498 498 500 501 503 503 503 504 504 504 506 506 507 507 508 508 509 509 509 xiii

30. Programming Adages 510 General 510 Design 511 Declarations 511 switch Statement 511 Preprocessor 511 Style 512 Compiling 512 The Ten Commandments for C++ Programmers 512 Final Note 513 514 Part VI. Appendixes A. ASCII Table 517 B. Ranges 519 C. Operator Precedence Rules 521 D. Computing Sine Using a Power Series 523 E. Resources 529 Index 531 xiv