Book Image

Modern C++: Efficient and Scalable Application Development

By : Richard Grimes, Marius Bancila
Book Image

Modern C++: Efficient and Scalable Application Development

By: Richard Grimes, Marius Bancila

Overview of this book

C++ is one of the most widely used programming languages. It is fast, flexible, and used to solve many programming problems. This Learning Path gives you an in-depth and hands-on experience of working with C++, using the latest recipes and understanding most recent developments. You will explore C++ programming constructs by learning about language structures, functions, and classes, which will help you identify the execution flow through code. You will also understand the importance of the C++ standard library as well as memory allocation for writing better and faster programs. Modern C++: Efficient and Scalable Application Development deals with the challenges faced with advanced C++ programming. You will work through advanced topics such as multithreading, networking, concurrency, lambda expressions, and many more recipes. By the end of this Learning Path, you will have all the skills to become a master C++ programmer. This Learning Path includes content from the following Packt products: • Beginning C++ Programming by Richard Grimes • Modern C++ Programming Cookbook by Marius Bancila • The Modern C++ Challenge by Marius Bancila
Table of Contents (24 chapters)
Title Page
Copyright
About Packt
Contributors
Preface
12
Math Problems
13
Language Features
14
Strings and Regular Expressions
15
Streams and Filesystems
16
Date and Time
17
Algorithms and Data Structures
Index

Preface

C++ is one of the most widely used programming languages. It is fast, flexible, and efficient which is used to solve many programming problems.

The objective of this Learning Path is to make you familiar and comfortable with C++. You will become familiar with the constructs of the C++ programming by learning about the language structures, functions, and classes which will help you identify the execution flow through the code. You will explore and understand the importance of the C++ standard library as well as memory allocation for writing better and faster programs.

This Learning Path also deals with understanding the challenges that come with advanced C++ programming. You will learn about advanced topics such as multithreading, networking, concurrency, performance, meta-programming, lambda expressions, regular expressions, testing, and many more in the form of recipes.

By the end of this Learning Path, you will become an expert in C++.

Who This Book Is For

This Learning Path is designed for developers who want to gain a solid foundation with C++.  A computer, an Internet connection, and the desire to learn how to code in C++ are all you need to get started with this Learning Path.

What This Book Covers

Chapter 1Understanding Language Features, covers C++ statements and expressions, constants, variables, operators, and how to control execution flow in applications.

Chapter 2Working with Memory, Arrays, and Pointers, covers how memory is allocated and used in C++ applications, how to use built-in arrays, the role of C++ references, and how to use C++ pointers to access memory.

Chapter 3Using Functions, explains how to define functions, how to pass parameters-by-reference and by-value using a variable number of parameters, creating and using pointers to functions, and defining template functions and overloaded operators.

Chapter 4Classes, describes how to define new types through classes and the various special functions used in a class, how to instantiate a class as an object and how to destroy them, and how to access objects through pointers and how to write template classes.

Chapter 5Using Standard Library Containers, covers all the C++ Standard Library container classes and how to use them with iterators and the standard algorithms so that you can manipulate the data in containers.

Chapter 6Using Strings, describes the features of the standard C++ string class, converting between numeric data and strings, internationalizing strings, and using regular expressions to search and manipulate strings.

Chapter 7Diagnostics and Debugging, explains how to prepare your code to provide diagnostics and to enable it to be debugged, how applications are terminated, abruptly or gracefully, and how to use C++ exceptions.

Chapter 8Learning Modern Core Language Features, teaches you about modern core language features including type inference, uniform initialization, scoped enums, range-based for loops, structured bindings, and others.

Chapter 9Working with Numbers and Strings, discusses how to convert between numbers and strings, generate pseudo-random numbers, work with regular expressions, and various types of string.

Chapter 10Exploring Functions, dives into defaulted and deleted functions, variadic templates, lambda expressions, and higher-order functions.

Chapter 11Standard Library Containers, Algorithms, and Iterators, introduces you to several standard containers, many algorithms, and teaches you how to write your own random access iterator.

Chapter 12Math Problems, contains a series of math exercises to warm you up for the more challenging problems in the next chapters.

Chapter 13Language Features, proposes problems for you to practice operator overloading, move semantics, user-defined literals, and template metaprogramming aspects such as variadic functions, fold expressions, and type traits.

Chapter 14Strings and Regular Expressions, has several problems for string manipulation, such as converting between strings and other data types, splitting and joining strings, and also for working with regular expressions.

Chapter 15Streams and Filesystems, covers output stream manipulation and working with files and directories using the C++17 filesystem library.

 

Chapter 16Date and Time, prepares you for the upcoming C++20 extensions to the chrono library, with several calendar and time zone problems that you can solve with the date library, on which the new standard additions are based.

Chapter 17Algorithms and Data Structures, is one of the largest chapters and contains a variety of problems where you need to utilize the existing standard algorithms; others are where you need to implement your own general-purpose algorithms or data structures, such as circular buffer and priority queue. The chapter ends with two rather fun problems, Dawkins' Weasel program and Conway's Game of Life program, where you can learn about evolutionary algorithms and cellular automata.

To Get the Most out of This Book

The readers should be equipped with the following configurations of the environment:

  1. C++11 (Intel, IBM, Sun, Apple, and Microsoft, as well as the open source GCC)
  2. Visual C++ 2017 Community Edition
  3. VC++ 2017 on Windows
  4. GCC 7.0 or Clang 5.0 on Linux and Mac

If you don't have the latest version of the compiler, or you want to try another compiler, you can use one that is available online. Although there are various online platforms that you could use, I recommend https://wandbox.org/ for GCC and Clang and http://webcompiler.cloudapp.net/ for VC++.

While working with a compiler with C++17 support, you will need a complete list of required libraries.

How to generate projects for Visual Studio 2017

Do the following in order to generate Visual Studio 2017 projects to target the x86 platform:

  1. Open a command prompt and go to the build directory in the source code root folder.
  2. Execute the following CMake command:cmake -G "Visual Studio 15 2017" .. -DCMAKE_USE_WINSSL=ON -DCURL_WINDOWS_SSPI=ON -DCURL_LIBRARY=libcurl -DCURL_INCLUDE_DIR=..\libs\curl\include -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF
  1. After completion, the Visual Studio solution can be found at build/cppchallenger.sln.

If you want to target the x64 platform instead, use the generator called "Visual Studio 15 2017 Win64". Visual Studio 2017 15.4 supports both filesystem (as an experimental library) and std::optional. If you use a previous version, or just want to use the Boost libraries instead, you can generate the projects using the following command, after you properly install Boost:

cmake -G "Visual Studio 15 2017" .. -DCMAKE_USE_WINSSL=ON -DCURL_WINDOWS_SSPI=ON -DCURL_LIBRARY=libcurl -DCURL_INCLUDE_DIR=..\libs\curl\include -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF -DBOOST_FILESYSTEM=ON -DBOOST_OPTIONAL=ON -DBOOST_INCLUDE_DIR=<path_to_headers> -DBOOST_LIB_DIR=<path_to_libs>

Make sure that the paths to the headers and static library files do not include trailing backslashes (i.e. \).

Download the Example Code Files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

 

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Modern-C-plus-plus-Efficient-and-Scalable-Application-Development. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/Cplusplus_Efficient_and_Scalable_Application_Development.pdf

Conventions Used

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The writer intended to type c = a + 8 / b + 1; and : they pressed comma instead of a /."

A block of code is set as follows:

inline auto mult(int lhs, int rhs) -> int 
    { 
        return lhs * rhs; 
    }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

if (op == ',' || op == '.' || op < '+' || op > '/')
    { 
        cout << endl << "operator not recognized" << endl; 
        usage(); 
        return 1; 
    }

Any command-line input or output is written as follows:

C:\Beginning_C++Chapter_02\cl /EHsc calc.cpp

Bold: New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The calling convention of the function determines whether the calling function or the called function has the responsibility to do this."

 

 

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in Touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.