Statistics
8
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-02-07

AuthorSlobodan Dmitrović

Learn the C++ programming language in a structured, straightforward, and friendly manner. This book teaches the basics of the modern C++ programming language, the C++ Standard Library, and modern C++ standards, including C++23. No previous programming experience is required. C++ is a language like no other, surprising in its complexity, yet wonderfully sleek and elegant in so many ways. It is also a language that cannot be learned by guessing, one that is easy to get wrong and challenging to get right. To overcome this, each section is filled with real-world examples that gradually increase in complexity. Modern C++ for Absolute Beginners, Second Edition teaches more than just programming in C++23. It provides a solid C++ foundation to build upon. The author takes you through the C++ programming language, the Standard Library, and C++11 to C++23 standard basics. Each chapter is accompanied by the right amount of theory and plenty of source code examples. You will work with C++23 features and standards, yet you will also compare and take a look into previous versions of C++. What You Will Learn • Get Introduced to modern C++ in a friendly but effective way • Work with the basics of C++: types, operators, variables, constants, expressions, references, functions, classes, I/O, smart pointers, polymorphism, and more • Set up the Visual Studio environment on Windows and GCC on Linux, where you can write your own code • Declare and define functions, classes, and objects, and organize code into namespaces • Discover object-oriented programming: classes and objects, encapsulation, inheritance, polymorphism, and more using the most advanced C++ features • Employ best practices in organizing source code and controlling program workflow • Get familiar with C++ language do's and don'ts, and more • Manage the basics of lambdas, inheritance, polymorphism, smart pointers, templates, modules, contracts, concepts, and more For novice programmers who wish to learn C++

Tags
No tags
ISBN: 1484292731
Publisher: Apress
Publish Year: 2023
Language: 英文
Pages: 441
File Format: PDF
File Size: 5.4 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Modern C++ for Absolute Beginners A Friendly Introduction to the C++ Programming Language and C++11 to C++23 Standards Second Edition Slobodan Dmitrović
Modern C++ for Absolute Beginners: A Friendly Introduction to the C++ Programming Language and C++11 to C++23 Standards ISBN-13 (pbk): 978-1-4842-9273-0 ISBN-13 (electronic): 978-1-4842-9274-7 https://doi.org/10.1007/978-1-4842-9274-7 Copyright © 2023 by Slobodan Dmitrović This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: James Markham Coordinating Editor: Gryffin Winkler Cover image designed by eStudioCalamar Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@ springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Slobodan Dmitrović Belgrade, Serbia
To Mary Anne, whose work is an inspiration to me.
v About the Author �����������������������������������������������������������������������������������������������������xv About the Technical Reviewer �������������������������������������������������������������������������������xvii Acknowledgments ��������������������������������������������������������������������������������������������������xix About the Second Edition ���������������������������������������������������������������������������������������xxi Table of Contents Chapter 1: Introduction��������������������������������������������������������������������������������������������� 1 Chapter 2: What Is C++? ������������������������������������������������������������������������������������������ 3 C++ Standards 3 Chapter 3: C++ Compilers ���������������������������������������������������������������������������������������� 5 Installing C++ Compilers  5 On Linux  5 On Windows  6 Chapter 4: Our First Program ����������������������������������������������������������������������������������� 7 Comments  7 Hello World Example  8 Chapter 5: Types ����������������������������������������������������������������������������������������������������� 13 Fundamental Types  13 Boolean  13 Character Type  14 Integer Types  16 Floating-Point Types  18 Type void  18 Type Modifiers  19 Variable Declaration, Definition, and Initialization  19
vi Chapter 6: Exercises ����������������������������������������������������������������������������������������������� 21 Hello World and Comments  21 Declaration  21 Definition  22 Initialization  22 Chapter 7: Operators ���������������������������������������������������������������������������������������������� 25 Assignment Operator  25 Arithmetic Operators  25 Compound Assignment Operators 27 Increment/Decrement Operators  28 Chapter 8: Standard Input �������������������������������������������������������������������������������������� 29 Chapter 9: Exercises ����������������������������������������������������������������������������������������������� 31 Standard Input  31 Two Inputs  31 Multiple Inputs  32 Inputs and Arithmetic Operations 33 Postincrement and Compound Assignment  33 Integral and Floating-Point Division  34 Chapter 10: Arrays �������������������������������������������������������������������������������������������������� 35 Chapter 11: Pointers ����������������������������������������������������������������������������������������������� 37 Chapter 12: References ������������������������������������������������������������������������������������������ 41 Chapter 13: Introduction to Strings ������������������������������������������������������������������������ 43 Defining a String  43 Concatenating Strings  44 Accessing Characters  45 Comparing Strings  46 String Input  47 A Pointer to a String  48 Table of ConTenTs
vii Substrings  48 Finding a Substring  49 Chapter 14: Automatic Type Deduction ������������������������������������������������������������������ 51 Chapter 15: Exercises ��������������������������������������������������������������������������������������������� 53 Array Definition  53 Pointer to an Object  54 Reference Type 54 Strings  55 Strings from Standard Input  55 Creating a Substring  56 Finding a Single Character  57 Finding a Substring  58 Automatic Type Deduction  59 Chapter 16: Statements ������������������������������������������������������������������������������������������ 61 Selection Statements  61 if Statement  61 Conditional Expression  63 The Logical Operators 65 switch Statement  70 Iteration Statements  71 for Statement 72 while Statement  73 do Statement  75 Chapter 17: Constants �������������������������������������������������������������������������������������������� 77 Chapter 18: Exercises ��������������������������������������������������������������������������������������������� 79 A Simple if Statement  79 Logical Operators  80 The switch Statement  81 Table of ConTenTs
viii The for Loop  82 Array and the for Loop  83 The const Type Qualifier  83 Chapter 19: Functions �������������������������������������������������������������������������������������������� 85 Introduction  85 Function Declaration  85 Function Definition  86 Return Statement  89 Passing Arguments  91 Passing by Value/Copy  91 Passing by Reference  92 Passing by Const Reference  93 Function Overloading  94 Chapter 20: Exercises ��������������������������������������������������������������������������������������������� 97 Function Definition  97 Separate Declaration and Definition  97 Function Parameters  98 Passing Arguments  99 Function Overloads  99 Chapter 21: Scope and Lifetime ���������������������������������������������������������������������������� 101 Local Scope  101 Block Scope  101 Lifetime  102 Automatic Storage Duration  102 Dynamic Storage Duration  102 Static Storage Duration  103 Operators new and delete  103 Table of ConTenTs
ix Chapter 22: Exercises ������������������������������������������������������������������������������������������� 105 Automatic Storage Duration  105 Dynamic Storage Duration  106 Automatic and Dynamic Storage Durations  107 Chapter 23: Classes – Introduction ���������������������������������������������������������������������� 109 Data Member Fields  109 Member Functions  110 Access Specifiers  112 Constructors  116 Default Constructor  116 Member Initialization  119 Copy Constructor  120 Copy Assignment  123 Move Constructor  124 Move Assignment  126 Operator Overloading  128 Destructors  134 Chapter 24: Exercises ������������������������������������������������������������������������������������������� 137 Class Instance  137 Class with Data Members  137 Class with Member Function  138 Class with Data and Function Members  139 Class Access Specifiers  139 User-Defined Default Constructor and Destructor  140 Constructor Initializer List  141 User-Defined Copy Constructor  143 User-Defined Move Constructor  144 Overloading Arithmetic Operators  145 Table of ConTenTs
x Chapter 25: Classes – Inheritance and Polymorphism ����������������������������������������� 147 Inheritance  147 Polymorphism  151 Chapter 26: Exercises ������������������������������������������������������������������������������������������� 157 Inheritance  157 Chapter 27: The static Specifier ��������������������������������������������������������������������������� 161 Chapter 28: Templates ������������������������������������������������������������������������������������������ 165 Function Templates  165 Class Templates  167 Template Specialization  171 Chapter 29: Enumerations ������������������������������������������������������������������������������������ 173 Chapter 30: Exercises ������������������������������������������������������������������������������������������� 177 Static Variable  177 Static Data Member  178 Static Member Function  179 Function Template  180 Class Template  181 Scoped Enums  182 Enums in a switch  183 Chapter 31: Organizing Code �������������������������������������������������������������������������������� 185 Header and Source Files  185 Header Guards  186 Namespaces  186 Chapter 32: Exercises ������������������������������������������������������������������������������������������� 191 Header and Source Files  191 Multiple Source Files  192 Namespaces  193 Nested Namespaces  194 Table of ConTenTs
xi Chapter 33: Conversions��������������������������������������������������������������������������������������� 197 Implicit Conversions  197 Explicit Conversions  201 Chapter 34: Exceptions ����������������������������������������������������������������������������������������� 205 Chapter 35: Smart Pointers ���������������������������������������������������������������������������������� 211 Unique Pointer  211 Shared Pointer  214 Chapter 36: Exercises ������������������������������������������������������������������������������������������� 217 static_cast Conversion  217 A Simple Unique Pointer  218 Unique Pointer to an Object of a Class  218 Shared Pointer Exercise  219 Simple Polymorphism  220 Polymorphism II  221 Exception Handling  222 Multiple Exceptions  223 Chapter 37: Input/Output Streams ������������������������������������������������������������������������ 225 File Streams  225 String Streams  229 Chapter 38: C++ Standard Library and Friends ���������������������������������������������������� 235 Containers  235 std::vector  236 std::array  238 std::set  239 std::map  241 std::pair  244 Other Containers  245 The Range-Based for Loop 245 Iterators 248 Table of ConTenTs
xii Algorithms and Utilities  251 std::sort  252 std::find  254 std::copy  255 Min and Max Elements  257 Lambda Expressions  258 Chapter 39: Exercises ������������������������������������������������������������������������������������������� 267 Basic Vector  267 Deleting a Single Value  268 Deleting a Range of Elements  268 Finding Elements in a Vector 269 Basic Set  270 Set Data Manipulation  271 Set Member Functions  272 Search for Data in a Set  273 Basic Map  274 Inserting into a Map  275 Searching and Deleting from a Map  277 Lambda Expressions  278 Chapter 40: C++ Standards ���������������������������������������������������������������������������������� 281 C++11  281 Automatic Type Deduction  282 Range-Based Loops 282 Initializer Lists  283 Move Semantics  284 Lambda Expressions  284 The constexpr Specifier  285 Scoped Enumerators  286 Smart Pointers  286 Table of ConTenTs
xiii std::unordered_set  288 std::unordered_map  290 std::tuple  292 static_assert  293 Introduction to Concurrency  294 Deleted and Defaulted Functions  301 Type Aliases  304 C++14  304 Binary Literals  305 Digit Separators  306 Auto for Functions  306 Generic Lambdas  307 std::make_unique  307 C++17  308 Nested Namespaces  308 Constexpr Lambdas  309 Structured Bindings  309 std::filesystem  311 std::string_view  315 std::any  317 std::variant 319 C++20  322 Modules  323 Concepts  325 Lambda Templates  329 [likely] and [unlikely] Attributes  330 Ranges  332 Coroutines  336 std::span  337 Mathematical Constants  338 Table of ConTenTs
xiv C++23  339 Multidimensional Subscript Operator  340 Literal Suffixes for size_t  340 The #warning Preprocessor Directive  341 The std::string::contains( ) Member Function  341 std::print  343 Chapter 41: Projects ��������������������������������������������������������������������������������������������� 347 Project I: Book Inventory  347 Project II: Book Inventory – Multiple Files  362 Project III: Message Logger  367 Project IV: Message Logger – Multiple Files  386 Project V: Information System  392 Project VI: Information System – Multiple Files  422 Index ��������������������������������������������������������������������������������������������������������������������� 433 Table of ConTenTs
xv About the Author Slobodan Dmitrović is a software consultant, trainer, and author of several programming books. He is a professional R&D software developer with two decades of experience in the industry. Slobodan provides C++ training courses for corporate clients and academic institutions. Connect with Slobodan at https://www.linkedin.com/in/slobodan- dmitrovic/.
xvii About the Technical Reviewer Germán González-Morris is a polyglot software architect/ engineer with 20+ years of experience in the field, having knowledge in Java, Spring, C, Julia, Python, Haskell, and JavaScript, among others. He works for cloud (architecture) and web distributed applications. Germán loves math puzzles (including reading Knuth), swimming, and table tennis. Also, he has reviewed several books including an application container book (WebLogic) and books on languages (C, Haskell, TypeScript, WebAssembly, Math for coders, regexp, Julia, Algorithms). For more details, you can visit his blog (https://devwebcl.blogspot.com/) or Twitter account (@devwebcl).
xix Acknowledgments I want to thank my friends, readers, and fellow C++ peers who have supported me in writing the second edition of this book. I am thankful to the entire C++ community for their help and feedback. I would like to thank Rainer Grimm, Klaus Iglberger, Jens Weller, Bartłomiej Filipek, and many others. My most profound appreciation goes to S. Antonijević, Ron and Brankica, and Saša Popović for their ongoing support.
xxi About the Second Edition The second edition of this book is updated to include the notable features inside the C++23 standard. I have revised and updated numerous chapters with more relevant information and clarification. The second edition includes more diagrams to represent certain concepts better. I have also added the output for all the code snippets. The book now has a chapter entirely dedicated to various projects. My intent with the second edition was to provide a simplified, updated, and accurate introduction to the modern C++ programming language.
1 CHAPTER 1 Introduction Congratulations on choosing to learn the C++ programming language, and thank you for picking up this book. I will try to introduce you to a beautiful world of C++ to the best of my abilities. This book is an effort to introduce the reader to the C++ programming language in a structured, straightforward, and friendly manner. We will use the “just enough theory and plenty of examples” approach whenever possible. To me, C++ is a wonderful product of the human intellect. Over the years, I have certainly come to think of it as a thing of beauty and elegance. C++ is a language like no other, surprising in its complexity yet wonderfully sleek and elegant in so many ways. It is also a language that cannot be learned by guessing, one that is easy to get wrong and challenging to get right. In this book, we will get familiar with the language basics first. Then, we will move on to classes, templates, and the C++ Standard Library. Once we got these covered, we will describe the modern C++ standards in more detail. After each section, source code exercises will help us adopt the learned material more efficiently. Finally, we will create a couple of source code projects. Let us get started! © Slobodan Dmitrović 2023 S. Dmitrović, Modern C++ for Absolute Beginners, https://doi.org/10.1007/978-1-4842-9274-7_1
3 CHAPTER 2 What Is C++? C++ is a programming language – a standardized, general-purpose, object-oriented, compiled language. Every C++ compiler is accompanied by a set of useful functions and containers called the C++ Standard Library. Bjarne Stroustrup created C++ as an extension to a C programming language. Still, C++ evolved to be a completely different programming language. Let us emphasize this: C and C++ are two different languages. C++ started as “C with classes,” but it is now a completely different language. So, C++ is not C; C++ is not C with classes; it is just C++. And there is no such thing as a C/C++ programming language. C++ is widely used for so-called systems programming as well as application programming. C++ is a language that allows us to get down to the metal where we can perform low-level routines if needed or soar high using abstraction mechanisms such as templates and classes. C++ Standards C++ is governed by the ISO C++ standard. There are multiple ISO C++ standards listed here in chronological order: C++03, C++11, C++14, C++17, C++20, and the upcoming C++23 standard. Every C++ standard, starting with C++11 onward, is referred to as “modern C++.” And modern C++ is what we will be teaching in this book. © Slobodan Dmitrović 2023 S. Dmitrović, Modern C++ for Absolute Beginners, https://doi.org/10.1007/978-1-4842-9274-7_2