O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering C++ Programming

Book Description

Take your C++ coding to the next level by leveraging the latest features and advanced techniques to building high performing, reliable applications.

About This Book

  • Get acquainted with the latest features in C++ 17
  • Take advantage of the myriad of features and possibilities that C++ offers to build real-world applications
  • Write clear and expressive code in C++, and get insights into how to keep your code error-free

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

What You Will Learn

  • Write modular C++ applications in terms of the existing and newly introduced features
  • Identify code-smells, clean up, and refactor legacy C++ applications
  • Leverage the possibilities provided by Cucumber and Google Test/Mock to automate test cases
  • Test frameworks with C++
  • Get acquainted with the new C++17 features
  • Develop GUI applications in C++
  • Build portable cross-platform applications using standard C++ features

In Detail

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.

Style and approach

This straightforward guide will help you level up your skills in C++ programming, be it for enterprise software or for low-latency applications like games. Filled with real-world, practical examples, this book will take you gradually up the steep learning curve that is C++.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. C++17 Features
    1. C++17 background
    2. What's new in C++17?
      1. What features are deprecated or removed in C++17?
    3. Key features in C++17
      1. Easier nested namespace syntax
      2. New rules for type auto-detection from braced initializer list 
      3. Simplified static_assert 
      4. The std::invoke( ) method
      5. Structured binding
      6. If and Switch local scoped variables
      7. Template type auto-deduction for class templates
      8. Inline variables
    4. Summary
  3. Standard Template Library
    1. The Standard Template Library architecture
      1. Algorithms
      2. Iterators
      3. Containers
      4. Functors
    2. Sequence containers
      1. Array
        1. Code walkthrough
        2. Commonly used APIs in an array
      2. Vector 
        1. Code walkthrough
        2. Commonly used vector APIs
        3. Code walkthrough
        4. Pitfalls of a vector
      3. List 
        1. Commonly used APIs in a list
      4. Forward list
        1. Code walkthrough
        2. Commonly used APIs in a forward_list container
      5. Deque
        1. Commonly used APIs in a deque
    3. Associative containers
      1. Set
        1. Code walkthrough
        2. Commonly used APIs in a set
      2. Map
        1. Code walkthrough
        2. Commonly used APIs in a map
      3. Multiset
      4. Multimap
      5. Unordered sets
      6. Unordered maps
      7. Unordered multisets
      8. Unordered multimaps
    4. Container adapters
      1. Stack
        1. Commonly used APIs in a stack
      2. Queue
        1. Commonly used APIs in a queue
      3. Priority queue
        1. Commonly used APIs in a priority queue
    5. Summary
  4. Template Programming
    1. Generic programming
      1. Function templates
        1. Code walkthrough
      2. Overloading function templates
        1. Code walkthrough
      3. Class template
        1. Code walkthrough
      4. Explicit class specializations
        1. Code walkthrough
      5. Partial template specialization
    2. Summary
  5. Smart Pointers
    1. Memory management
    2. Issues with raw pointers
    3. Smart pointers
      1. auto_ptr
        1. Code walkthrough - Part 1
        2. Code walkthrough - Part 2
      2. unique_ptr
        1. Code walkthrough
      3. shared_ptr
        1. Code walkthrough
      4. weak_ptr
        1. Circular dependency
    4. Summary
  6. Developing GUI Applications in C++
    1. Qt 
      1. Installing Qt 5.7.0 in Ubuntu 16.04
    2. Qt Core
      1. Writing our first Qt console application
    3. Qt Widgets
      1. Writing our first Qt GUI application
    4. Layouts
      1. Writing a GUI application with a horizontal layout
      2. Writing a GUI application with a vertical layout
      3. Writing a GUI application with a box layout
      4. Writing a GUI application with a grid layout
    5. Signals and slots
    6. Using stacked layout in Qt applications
      1. Writing a simple math application combining multiple layouts
    7. Summary
  7. Multithreaded Programming and Inter-Process Communication
    1. Introduction to POSIX pthreads
    2. Creating threads with the pthreads library
      1. How to compile and run
    3. Does C++ support threads natively?
    4. How to write a multithreaded application using the native C++ thread feature
      1. How to compile and run
    5. Using std::thread in an object-oriented fashion
      1. How to compile and run
        1. What did you learn?
    6. Synchronizing threads
      1. What would happen if threads weren't synchronized?
      2. How to compile and run
      3. Let's use mutex
      4. How to compile and run
      5. What is a deadlock?
      6. How to compile and run
        1. What did you learn?
      7. Shared mutex
      8. Conditional variable
      9. How to compile and run
        1. What did you learn?
      10. Semaphore
    7. Concurrency
      1. How to compile and run
      2. Asynchronous message passing using the concurrency support library
        1. How to compile and run
      3. Concurrency tasks
        1. How to compile and run
      4. Using tasks with a thread support library
        1. How to compile and run
      5. Binding the thread procedure and its input to packaged_task 
        1. How to compile and run
      6. Exception handling with the concurrency library
        1. How to compile and run
        2. What did you learn?
    8. Summary
  8. Test-Driven Development
    1. TDD
    2. Common myths and questions around TDD
      1. Does it take more efforts for a developer to write a unit test? 
      2. Is code coverage metrics good or bad?
      3. Does TDD work for complex legacy projects? 
      4. Is TDD even applicable for embedded or products that involve hardware?
    3. Unit testing frameworks for C++
    4. Google test framework
      1. Installing Google test framework on Ubuntu
      2. How to build google test and mock together as one single static library without installing?
      3. Writing our first test case using the Google test framework
      4. Using Google test framework in Visual Studio IDE
    5. TDD in action
      1. Testing a piece of legacy code that has dependency
    6. Summary
  9. Behavior-Driven Development
    1. Behavior-driven development
    2. TDD versus BDD
    3. C++ BDD frameworks
    4. The Gherkin language
    5. Installing cucumber-cpp in Ubuntu
      1. Installing the cucumber-cpp framework prerequisite software
      2. Building and executing the test cases
    6. Feature file
    7. Spoken languages supported by Gherkin
    8. The recommended cucumber-cpp project folder structure
    9. Writing our first Cucumber test case
      1. Integrating our project in cucumber-cpp CMakeLists.txt
      2. Executing our test case
    10. Dry running your cucumber test cases
    11. BDD - a test-first development approach
      1. Let's build and run our BDD test case
      2. It's testing time!
    12. Summary
  10. Debugging Techniques
    1. Effective debugging
    2. Debugging strategies
    3. Debugging tools
      1. Debugging your application using GDB
        1. GDB commands quick reference
      2. Debugging memory leaks with Valgrind
        1. The Memcheck tool
          1. Detecting memory access outside the boundary of an array
          2. Detecting memory access to already released memory locations
          3. Detecting uninitialized memory access
          4. Detecting memory leaks
          5. Fixing the memory leaks
          6. Mismatched use of new and free or malloc and delete
    4. Summary
  11. Code Smells and Clean Code Practices
    1. Code refactoring
    2. Code smell
    3. What is agile?
    4. SOLID design principle
      1. Single responsibility principle
      2. Open closed principle
      3. Liskov substitution principle
      4. Interface segregation
      5. Dependency inversion
    5. Code smell
      1. Comment smell
      2. Long method
      3. Long parameter list
      4. Duplicate code
      5. Conditional complexity
      6. Large class
      7. Dead code
      8. Primitive obsession
      9. Data class
      10. Feature envy
    6. Summary