ebook img

C++ through Examples PDF

335 Pages·2020·2.368 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ through Examples

C++ through Examples Alex Vasilev C++ through Examples Written, codes, cover and illustrations by Alex Vasilev Copyright © 2020 Alex Vasilev All rights reserved ISBN: 9798629182832 Contents C++ through Examples 3 Contents Introduction. The Book about С++ ................................ 7 About the Book ................................ 7 The C++ Programming Language ................................ 8 An Integrated Development Environment ................................ 9 About the Author ................................ 9 Feedback ................................ 9 Downloads ................................ 9 Thanks ................................ 10 Chapter 1. Simple Programs ................................ 11 The First Program ................................ 11 Using Variables ................................ 14 Using Functions ................................ 20 Using a Loop Statement ................................ 23 Using a Conditional Statement ................................ 27 Using Arrays ................................ 29 Chapter 2. Control Statements ................................ 32 The for Loop Statement ................................ 32 The do-while Statement ................................ 37 The switch Statement ................................ 40 Nested Conditional Statements ................................ 46 Nested Loop Statements ................................ 48 The Loop over a Collection ................................ 52 Handling and Throwing Exceptions ................................ 55 The goto Statement ................................ 60 Chapter 3. Pointers, Arrays, and References ................................ 62 Using Pointers ................................ 62 Arrays and Pointers ................................ 65 Contents C++ through Examples 4 Using References ................................ 69 Using Dynamic Memory ................................ 71 Character Arrays ................................ 74 Two-Dimensional Arrays ................................ 79 Arrays of Pointers ................................ 86 Chapter 4. Functions ................................ 92 Using Functions ................................ 92 Overloading Functions ................................ 97 Default Values for Arguments ................................ 101 Using Recursion ................................ 103 Passing Arguments to Functions ................................ 106 Passing Pointers to Functions ................................ 110 Passing Arrays to Functions ................................ 112 Passing a String to a Function ................................ 119 A Pointer as the Result of a Function ................................ 121 A Reference as the Result of a Function ................................ 125 A Dynamic Array as the Result of a Function ................................ 128 Pointers to Functions ................................ 134 Chapter 5. Classes and Objects ................................ 141 Using Classes and Objects ................................ 141 Public and Private Members ................................ 145 Overloading Methods ................................ 148 Constructors and Destructors ................................ 155 Operator Overloading ................................ 162 Using Inheritance ................................ 173 Chapter 6. Using Object-Oriented Programming ................................ 182 A Pointer to an Object ................................ 182 Arrays of Objects ................................ 191 An Array as a Field ................................ 194 Contents C++ through Examples 5 Functors and Object Indexing ................................ 200 The Copy Constructor ................................ 204 Inheritance and Private Members ................................ 208 Virtual Methods and Inheritance ................................ 211 Multiple Inheritance ................................ 215 Base Class Variables ................................ 218 Chapter 7. Template Functions and Classes ................................ 223 Template Functions ................................ 223 Template Functions with Several Parameters ................................ 228 Overloading Template Functions ................................ 230 The Explicit Specialization of Template Functions ................................ 232 Template Classes ................................ 234 The Explicit Specialization of Template Classes ................................ 237 Default Values for Template Parameters ................................ 242 Inheritance of Template Classes ................................ 244 Integer Template Parameters ................................ 250 Chapter 8. Different Programs ................................ 262 Using Structures ................................ 262 Template Structures ................................ 265 Complex Numbers ................................ 267 Numerical Arrays ................................ 272 Dynamic Arrays ................................ 283 Using Sets ................................ 289 Associative Containers ................................ 292 Handling Exceptions ................................ 296 Using Multithreading ................................ 298 Chapter 9. Mathematical Problems ................................ 304 The Fixed-Point Iteration Method ................................ 304 The Bisection Method ................................ 307 Contents C++ through Examples 6 Newton's Method ................................ 312 The Lagrange Interpolation Polynomial ................................ 315 The Newton Interpolation Polynomial ................................ 319 Simpson's Method for Calculating Integrals ................................ 323 The Monte Carlo Method for Calculating Integrals ................................ 326 Euler's Method for Solving Differential Equations ................................ 329 The Classical Runge-Kutta Method ................................ 332 Conclusion. Some Advice ................................ 335 Introduction The Book about С++ 7 Introduction The Book about ++ С You can observe a lot by watching. Yogi Berra This book is about the C++ programming language. If you have no (or little) experience in C++, if you want to succeed in programming in C++, and if you are ready to study hard, then the book is for you. About the Book The book consists of specially selected examples and problems. They cover all main subjects essential for programming in C++. The first chapter gives a general notion of what we can do in C++. There we learn how to create a program. The chapter also contains information about input and output operators, variables, functions, loop statements, conditional statements, and arrays. The second chapter is devoted to the control statements. In this chapter, we consider in detail the conditional statement, loop statements, the selection statement. We also will get a notion about handling and throwing exceptions. In the third chapter, we discuss pointers, arrays, and references. Namely, we consider how to create and use pointers. We discuss the relationship between pointers and arrays, and also we investigate how and when we can use references. Moreover, the chapter contains information about memory allocation. As well, there we analyze the peculiarities of character arrays used to implement strings. The fourth chapter is about functions. There we learn what a function can do and how we can use it. We overload functions, define the arguments with default values, and use recursion. We will get to know how arguments are passed to functions and how we pass a function as an argument. Also, we learn how functions operate with arrays, references, and pointers. Introduction The Book about С++ 8 In the fifth chapter, we discuss the principles of object-oriented programming. We learn how to describe classes and create objects. Also, the chapter contains information about public and private members, constructors and destructors, overloading methods and operators, using inheritance. In the sixth chapter, we continue discussing classes and objects. Namely, we get familiar with pointers to objects, arrays of objects, and functors. We will use objects whose fields are arrays and implement object indexing. We will get familiar with virtual methods and multiple inheritance, and do some other tricks with classes and objects. In the seventh chapter, we investigate template functions and classes. There we create and overload template functions with several parameters. We will come to know how to define default values for template parameters. There we also apply inheritance for template classes. Moreover, in the chapter, we consider how to use integer template parameters. In the eighth chapter, we consider different programs. The chapter explains how to use structures, complex numbers, and containers. We will also get familiar with multithreading. The ninth chapter is devoted to mathematical problems. In that chapter, we show how to implement methods for solving algebraic and differential equations, calculate integrals, and create interpolation polynomials. The C++ Programming Language By now, C++ is one of the most popular programming languages. It is impossible to imagine a professional programmer who would not know C++. In this sense, choosing C++ for studying seems to be very reasonable. In addition to the direct benefits of having the ability to create programs in C++, there is also an important methodological aspect. It is based on exceptional flexibility and richness of the C ++ language. After having studied C++, it is much more comfortable "to adopt" other programming languages. But, in any case, C++ is a "must-know" language, and it Introduction The Book about С++ 9 will be in the nearest future. No doubt, C++ is an excellent choice to get familiar with programming. An Integrated Development Environment There is no lack of software for creating programs in C++. Now, a lot of high- quality software development tools exist, both commercial and free. As usual, an integrated development environment is used, which binds a code editor, debugger, compiler (not always, but very often), and some other additional utilities. Of course, you are free to choose any development environment you want. Considered in the book codes are universal, and they are not tied to any particular application. However, it is essential to note that all the programs in the book were tested in Microsoft Visual Studio Express. About the Author The author of the book, Alex Vasilev, is a Professor in Theoretical Physics at the Physics Department of Kiev University. He teaches programming in C++, C#, Java, JavaScript, and Python for more than fifteen years. Research interests are physics of liquids and liquid crystals, biophysics, synergetic, mathematical methods in economics, modeling of social and political processes, mathematical linguistics. Feedback You can send your comments and suggestions about the book by email [email protected]. Downloads You can download the codes from the book at www.vasilev.kiev.ua/books/cpp/codes.zip. Introduction The Book about С++ 10 Thanks I express my sincere gratitude to the readers for their interest in the book and hope that the book will help in studying C++.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.