C++ 20 Quick Syntax Reference A Pocket Guide to the Language, APIs, and Library (Mikael Olsson) (Z-Library)

Author: Mikael Olsson

科学

No Description

📄 File Format: PDF
💾 File Size: 2.1 MB
23
Views
0
Downloads
0.00
Total Donations

📄 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.

📄 Page 1
C++20 Quick Syntax Reference A Pocket Guide to the Language, APIs, and Library — Fourth Edition — Mikael Olsson
📄 Page 2
C++20 Quick Syntax Reference A Pocket Guide to the Language, APIs, and Library Fourth Edition Mikael Olsson
📄 Page 3
C++20 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library ISBN-13 (pbk): 978-1-4842-5994-8 ISBN-13 (electronic): 978-1-4842-5995-5 https://doi.org/10.1007/978-1-4842-5995-5 Copyright © 2020 by Mikael Olsson 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: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. 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 editorial@apress.com; for reprint, paperback, or audio rights, please email 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/9781484259948. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Mikael Olsson Hammarland, Finland
📄 Page 4
iii About the Author �������������������������������������������������������������������������������xiii About the Technical Reviewer ������������������������������������������������������������xv Introduction ��������������������������������������������������������������������������������������xvii Chapter 1: Hello World ��������������������������������������������������������������������������1 Choosing an IDE ����������������������������������������������������������������������������������������������������1 Creating a Project �������������������������������������������������������������������������������������������������2 Adding a Source File ���������������������������������������������������������������������������������������������2 Selecting Language Standard �������������������������������������������������������������������������������2 Hello World ������������������������������������������������������������������������������������������������������������3 Using the Standard Namespace ����������������������������������������������������������������������������4 IntelliSense �����������������������������������������������������������������������������������������������������������4 Chapter 2: Compile and Run �����������������������������������������������������������������5 Visual Studio Compilation �������������������������������������������������������������������������������������5 Console Compilation ���������������������������������������������������������������������������������������������5 Comments �������������������������������������������������������������������������������������������������������������6 Chapter 3: Variables �����������������������������������������������������������������������������7 Data Types �������������������������������������������������������������������������������������������������������������7 Declaring Variables �����������������������������������������������������������������������������������������������8 Assigning Variables �����������������������������������������������������������������������������������������������8 Variable Scope ������������������������������������������������������������������������������������������������������9 Integer Types �������������������������������������������������������������������������������������������������������10 Table of Contents
📄 Page 5
iv Signed and Unsigned Integers ����������������������������������������������������������������������������11 Numeric Literals ��������������������������������������������������������������������������������������������������12 Floating-Point Types ��������������������������������������������������������������������������������������������13 Literal Suffixes ����������������������������������������������������������������������������������������������������14 Char Type ������������������������������������������������������������������������������������������������������������14 Bool Type �������������������������������������������������������������������������������������������������������������16 Chapter 4: Operators ��������������������������������������������������������������������������17 Arithmetic Operators �������������������������������������������������������������������������������������������17 Assignment Operators ����������������������������������������������������������������������������������������18 Increment and Decrement Operators ������������������������������������������������������������������18 Comparison Operators ����������������������������������������������������������������������������������������19 Logical Operators ������������������������������������������������������������������������������������������������19 Bitwise Operators �����������������������������������������������������������������������������������������������20 Operator Precedence ������������������������������������������������������������������������������������������20 Chapter 5: Pointers �����������������������������������������������������������������������������23 Creating Pointers ������������������������������������������������������������������������������������������������23 Dereferencing Pointers ���������������������������������������������������������������������������������������24 Pointing to a Pointer ��������������������������������������������������������������������������������������������25 Dynamic Allocation ���������������������������������������������������������������������������������������������25 Null Pointer ���������������������������������������������������������������������������������������������������������26 Chapter 6: References ������������������������������������������������������������������������29 Creating References��������������������������������������������������������������������������������������������29 References and Pointers �������������������������������������������������������������������������������������30 Reference and Pointer Guideline�������������������������������������������������������������������������30 Rvalue Reference ������������������������������������������������������������������������������������������������30 Table of ConTenTs
📄 Page 6
v Chapter 7: Arrays ��������������������������������������������������������������������������������33 Array Declaration and Allocation �������������������������������������������������������������������������33 Array Assignment ������������������������������������������������������������������������������������������������33 Multidimensional Arrays �������������������������������������������������������������������������������������34 Dynamic Arrays ���������������������������������������������������������������������������������������������������35 Array Size ������������������������������������������������������������������������������������������������������������35 Vector ������������������������������������������������������������������������������������������������������������������36 Chapter 8: Strings �������������������������������������������������������������������������������39 String Combining ������������������������������������������������������������������������������������������������39 Escape Characters ����������������������������������������������������������������������������������������������40 String Compare ���������������������������������������������������������������������������������������������������41 String Functions ��������������������������������������������������������������������������������������������������41 String Encodings �������������������������������������������������������������������������������������������������42 String Formatting ������������������������������������������������������������������������������������������������43 Chapter 9: Conditionals ����������������������������������������������������������������������45 If Statement ��������������������������������������������������������������������������������������������������������45 Switch Statement �����������������������������������������������������������������������������������������������46 Ternary Operator �������������������������������������������������������������������������������������������������47 Initializers������������������������������������������������������������������������������������������������������������47 Chapter 10: Loops �������������������������������������������������������������������������������49 While Loop ����������������������������������������������������������������������������������������������������������49 Do-while Loop �����������������������������������������������������������������������������������������������������50 For Loop ��������������������������������������������������������������������������������������������������������������50 Break and Continue ���������������������������������������������������������������������������������������������52 Goto Statement ���������������������������������������������������������������������������������������������������52 Table of ConTenTs
📄 Page 7
vi Chapter 11: Functions ������������������������������������������������������������������������53 Defining Functions ����������������������������������������������������������������������������������������������53 Calling Functions ������������������������������������������������������������������������������������������������53 Function Parameters �������������������������������������������������������������������������������������������54 Default Parameter Values ������������������������������������������������������������������������������������54 Function Overloading ������������������������������������������������������������������������������������������55 Return Statement ������������������������������������������������������������������������������������������������55 Forward Declaration ��������������������������������������������������������������������������������������������56 Pass by Value ������������������������������������������������������������������������������������������������������57 Pass by Reference ����������������������������������������������������������������������������������������������58 Pass by Address ��������������������������������������������������������������������������������������������������58 Return by Value, Reference, or Address ��������������������������������������������������������������59 Inline Functions ���������������������������������������������������������������������������������������������������60 Auto and Decltype �����������������������������������������������������������������������������������������������61 Returning Multiple Values �����������������������������������������������������������������������������������63 Lambda Functions �����������������������������������������������������������������������������������������������66 Chapter 12: Classes ����������������������������������������������������������������������������71 Class Methods �����������������������������������������������������������������������������������������������������71 Inline Methods ����������������������������������������������������������������������������������������������������72 Object Creation ���������������������������������������������������������������������������������������������������72 Accessing Object Members ��������������������������������������������������������������������������������73 Forward Declaration ��������������������������������������������������������������������������������������������74 Chapter 13: Constructors �������������������������������������������������������������������75 Constructor Overloading �������������������������������������������������������������������������������������76 This Keyword ������������������������������������������������������������������������������������������������������77 Field Initialization ������������������������������������������������������������������������������������������������77 Table of ConTenTs
📄 Page 8
vii Default Constructor ���������������������������������������������������������������������������������������������78 Destructor �����������������������������������������������������������������������������������������������������������79 Special Member Functions ���������������������������������������������������������������������������������79 Object Initialization ���������������������������������������������������������������������������������������������80 Direct Initialization ����������������������������������������������������������������������������������������������81 Value Initialization �����������������������������������������������������������������������������������������������81 Copy Initialization ������������������������������������������������������������������������������������������������82 New Initialization ������������������������������������������������������������������������������������������������82 Aggregate Initialization ���������������������������������������������������������������������������������������83 Uniform Initialization �������������������������������������������������������������������������������������������83 Designated Initializers�����������������������������������������������������������������������������������������85 Chapter 14: Inheritance ����������������������������������������������������������������������87 Upcasting ������������������������������������������������������������������������������������������������������������87 Downcasting �������������������������������������������������������������������������������������������������������88 Constructor Inheritance ��������������������������������������������������������������������������������������89 Multiple Inheritance ��������������������������������������������������������������������������������������������90 Chapter 15: Overriding �����������������������������������������������������������������������93 Hiding Derived Members�������������������������������������������������������������������������������������93 Overriding Derived Members ������������������������������������������������������������������������������94 Base Class Scoping ���������������������������������������������������������������������������������������������96 Pure Virtual Functions �����������������������������������������������������������������������������������������96 Chapter 16: Access Levels ������������������������������������������������������������������99 Private Access �����������������������������������������������������������������������������������������������������99 Protected Access ����������������������������������������������������������������������������������������������100 Public Access ����������������������������������������������������������������������������������������������������101 Table of ConTenTs
📄 Page 9
viii Access Level Guideline �������������������������������������������������������������������������������������101 Friend Classes and Functions ���������������������������������������������������������������������������102 Public, Protected, and Private Inheritance ��������������������������������������������������������104 Chapter 17: Static �����������������������������������������������������������������������������105 Static Fields ������������������������������������������������������������������������������������������������������105 Static Methods ��������������������������������������������������������������������������������������������������106 Static Local Variables ����������������������������������������������������������������������������������������107 Static Global Variables ��������������������������������������������������������������������������������������107 Chapter 18: Enum Types �������������������������������������������������������������������109 Enum Example ��������������������������������������������������������������������������������������������������109 Enum Constant Values ��������������������������������������������������������������������������������������110 Enum Scope ������������������������������������������������������������������������������������������������������111 Weakly Typed Enums ����������������������������������������������������������������������������������������111 Enum Constant Type ������������������������������������������������������������������������������������������113 Chapter 19: Structs and Unions ��������������������������������������������������������115 Structs���������������������������������������������������������������������������������������������������������������115 Struct Initialization ��������������������������������������������������������������������������������������������115 Union �����������������������������������������������������������������������������������������������������������������117 Anonymous Union ���������������������������������������������������������������������������������������������118 Chapter 20: Operator Overloading ����������������������������������������������������121 Binary Operator Overloading �����������������������������������������������������������������������������122 Unary Operator Overloading ������������������������������������������������������������������������������122 Comparison Operator Overloading ��������������������������������������������������������������������123 Overloadable Operators ������������������������������������������������������������������������������������126 Table of ConTenTs
📄 Page 10
ix Chapter 21: Custom Conversions �����������������������������������������������������127 Implicit Conversion Constructor ������������������������������������������������������������������������127 Explicit Conversion Constructor ������������������������������������������������������������������������129 Conversion Operators ����������������������������������������������������������������������������������������129 Explicit Conversion Operators ���������������������������������������������������������������������������130 Chapter 22: Namespaces ������������������������������������������������������������������133 Accessing Namespace Members ����������������������������������������������������������������������134 Nesting Namespaces ����������������������������������������������������������������������������������������134 Importing Namespaces �������������������������������������������������������������������������������������135 Namespace Member Import �����������������������������������������������������������������������������135 Namespace Alias �����������������������������������������������������������������������������������������������136 Type Alias ����������������������������������������������������������������������������������������������������������136 Including Namespace Members �����������������������������������������������������������������������137 Chapter 23: Constants ����������������������������������������������������������������������139 Constant Variables ��������������������������������������������������������������������������������������������139 Constant Pointers ����������������������������������������������������������������������������������������������139 Constant References �����������������������������������������������������������������������������������������140 Constant Objects �����������������������������������������������������������������������������������������������140 Constant Methods ���������������������������������������������������������������������������������������������141 Constant Return Type and Parameters ��������������������������������������������������������������142 Constant Fields �������������������������������������������������������������������������������������������������142 Constant Expressions ����������������������������������������������������������������������������������������143 Immediate Functions ����������������������������������������������������������������������������������������146 Constant Guideline ��������������������������������������������������������������������������������������������146 Table of ConTenTs
📄 Page 11
x Chapter 24: Preprocessor �����������������������������������������������������������������147 Including Source Files ���������������������������������������������������������������������������������������148 Define ����������������������������������������������������������������������������������������������������������������148 Undefine ������������������������������������������������������������������������������������������������������������149 Predefined Macros ��������������������������������������������������������������������������������������������149 Macro Functions �����������������������������������������������������������������������������������������������150 Conditional Compilation ������������������������������������������������������������������������������������152 Compile if Defined ���������������������������������������������������������������������������������������������153 Error ������������������������������������������������������������������������������������������������������������������153 Line �������������������������������������������������������������������������������������������������������������������154 Pragma ��������������������������������������������������������������������������������������������������������������154 Attributes ����������������������������������������������������������������������������������������������������������154 Chapter 25: Exception Handling �������������������������������������������������������157 Throwing Exceptions �����������������������������������������������������������������������������������������157 Try-Catch Statement �����������������������������������������������������������������������������������������157 Rethrowing Exceptions �������������������������������������������������������������������������������������159 Noexcept Specifier ��������������������������������������������������������������������������������������������159 Exception Class �������������������������������������������������������������������������������������������������160 Chapter 26: Type Conversions ����������������������������������������������������������161 Implicit Conversions ������������������������������������������������������������������������������������������161 Explicit Conversions ������������������������������������������������������������������������������������������162 C++ Casts ���������������������������������������������������������������������������������������������������162 Static Cast ���������������������������������������������������������������������������������������������������������163 Reinterpret Cast ������������������������������������������������������������������������������������������������163 Table of ConTenTs
📄 Page 12
xi Const Cast ���������������������������������������������������������������������������������������������������������164 C-Style and New-Style Casts ����������������������������������������������������������������������������165 Dynamic Cast ����������������������������������������������������������������������������������������������������165 Dynamic or Static Cast ��������������������������������������������������������������������������������������167 Chapter 27: Smart Pointers ��������������������������������������������������������������169 Unique Pointer ��������������������������������������������������������������������������������������������������169 Shared Pointer ��������������������������������������������������������������������������������������������������170 Weak Shared Pointer ����������������������������������������������������������������������������������������171 Chapter 28: Templates ����������������������������������������������������������������������173 Function Templates �������������������������������������������������������������������������������������������173 Calling Function Templates �������������������������������������������������������������������������������174 Multiple Template Parameters ��������������������������������������������������������������������������175 Class Templates ������������������������������������������������������������������������������������������������176 Non-type Parameters ����������������������������������������������������������������������������������������177 Default Types and Values ����������������������������������������������������������������������������������178 Class Template Specialization ���������������������������������������������������������������������������178 Function Template Specialization ���������������������������������������������������������������������179 Variable Templates ��������������������������������������������������������������������������������������������180 Variadic Templates ��������������������������������������������������������������������������������������������181 Fold Expressions �����������������������������������������������������������������������������������������������182 Concepts �����������������������������������������������������������������������������������������������������������184 Abbreviated Function Templates �����������������������������������������������������������������������186 Template Lambdas ��������������������������������������������������������������������������������������������187 Table of ConTenTs
📄 Page 13
xii Chapter 29: Headers �������������������������������������������������������������������������189 Why Use Headers ����������������������������������������������������������������������������������������������189 Using Headers ���������������������������������������������������������������������������������������������������190 What to Include in Headers �������������������������������������������������������������������������������190 Inline Variables ��������������������������������������������������������������������������������������������������194 Include Guards ��������������������������������������������������������������������������������������������������195 Modules ������������������������������������������������������������������������������������������������������������196 Index �������������������������������������������������������������������������������������������������199 Table of ConTenTs
📄 Page 14
xiii About the Author Mikael Olsson is a professional web entrepreneur, programmer, and author. He works for an R&D company in Finland where he specializes in software development. In his spare time, he writes books and creates websites that summarize various fields of interest. The books he writes are focused on teaching their subject in the most efficient way possible, by explaining only what is relevant and practical without any unnecessary repetition or theory.
📄 Page 15
xv About the Technical Reviewer Marc Gregoire is a software engineer from Belgium. He graduated from the University of Leuven, Belgium, with a degree in “Burgerlijk ingenieur in de computerwetenschappen” (equivalent to a master of science degree in computer engineering). The year after, he received the cum laude degree of master in artificial intelligence at the same university. After his studies, Marc started working for a software consultancy company called Ordina Belgium. As a consultant, he worked for Siemens and Nokia Siemens Networks on critical 2G and 3G software running on Solaris for telecom operators. This required working on international teams stretching from South America and the United States to Europe, the Middle East, and Asia. Currently, Marc works for Nikon Metrology on industrial 3D laser scanning software.
📄 Page 16
xvii Introduction The C++ programming language is a general-purpose multiparadigm language created by Bjarne Stroustrup. The development of the language started in 1979 under the name “C with classes.” As the name implies, it was an extension of the C language with the additional concept of classes. Stroustrup wanted to create a better C that combined the power and efficiency of C with high-level abstractions to better manage large development projects. The resulting language was renamed C++ (pronounced “C-plus-plus”) in 1983. As a deliberate design feature, C++ maintains compatibility with C, and so most C code can easily be made to compile in C++. The introduction of C++ became a major milestone in the software industry as a widely successful language for both system and application development. System programming involves software that controls the computer hardware directly, such as drivers, operating systems, and software for embedded microprocessors. These areas remain the core domain of the language, where resources are scarce and come at a premium. C++ is also widely used for writing applications, which run on top of system software, especially high-performance software such as games, databases, and resource-demanding desktop applications. Despite the introduction of many modern, high-level languages in this domain— such as Java, C#, and Python—C++ still holds its own and overall remains one of the most popular and influential programming languages in use today. There are several reasons for the widespread adoption of C++. The foremost reason was the rare combination of high-level and low-level abstractions from the hardware. The low-level efficiency was inherited
📄 Page 17
xviii from C, and the high-level constructs came in part from a simulation language called Simula. This combination makes it possible to write C++ software with the strength of both approaches. Another strong point of the language is that it does not impose a specific programming paradigm on its users. It is designed to give the programmer a lot of freedom by supporting many different programming styles or paradigms, such as procedural, object-oriented, and generic programming. C++ is updated and maintained by the C++ standards committee. In 1998, the first international standard was published, known informally as C++98. The language has since undergone five more revisions with further improvements, including C++03, C++11, C++14, C++17, and most recently C++20, which is the latest ISO standard for the C++ programming language released in 2020. InTroduCTIon
📄 Page 18
1© Mikael Olsson 2020 M. Olsson, C++20 Quick Syntax Reference, https://doi.org/10.1007/978-1-4842-5995-5_1 CHAPTER 1 Hello World Choosing an IDE To begin developing in C++, you need a text editor and a C++ compiler. You can get both at the same time by installing an Integrated Development Environment (IDE) that includes support for C++. A good choice is Microsoft's Visual Studio Community Edition, which is a free version of Visual Studio that is available from Microsoft’s website.1 The C++ compiler that comes with this IDE has good support for the C++17 standard and includes many features of C++20 as of the 2019 version. If you are running the Visual Studio installer on Windows, make sure to select the “Desktop development with C++” workload to enable development in C++. Visual Studio is available on Windows and Mac, and there is a lightweight version called Visual Studio Code which can also be run on Linux. Two other popular cross-platform IDEs include NetBeans and Eclipse CDT. Alternatively, you can develop using a simple text editor such as Notepad, although this is less convenient than using an IDE. If you choose to use a simple text editor, just create an empty document with a .cpp file extension and open it in the editor of your choice. 1 http://visualstudio.microsoft.com
📄 Page 19
2 Creating a Project After installing Visual Studio 2019, go ahead and launch the program. You then need to create a project, which will manage the C++ source files and other resources. Go to File ➤ New ➤ Project in Visual Studio to display the Create a new project window. From there, select the C++ language from the drop-down list to view only the C++ project templates. Then select the Empty Project template and click the Next button. At the next screen, you can configure the name and location of the project if you want to. When you are finished, click the Create button to let the wizard create your empty project. Adding a Source File You have now created a C++ project. In the Solution Explorer pane (choose View ➤ Solution Explorer), you can see that the project consists of three empty folders: Header Files, Resource Files, and Source Files. Right-click the Source Files folder and choose Add ➤ New Item. From the Add New Item dialog box, choose the C++ File (.cpp) type. Give this source file the name MyApp and click the Add button. An empty .cpp file will now be added to your project and opened for you. Selecting Language Standard To enable the latest features of the C++ language outlined in this book, it is necessary to manually change the language standard setting for your project. You can do this by first going to Project ➤ Properties to bring up the Property pages. From there, navigate to Configuration Properties ➤ C/C++ ➤ Language ➤ C++ Language Standard. Select the latest standard from the drop-down list (std:c++latest). Click OK and the project will now be configured to compile with the latest supported C++20 features. Chapter 1 hello World
📄 Page 20
3 Hello World The first thing to add to the source file is the main() function. This is the entry point of the program, and the code inside of the curly brackets is executed when the program runs. The brackets, along with their content, are referred to as a code block, or just a block. int main() {} The first application will simply output the text "Hello World" to the screen. Before this can be done, the iostream header needs to be included. This header provides input and output functionality for the program, and it is one of the standard library files that comes with all C++ compilers. The #include directive effectively replaces the line with everything in the specified header before the file is compiled into an executable. #include <iostream> int main() {} With iostream included, you gain access to several new functions. These are all located in the standard namespace called std, which you can examine by using a double colon, also called the scope resolution operator (::). After typing this in Visual Studio, the IntelliSense window will automatically open, displaying the namespace contents. Among the members, you find the cout stream. This is the standard output stream in C++ which can be used to print text to a console window. It uses two less than signs, collectively known as the insertion operator (<<), to indicate what to output. The string can then be specified, delimited by double quotes, and followed by a semicolon. The semicolon is used in C++ to mark the end of a statement. Chapter 1 hello World
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List