1. Trang chủ
  2. » Công Nghệ Thông Tin

Ebook Fundamentals of C++ programming Part 1

387 302 0

Đ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 387
Dung lượng 5,14 MB

Nội dung

(BQ) Part 1 book Fundamentals of C++ programming has contents The context of software development, writing a C++ program, values and variables, expressions and arithmetic, conditional execution, iteration, other conditional and iterative statements, using functions,... and other contents.

Fundamentals of C++ Programming T F A DR Richard L Halterman School of Computing Southern Adventist University January 30, 2017 Copyright © 2008–2017 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 Exercises Writing a C++ Program 2.1 General Structure of a Simple C++ Program 2.2 Editing, Compiling, and Running the Program 2.3 Variations of our simple program 2.4 Template for simple C++ programs 12 2.5 Exercises 13 Values and Variables 15 3.1 Integer Values 15 3.2 Variables and Assignment 18 3.3 Identifiers 21 3.4 Additional Integer Types 24 3.5 Floating-point Types 25 3.6 Constants 27 3.7 Other Numeric Types 28 3.8 Characters 28 3.9 Enumerated Types 31 3.10 Type Inference with auto 32 3.11 Exercises 32 Expressions and Arithmetic 35 ©2017 Richard L Halterman Draft date: January 30, 2017 ii CONTENTS 4.1 Expressions 35 4.2 Mixed Type Expressions 39 4.3 Operator Precedence and Associativity 42 4.4 Comments 44 4.5 Formatting 45 4.6 Errors and Warnings 48 4.6.1 Compile-time Errors 48 4.6.2 Run-time Errors 49 4.6.3 Logic Errors 50 4.6.4 Compiler Warnings 51 4.7 Arithmetic Examples 53 4.8 Integers vs Floating-point Numbers 56 4.8.1 Integer Implementation 57 4.8.2 Floating-point Implementation 62 More Arithmetic Operators 67 4.10 Bitwise Operators 70 4.11 Algorithms 74 4.12 Exercises 76 Conditional Execution 83 5.1 Type bool 83 5.2 Boolean Expressions 84 5.3 The Simple if Statement 86 5.4 Compound Statements 89 5.5 The if/else Statement 91 5.6 Compound Boolean Expressions 95 5.7 Nested Conditionals 98 5.8 Multi-way if/else Statements 109 5.9 Errors in Conditional Statements 114 4.9 5.10 Exercises 115 Iteration 121 6.1 The while Statement 121 6.2 Nested Loops 131 6.3 Abnormal Loop Termination 138 ©2017 Richard L Halterman Draft date: January 30, 2017 iii CONTENTS The break statement 138 6.3.2 The goto Statement 139 6.3.3 The continue Statement 141 6.4 Infinite Loops 142 6.5 Iteration Examples 146 6.6 6.3.1 6.5.1 Drawing a Tree 146 6.5.2 Printing Prime Numbers 148 Exercises 151 Other Conditional and Iterative Statements 157 7.1 The switch Statement 157 7.2 The Conditional Operator 162 7.3 The do/while Statement 163 7.4 The for Statement 165 7.5 Exercises 171 Using Functions 177 8.1 Introduction to Using Functions 179 8.2 Standard Math Functions 184 8.3 Maximum and Minimum 188 8.4 clock Function 189 8.5 Character Functions 190 8.6 Random Numbers 191 8.7 Exercises 195 Writing Functions 199 9.1 Function Basics 200 9.2 Using Functions 208 9.3 Pass by Value 213 9.4 Function Examples 215 9.4.1 Better Organized Prime Generator 215 9.4.2 Command Interpreter 217 9.4.3 Restricted Input 218 9.4.4 Better Die Rolling Simulator 220 9.4.5 Tree Drawing Function 221 ©2017 Richard L Halterman Draft date: January 30, 2017 iv CONTENTS 9.4.6 Floating-point Equality 222 9.4.7 Multiplication Table with Functions 225 9.5 Commenting Functions 228 9.6 Custom Functions vs Standard Functions 229 9.7 Exercises 231 10 Managing Functions and Data 237 10.1 Global Variables 237 10.2 Static Variables 245 10.3 Overloaded Functions 247 10.4 Default Arguments 248 10.5 Recursion 250 10.6 Making Functions Reusable 256 10.7 Pointers 262 10.8 Reference Variables 267 10.9 Pass by Reference 269 10.9.1 Pass by Reference via Pointers 270 10.9.2 Pass by Reference via References 272 10.10Higher-order Functions 273 10.11Exercises 276 11 Sequences 283 11.1 Vectors 285 11.1.1 Declaring and Using Vectors 285 11.1.2 Traversing a Vector 289 11.1.3 Vector Methods 293 11.1.4 Vectors and Functions 296 11.1.5 Multidimensional Vectors 301 11.2 Arrays 305 11.2.1 Static Arrays 305 11.2.2 Pointers and Arrays 310 11.2.3 Dynamic Arrays 316 11.2.4 Copying an Array 320 11.2.5 Multidimensional Arrays 324 11.2.6 C Strings 327 ©2017 Richard L Halterman Draft date: January 30, 2017 CONTENTS v 11.2.7 Command-line Arguments 330 11.3 Vectors vs Arrays 332 11.4 Prime Generation with a Vector 335 11.5 Exercises 338 12 Sorting and Searching 345 12.1 Sorting 345 12.2 Flexible Sorting 348 12.3 Search 350 12.3.1 Linear Search 350 12.3.2 Binary Search 353 12.4 Vector Permutations 363 12.5 Randomly Permuting a Vector 369 12.6 Exercises 375 13 Standard C++ Classes 377 13.1 String Objects 378 13.2 Input/Output Streams 382 13.3 File Streams 385 13.4 Complex Numbers 391 13.5 Better Pseudorandom Number Generation 392 14 Custom Objects 401 14.1 Object Basics 401 14.2 Instance Variables 403 14.3 Member Functions 408 14.4 Constructors 415 14.5 Defining a New Numeric Type 418 14.6 Encapsulation 421 14.7 Exercises 423 15 Fine Tuning Objects 429 15.1 Passing Object Parameters 429 15.2 Pointers to Objects and Object Arrays 431 15.3 The this Pointer 434 15.4 const Methods 436 ©2017 Richard L Halterman Draft date: January 30, 2017 CONTENTS vi 15.5 Separating Method Declarations and Definitions 438 15.6 Preventing Multiple Inclusion 444 15.7 Overloaded Operators 448 15.7.1 Operator Functions 448 15.7.2 Operator Methods 451 15.8 static Members 452 15.9 Classes vs structs 456 15.10Friends 457 15.11Exercises 461 16 Building some Useful Classes 463 16.1 A Better Rational Number Class 463 16.2 Stopwatch 465 16.3 Sorting with Logging 471 16.4 Automating Testing 475 16.5 Convenient High-quality Pseudorandom Numbers 479 17 Inheritance and Polymorphism 483 17.1 I/O Stream Inheritance 483 17.2 Inheritance Mechanics 485 17.3 Uses of Inheritance 487 17.4 Polymorphism 495 17.5 Protected Members 501 17.6 Fine Tuning Inheritance 509 17.7 Exercises 518 18 Memory Management 521 18.1 Memory Available to C++ Programs 521 18.2 Manual Memory Management 522 18.3 Linked Lists 527 18.4 Resource Management 536 18.5 Rvalue References 556 18.6 Smart Pointers 567 19 Generic Programming 585 19.1 Function Templates 585 ©2017 Richard L Halterman Draft date: January 30, 2017 vii CONTENTS 19.2 Class Templates 596 19.3 Exercises 609 20 The Standard Template Library 611 20.1 Containers 611 20.2 Iterators 613 20.3 Iterator Ranges 617 20.4 Lambda Functions 627 20.5 Algorithms in the Standard Library 633 20.6 Namespaces 651 21 Associative Containers 659 21.1 Associative Containers 659 21.2 The std::set Data Type 659 21.3 Tuples 665 21.4 The std::map Data Type 668 21.5 The std::unordered_map Data Type 672 21.6 Counting with Associative Containers 674 21.7 Grouping with Associative Containers 678 21.8 Memoization 681 22 Handling Exceptions 689 22.1 Motivation 689 22.2 Exception Examples 690 22.3 Custom Exceptions 698 22.4 Catching Multiple Exceptions 700 22.5 Exception Mechanics 703 22.6 Using Exceptions 706 Appendices 711 A Using Visual Studio 2015 to Develop C++ Programs 711 B Command Line Development 717 B.0.1 Visual Studio Command Line Tools 718 B.0.2 Developing C++ Programs with the GNU Tools 720 ©2017 Richard L Halterman Draft date: January 30, 2017 CONTENTS viii Bibliography 722 Index 723 ©2017 Richard L Halterman Draft date: January 30, 2017 362 12.3 SEARCH Action n = a.size() first = last = n - first seek last = mid - or first = mid + return mid or return -1 Operations =,a.size = =, =,± return Operation Cost 2 2 Iterations 1 log2 n log2 n log2 n log2 n log2 n Total Cost Cost 2 log2 n log2 n log2 n log2 n log2 n 12 log2 n + Table 12.2: Analysis of Binary Search Next, we consider binary search: int binary_search(const std::vector& a, int seek) { int n = a.size(), // Number of elements first = 0, // Initially the first element in vector last = n - 1, // Initially the last element in vector mid; // The middle of the vector while (first seek) last = mid - 1; // continue with 1st half else // a[mid] < seek first = mid + 1; // continue with 2nd half } return -1; // Not there } We determined that in the worst case the loop in binary_search iterates log2 n times if the vector contains n elements The two initializations before the loop are performed once per call Most of the actions within the loop occur log2 n times, except that only one return statement can be executed per call, and in the if/else statement only one path can be chosen per loop iteration Table 12.2 shows the complete analysis of binary search We will call our binary search function B(n) Figure 12.3 shows the plot of the two functions L(n) = 3n + and B(n) = 12 log2 n + For n < 17, the linear function 3n + is less than the binary function 12 log2 n + This means that linear search should perform better than binary search for vector sizes less than 17 This is because the code for linear search is less complicated, and it can complete its work on smaller vectors before the binary search finishes its more sophisticated computations At n = 17, however, the two algorithms should perform about the same because L(17) = 3(17) + = 51 + = 55 ≈ 55.05 = 49.05 + = 12(4.09) + = 12 log2 17 + = B(17) Figure 12.3 shows that for all n > 17 binary search outperforms linear search, and the performance gap increases rapidly as n grows This wide performance discrepancy agrees with our empirical observations ©2017 Richard L Halterman Draft date: January 30, 2017 363 12.4 VECTOR PERMUTATIONS Figure 12.3 A graph of the functions derived from analyzing the linear and binary search routines 250 Time 200 Linear Binary 150 100 50 0 10 20 30 40 50 60 70 80 90 List Size we obtained from Listing 12.5 (searchcompare.cpp) Unfortunately we cannot empirically compare the running times of the two searches for vectors small enough to demonstrate that linear search is faster for very small vectors As the output of Listing 12.5 (searchcompare.cpp) shows, both searches complete their work in time less than the resolution of our timer for vectors with 1,000 elements Both empirically and analytically, we see that binary search is fast even for very large vectors, while linear search is impractical for large vectors 12.4 Vector Permutations Sometimes it is useful to consider all the possible arrangements of the elements within a vector A sorting algorithm, for example, must work correctly on any initial arrangement of elements in a vector To test a sort function, a programmer could check to see to see if it produces the correct result for all arrangements of a relatively small vector A rearrangement of a collection of ordered items is called a permutation Listing 12.6 (vectorpermutations.cpp) prints all the permutations of the contents of a given vector Listing 12.6: vectorpermutations.cpp #include #include /* * print * Prints the contents of a vector of integers * a is the vector to print; a is not modified */ void print(const std::vector& a) { int n = a.size(); std::cout 0) { std::cout

Ngày đăng: 16/05/2017, 16:20

TỪ KHÓA LIÊN QUAN

w