Book Image

Mastering C++ Programming

By : Jeganathan Swaminathan
Book Image

Mastering C++ Programming

By: Jeganathan Swaminathan

Overview of this book

C++ ? ?has ? ?come ? ?a ? ?long ? ?way ? ?and ? ?has ? ?now ? ?been ? ?adopted ? ?in ? ?several ? ?contexts. Its ? ?key ? ?strengths ? ?are ? ?its ? ?software ? ?infrastructure ? ?and ? ?resource-constrained applications. ? ?The ?C++ ? ?17 ? ?release ? ?will ? ?change ? ?the ? ?way ? ?developers ? ?write code, ? ?and ? ?this ? ?book ? ?will ? ?help ?you ? ?master ? ?your ? ?developing ? ?skills ? ?with ? ?C++. With ? ?real-world, ? ?practical ? ?examples ? ?explaining ? ?each ? ?concept, ? ?the ? ?book ? ?will begin ? ?by ? ?introducing ? ?you ? ?to ? ?the ? ?latest ? ?features ? ?in ? ?C++ ? ?17. ? ?It ? ?encourages clean ? ?code ? ?practices ? ?in ? ?C++ ? ?in ? ?general, ? ?and ? ?demonstrates ? ?the ? ?GUI app-development ? ?options ? ?in ? ?C++. ? ?You’ll ? ?get ? ?tips ? ?on ? ?avoiding ? ?memory ? ?leaks using ? ?smart-pointers. ? ?Next, ? ?you’ll ? ?see ? ?how ? ?multi-threaded ?programming can ? ?help ? ?you ? ?achieve ? ?concurrency ? ?in ? ?your ? ?applications. Moving ? ?on, ? ?you’ll ? ?get ? ?an ? ?in-depth ? ?understanding ? ?of ? ?the ? ?C++ ? ?Standard Template ? ?Library. ? ?We ? ?show ? ?you ? ?the ? ?concepts ? ?of ? ?implementing ? ?TDD ? ?and BDD ? ?in ? ?your ? ?C++ ? ?programs, ? ?and ? ?explore ? ?template-based ? ?generic programming, ? ?giving ? ?you ? ?the ? ?expertise ? ?to ? ?build ? ?powerful ? ?applications. Finally, ? ?we’ll ? ?round ? ?up ? ?with ? ?debugging ? ?techniques ? ?and ? ?best ? ?practices.By ? ?the ? ?end ? ?of ? ?the ? ?book, ? ?you’ll ? ?have ? ?an ? ?in-depth ? ?understanding ? ?of ? ?the language ? ?and ? ?its ? ?various ? ?facets.
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface

Preface

C++ is an interesting programming language that has been around for almost three decades now. It is used to develop complex desktop applications, web applications, networking applications, device drivers, kernel modules, embedded applications, and GUI applications using third-party widget frameworks; literally speaking, C++ can be used in any domain.

Ever since I started programming in 1993, I have cherished the good old technical discussions that I had with many of my colleagues and industry experts that I met from time to time. Of all the technical discussions, one topic gets repeated time and again, which is, "Do you think C++ is a relevant programming language today? Should I continue working on C++ or should I move on to other modern programming languages, such as Java, C#, Scala, or Angular/Node.js?"

I have always felt that one should be open to learning other technologies, but that doesn’t mean having to give up on C++. However, the good news is that with the new C++17 features in place, C++ has been reborn and it is going to stay and rock for many more decades, which is my motivation to write this book.

People have always felt that Java will take over C++, but it has continued to stay. The same discussion started again when C# came into the industry and today again when Angular/Node.js and Scala seem to be more attractive for rapid programming. However, C++ has its own, place and no programming language has been able to take over the place of C++ so far.

There are already many C++ books that help you understand the language, but there are very few books that address developing GUI applications in C++, TDD with C++, and BDD with C++.

C++ has come a long way and has now been adopted in several contexts. Its key strengths are its software infrastructure and resource-constrained applications. The C++ 17 release will change the way developers write code, and this book will help you master your developing skills with C++.

With real-world, practical examples explaining each concept, the book will begin by introducing you to the latest features of C++ 17. It will encourage clean code practices in C++, and demonstrate GUI app development options in C++. You will gain insights into how to avoid memory leaks using smart pointers. Next, you will learn how multithreaded programming can help you achieve concurrency in your applications.

Moving on, you’ll also get an in-depth understanding of the C++ Standard Template Library. We'll explain the concepts of implementing TDD and BDD in your C++ programs, along with template-based generic programming, to equip you with the expertise to build powerful applications. Finally, we’ll round the book off with debugging techniques and best practices. By the time you reach the end of the book, you will have an in-depth understanding of the language and its various facets.

What this book covers

Chapter 1, C++17 Features, explains the new features of C++17 and features that have been removed. It also demonstrates key C++17 features with easy-to-understand examples.

Chapter 2, Standard Template Library, gives an overview of STL, demonstrates various containers and iterators, and explains how to apply useful algorithms on the containers. The chapter also touches on the internal data structures used and their runtime efficiencies.

Chapter 3, Template Programming, gives an overview of generic programming and its benefits. It demonstrates writing function templates and class templates, and overloading function templates. It also touches upon writing generic classes, explicit class specializations, and partial specializations.

Chapter 4, Smart Pointers, explains the issues of using raw pointers and motivates the use of smart pointers. Gradually, this chapter introduces you to the usage of auto_ptr, unique_ptr, shared_ptr, and weak_ptr, and explains ways to resolve cyclic dependency issues.

Chapter 5, Developing GUI Applications in C++, gives an overview of Qt and provides you with step-by-step instructions to install Qt on Linux and Windows. The chapter gradually helps you develop impressive GUI applications with interesting widgets and various layouts. 

Chapter 6, Multithreaded Programming and Inter-Process Communication, introduces to the POSIX pthreads library and discusses the native C++ thread library. It also discusses the benefits of using the C++ thread library. Later, it helps you write multithreaded applications, explores ways to manage the threads, and explains the use of synchronization mechanisms. The chapter discusses deadlocks and possible solutions. Toward the end of the chapter, it introduces you to the concurrency library.

Chapter 7, Test-Driven Development, gives a brief overview of TDD and clarifies FAQs on TDD. This chapter provides you with step-by-step instructions to install Google test framework and integrate it with the Linux and Windows platforms. It helps you develop applications using TDD with an easy-to-understand tutorial style.

Chapter 8, Behavior-Driven Development, gives an overview of BDD and guides you through the installation, integration, and configuration of the Cucumber framework on Linux platforms. It also explains Gherkin and helps you write BDD test cases.

Chapter 9, Debugging Techniques, discusses the various strategies and techniques followed in the industry for debugging your application problems. Later, it helps you understand the use of the GDB and Valgrind tools for step by step debugging, watching variables, fixing various memory-related issues, including memory leaks.

Chapter 10, Code Smells and Clean Code Practices, discusses various code smells and refactoring techniques.

What you need for this book

You will need to be equipped with the following tools before you get started with the book:

  • g++ compiler of version 5.4.0 20160609 or above
  • GDB 7.11.1
  • Valgrind 3.11.0
  • Cucumber-cpp Git 2.7.4
  • Google test framework (gtest 1.6 or later)
  • CMake 3.5.1
  • Ruby 2.5.1
  • Qt 5.7.0
  • Bundler v 1.14.6

The OS required is Ubuntu 16.04 64-bit or later. The hardware configuration should at least be of 1 GB RAM and 20 GB hard drive space. A virtual machine with this configuration should also suffice.

Who this book is for

This ​book ​is ​for ​experienced ​C++ ​developers. ​If ​you ​are ​a ​novice ​C++ developer, ​then ​it’s ​ ​highly ​recommended ​that ​you ​get ​a ​solid understanding ​of ​the ​C++ ​language ​before reading ​this ​book.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. 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 initialize() method initializes the deque iterator pos to the first data element stored within deque."

A block of code is set as follows:

#include <iostream>

int main ( ) {

        const int x = 5, y = 5;

        static_assert ( 1 == 0, "Assertion failed" );
        static_assert ( 1 == 0 );
        static_assert ( x == y );

        return 0;
}

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

#include <iostream>
#include <thread>
#include <mutex>
#include "Account.h"
using namespace std;

enum ThreadType {
  DEPOSITOR,
  WITHDRAWER
};

mutex locker;

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

g++ main.cpp -std=c++17
./a.out

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: " You need to create a new project namedMathAppby navigating toNew Project | Visual Studio | Windows | Win32 | Win32 Console Application."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.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 to our website using your email address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

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/Mastering-Cpp-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

 

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.