Fundamentals of c ++ programing

647 113 0
Fundamentals of c ++ programing

Đ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

Fundamentals of C++ Programming T F A DR Richard L Halterman School of Computing Southern Adventist University September 12, 2016 Copyright © 2008–2016 Richard L Halterman All rights reserved i Contents The Context of Software Development 1.1 Software 1.2 Development Tools 1.3 Learning Programming with C++ 1.4 Summary 1.5 Exercises Writing a C++ Program 2.1 General Structure of a Simple C++ Program 2.2 Editing, Compiling, and Running the Program 11 2.3 Variations of our simple program 11 2.4 Template for simple C++ programs 14 2.5 Summary 15 2.6 Exercises 16 Values and Variables 17 3.1 Integer Values 17 3.2 Variables and Assignment 19 3.3 Identifiers 23 3.4 Additional Integer Types 25 3.5 Floating-point Types 26 3.6 Constants 28 3.7 Other Numeric Types 29 3.8 Characters 29 3.9 Enumerated Types 32 3.10 Type Inference with auto 33 ©2016 Richard L Halterman Draft date: September 12, 2016 ii CONTENTS 3.11 Summary 34 3.12 Exercises 35 Expressions and Arithmetic 39 4.1 Expressions 39 4.2 Mixed Type Expressions 43 4.3 Operator Precedence and Associativity 47 4.4 Comments 49 4.5 Formatting 50 4.6 Errors and Warnings 53 4.6.1 Compile-time Errors 53 4.6.2 Run-time Errors 54 4.6.3 Logic Errors 55 4.6.4 Compiler Warnings 56 4.7 Arithmetic Examples 58 4.8 Integers vs Floating-point Numbers 61 4.8.1 Integer Implementation 63 4.8.2 Floating-point Implementation 67 More Arithmetic Operators 72 4.10 Bitwise Operators 75 4.11 Algorithms 80 4.12 Summary 81 4.13 Exercises 84 Conditional Execution 91 5.1 Type bool 91 5.2 Boolean Expressions 92 5.3 The Simple if Statement 94 5.4 Compound Statements 97 5.5 The if/else Statement 98 5.6 Compound Boolean Expressions 102 5.7 Nested Conditionals 106 5.8 Multi-way if/else Statements 117 5.9 Errors in Conditional Statements 122 4.9 5.10 Summary 124 ©2016 Richard L Halterman Draft date: September 12, 2016 iii CONTENTS 5.11 Exercises 125 Iteration 129 6.1 The while Statement 129 6.2 Nested Loops 139 6.3 Abnormal Loop Termination 147 6.3.1 The break statement 147 6.3.2 The goto Statement 148 6.3.3 The continue Statement 150 6.4 Infinite Loops 152 6.5 Iteration Examples 155 6.5.1 Drawing a Tree 156 6.5.2 Printing Prime Numbers 158 6.6 Summary 161 6.7 Exercises 162 Other Conditional and Iterative Statements 167 7.1 The switch Statement 167 7.2 The Conditional Operator 172 7.3 The do/while Statement 173 7.4 The for Statement 175 7.5 Summary 182 7.6 Exercises 183 Using Functions 187 8.1 Introduction to Using Functions 189 8.2 Standard Math Functions 194 8.3 Maximum and Minimum 198 8.4 clock Function 199 8.5 Character Functions 200 8.6 Random Numbers 202 8.7 Summary 205 8.8 Exercises 206 Writing Functions 9.1 209 Function Basics 210 ©2016 Richard L Halterman Draft date: September 12, 2016 iv CONTENTS 9.2 Using Functions 219 9.3 Pass by Value 225 9.4 Function Examples 226 9.4.1 Better Organized Prime Generator 226 9.4.2 Command Interpreter 228 9.4.3 Restricted Input 229 9.4.4 Better Die Rolling Simulator 231 9.4.5 Tree Drawing Function 233 9.4.6 Floating-point Equality 234 9.4.7 Multiplication Table with Functions 236 9.5 Commenting Functions 239 9.6 Custom Functions vs Standard Functions 241 9.7 Summary 243 9.8 Exercises 244 10 Managing Functions and Data 249 10.1 Global Variables 249 10.2 Static Variables 257 10.3 Overloaded Functions 260 10.4 Recursion 260 10.5 Making Functions Reusable 266 10.6 Pointers 271 10.7 Reference Variables 276 10.8 Pass by Reference 278 10.8.1 Pass by Reference via Pointers 279 10.8.2 Pass by Reference via References 281 10.9 Higher-order Functions 282 10.10Summary 285 10.11Exercises 286 11 Aggregate Data 295 11.1 Vectors 297 11.1.1 Declaring and Using Vectors 297 11.1.2 Traversing a Vector 301 11.1.3 Vector Methods 306 ©2016 Richard L Halterman Draft date: September 12, 2016 CONTENTS v 11.1.4 Vectors and Functions 308 11.1.5 Multidimensional Vectors 313 11.2 Arrays 317 11.3 Vectors vs Arrays 322 11.4 Prime Generation with a Vector 324 11.5 Summary 327 11.6 Exercises 329 12 Sorting and Searching 333 12.1 Sorting 333 12.2 Flexible Sorting 336 12.3 Search 338 12.3.1 Linear Search 338 12.3.2 Binary Search 341 12.4 Vector Permutations 352 12.5 Randomly Permuting a Vector 358 12.6 Summary 364 12.7 Exercises 364 13 Standard C++ Classes 367 13.1 String Objects 368 13.2 Input/Output Streams 372 13.3 File Streams 375 13.4 Complex Numbers 379 13.5 Better Pseudorandom Number Generation 380 13.6 Summary 388 13.7 Exercises 389 14 Custom Objects 391 14.1 Object Basics 391 14.2 Fields 393 14.3 Methods 398 14.4 Constructors 405 14.5 Destructors 408 14.6 Defining a New Numeric Type 409 ©2016 Richard L Halterman Draft date: September 12, 2016 CONTENTS vi 14.7 Encapsulation 411 14.8 Summary 414 14.9 Exercises 415 15 Fine Tuning Objects 421 15.1 Pointers to Objects and Object Arrays 421 15.2 The this Pointer 424 15.3 const Methods 426 15.4 Separating Method Declarations and Definitions 428 15.5 Preventing Multiple Inclusion 434 15.6 Overloaded Operators 438 15.6.1 Operator Functions 438 15.6.2 Operator Methods 441 15.7 static Members 442 15.8 Classes vs structs 446 15.9 Summary 447 15.10Exercises 447 16 Building some Useful Classes 449 16.1 A Better Rational Number Class 449 16.2 Stopwatch 451 16.3 Sorting with Logging 457 16.4 Linked Lists 461 16.5 Automating Testing 471 16.6 Convenient High-quality Pseudorandom Numbers 476 16.7 Summary 478 16.8 Exercises 478 17 Inheritance and Polymorphism 481 17.1 I/O Stream Inheritance 481 17.2 Inheritance Mechanics 483 17.3 Uses of Inheritance 486 17.4 Polymorphism 493 17.5 Protected Members 500 17.6 Fine Tuning Inheritance 508 ©2016 Richard L Halterman Draft date: September 12, 2016 CONTENTS vii 17.7 Summary 518 17.8 Exercises 519 18 Generic Programming 521 18.1 Function Templates 521 18.2 Class Templates 532 18.3 Iterators 542 18.4 Iterator Ranges 546 18.5 The Standard Template Library 554 18.6 Summary 554 18.7 Exercises 554 19 Handling Exceptions 555 19.1 Motivation 555 19.2 Exception Examples 556 19.3 Custom Exceptions 564 19.4 Catching Multiple Exceptions 566 19.5 Exception Mechanics 569 19.6 Using Exceptions 572 19.7 Summary 574 19.8 Exercises 574 20 Concurrent Programming 575 20.1 Motivation 575 20.2 Summary 575 20.3 Exercises 575 Appendices 579 A Using Visual Studio 2013 to Develop C++ Programs 579 B Visual C++ Command Line Development 585 C Developing C++ Programs with the GNU Tools 589 D Arrays 593 D.1 Declaring and Using Arrays 593 D.2 Arrays and Functions 601 ©2016 Richard L Halterman Draft date: September 12, 2016 CONTENTS viii D.3 Prime Generation with an Array 604 D.4 Multidimensional Arrays 605 D.5 Pointers and Arrays 608 D.6 Array Ranges 612 D.7 C Strings 613 D.8 Dynamic Arrays 617 D.9 The sizeof Operator 621 D.10 Copying an Array 623 D.11 Memory Management 624 D.12 Summary 629 D.13 Exercises 630 Bibliography 633 Index 634 ©2016 Richard L Halterman Draft date: September 12, 2016 622 D.9 THE SIZEOF OPERATOR on a system in which integers use four bytes of storage This is because list holds 100 integers at four bytes each, and set holds five integers at four bytes each The sizeof operator computes the storage required for any type; for example, cout

Ngày đăng: 01/06/2018, 15:01

Từ khóa liên quan

Mục lục

  • The Context of Software Development

    • Software

    • Development Tools

    • Learning Programming with C++

    • Summary

    • Exercises

    • Writing a C++ Program

      • General Structure of a Simple C++ Program

      • Editing, Compiling, and Running the Program

      • Variations of our simple program

      • Template for simple C++ programs

      • Summary

      • Exercises

      • Values and Variables

        • Integer Values

        • Variables and Assignment

        • Identifiers

        • Additional Integer Types

        • Floating-point Types

        • Constants

        • Other Numeric Types

        • Characters

        • Enumerated Types

Tài liệu cùng người dùng

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

Tài liệu liên quan