logo资料库

Expert C++: Become a proficient programmer by learning coding best practices.pdf

第1页 / 共591页
第2页 / 共591页
第3页 / 共591页
第4页 / 共591页
第5页 / 共591页
第6页 / 共591页
第7页 / 共591页
第8页 / 共591页
资料共591页,剩余部分请下载后查看
Cover
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Table of Contents
Preface
Section 1: Under the Hood of C++ Programming
Chapter 1: Introduction to Building C++ Applications
Technical requirements
Introduction to C++20
Concepts
Coroutines
Ranges
More C++20 features
Building and running programs
Understanding preprocessing
Header files
Using modules in C++20
Understanding Compiling
Tokenization
Syntax analysis
Semantic analysis
Intermediate code generation
Optimization
Machine code generation
Platforms and object files
Introducing Linking
Linking libraries
Summary
Questions
Further reading
Chapter 2: Low-Level Programming with C++
Technical requirements
Program execution
main()
Special properties of main()
constexpr
Recursion
Working with data
Virtual memory
Addressing
Data types
Pointers
Memory segments
The heap
Arrays
Control flow
Conditionals
The switch statement
Replacing conditionals with function pointers
Functions as types
Loops
Summary
Questions
Further reading
Chapter 3: Details of Object-Oriented Programming
Technical requirements
Understanding objects
Low-level details of objects
High-level details of objects
State
Identity
Behavior
Mimicking a class
Working with classes
Classes from the compiler perspective
Initialization and destruction
Copying objects
Moving objects
Lvalue references
Rvalue references
Notes on operator overloading
Encapsulation and the public interface
Structs in C++
Class relationships
Aggregation and composition
Inheritance
Inheritance from the compiler perspective
Composition versus inheritance
Protected inheritance
Polymorphism
Virtual functions under the hood
Design patterns
Summary
Questions
Further reading
Chapter 4: Understanding and Designing Templates
Technical requirements
Exploring function and class templates
Motivation
Function templates
Syntax
Instantiation
Explicit instantiations
Implicit instantiations
Deduction
Specialization and overloading
Class templates
Syntax
Instantiation
Explicit instantiation
Implicit instantiation
Specialization
Understanding variadic templates
Syntax
Examples
Exploring template parameters and arguments
Template parameters
Non-type template parameter
Type template parameter
Template template parameter
Template arguments
Template non-type arguments
Template type arguments
Template template arguments
Default template arguments
Exploring traits
Type trait implementation
boost::is_void
boost::is_pointer
Optimizing algorithms using traits
Exploring template metaprogramming
Compile-time computation
Compile-time code optimization
Static polymorphism
Summary
Questions
Further reading
Chapter 5: Memory Management and Smart Pointers
Technical requirements
Understanding computer memory
Designing a memory storage device
Understanding computer memory from a higher-level perspective
Registers
Cache memory
Main memory
Permanent storage
The basics of memory management
An example of memory management
Using smart pointers
Leveraging the RAII idiom
std::unique_ptr
std::shared_ptr and std::weak_ptr
Garbage collection
Using allocators
Summary
Questions
Further reading
Section 2: Designing Robust and Efficient Applications
Chapter 6: Digging into Data Structures and Algorithms in STL
Technical requirements
Data structures
Sequential data structures
Node-based data structures
Containers in memory
STL containers
Using std::vector and std::list
Using container adapters
Iterating containers
Concepts and iterators
Understanding concepts 
Using iterators in C++20
Mastering algorithms
Searching
Binary search
Sorting
Exploring trees and graphs
Hash tables
Graphs
Strings
Summary
Questions
Further reading
Chapter 7: Functional Programming
Technical requirements
Unveiling functional programming
Using ranges
Why use functional programming?
Principles of functional programming
Pure functions
Higher-order functions
Folding
Diving deeper into recursion
Head recursion
Tail recursion
Metaprogramming in functional C++
Summary
Questions
Further reading
Chapter 8: Concurrency and Multithreading
Technical requirements
Understanding concurrency and multithreading
Processes
Challenges with processes
Threads
Working with threads
Waiting for threads
Using std::jthread
Passing arguments to the thread function
Managing threads and sharing data
Sharing data
Protecting shared data using a mutex
Avoiding deadlocks
Designing concurrent code
Introducing coroutines
Summary
Questions
Further reading
Chapter 9: Designing Concurrent Data Structures
Technical requirements
A closer look at data races
A synchronized increment
Implementing a thread-safe stack
Designing lock-free data structures
Using atomic types
Operations on atomic types
Designing a lock-free stack
More operations on atomics
Summary
Questions
Further reading
Chapter 10: Designing World-Ready Applications
Technical requirements
Project development life cycle
Requirements gathering and analysis
Specification creation
Design and test planning
Decomposing entities
Coding
Testing and stabilization
Release and maintenance
Diving into the design process
Using SOLID principles
The single responsibility principle
The open-closed principle
The Liskov substitution principle
The interface segregation principle
The dependency inversion principle
Using domain-driven design
Leveraging design patterns
The repository pattern
The factory pattern
Summary
Questions
Further reading
Chapter 11: Designing a Strategy Game Using Design Patterns
Technical requirements
Introduction to game design
Introduction to the Readers and Disturbers game
Strategy game components
Interaction between the components
Designing the game
Designing character units
Designing buildings
Designing game controllers
Concurrent actions
The game event loop
Using design patterns
The Command pattern
The Observer pattern
The Flyweight pattern
The Prototype pattern
Designing the game loop
Summary
Questions
Further reading
Chapter 12: Networking and Security
Technical requirements
Discovering network programming in C++
Network applications under the hood
Programming network applications using sockets
Network protocols
Designing a network application
Using POSIX sockets
Implementing a POSIX socket wrapper class
Securing the C++ code
Securing network applications
Summary
Questions
Further reading
Chapter 13: Debugging and Testing
Technical requirements
Understanding the root cause of an issue
The RCA overview
Prevention is better than the cure – a good coding behavior
Uninitialized variable problem
Side effects in compound expressions
Mixing signed and unsigned problems
Order of evaluation problem
Compile-time checking versus runtime checking
Avoiding memory leaks
Debugging C++ programs
Tools to debug a C/C++ program
GDB overview
Examples of GDB
Setting breakpoints and inspection variable values
Function breakpoints, conditional breakpoints,  watchpoint, and the continue and finish commands
Logging gdb into a text file
Practical debugging strategies
Understanding static and dynamic analysis
Static analysis
Dynamic analysis
Exploring unit testing, TDD, and BDD
Unit testing
TDD
Example of TDD
Step 1 – writing a failing test
Step 2 – developing code to let the test pass
Step 3 – refactoring
BDD
Summary
Further reading
Exercises and questions
Chapter 14: Graphical User Interface with Qt
Technical requirements
Understanding cross-platform GUI programming
Using C++ as Java
Qt's cross-platform model
Writing a simple application
Discovering Qt
Grasping signals and slots
Understanding Model/View programming
Using Qt widgets
Common Qt widgets
Composing widgets using layouts
Summary
Questions
Further reading
Section 3: C++ in the AI World
Chapter 15: Using C++ in Machine Learning Tasks
Technical requirements
Introduction to AI
Computer vision
NLP
Knowledge reasoning
ML
Understanding ML
Designing an algorithm that learns
Categories of ML
Applications of ML
Neural networks
Clustering
Regression analysis
C++ and ML
Summary
Questions
Further reading
Chapter 16: Implementing a Dialog-Based Search Engine
Technical requirements
Understanding the structure of a search engine
Providing a convenient user interface
Dealing with typos in queries
Crawling websites
Indexing documents
Tokenizing documents
Sorting the results
Building a recommendation engine
Using a knowledge graph
Implementing a dialog-based search engine
Implementing the query parser
Implementing the query processor
Summary
Questions
Further reading
Assessments
Other Books You May Enjoy
Index
Expert C++ Become a proficient programmer by learning coding best practices with C++17 and C++20's latest features Vardan Grigoryan Shunguang Wu BIRMINGHAM - MUMBAI
Expert C++ Copyright © 2020 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Richa Tripathi Acquisition Editor: Denim Pinto Content Development Editor: Pathikrit Roy Senior Editor: Rohit Singh Technical Editor: Romy Dias Copy Editor: Safis Editing Project Coordinator: Francy Puthiry Proofreader: Safis Editing Indexer: Tejal Daruwale Soni Production Designer: Alishon Mendonsa First published: April 2020 Production reference: 1100420 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-83855-265-7 www.packt.com
To my mother, Karine, and to my little princess, Leia, for their encouragement and support. To my wife, Wen, and to my sons, Justin and Zachary. - Shunguang Wu – Vardan Grigoryan
Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Fully searchable for easy access to vital information Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors About the authors Vardan Grigoryan is a senior backend engineer and C++ developer with more than 9 years of experience. Vardan started his career as a C++ developer and then moved to the world of server-side backend development. While being involved in designing scalable backend architectures, he always tries to incorporate the use of C++ in critical sections that require the fastest execution time. Vardan loves tackling computer systems and program structures on a deeper level. He believes that true excellence in programming can be achieved by means of a detailed analysis of existing solutions and by designing complex systems. Shunguang Wu is a senior professional staff at Johns Hopkins University Applied Physics Laboratory, and received his PhDs in theoretical physics and electrical engineering from Northwestern University (China) and Wright State University (USA), respectively. He published about 50 reviewed journal papers in the area of nonlinear dynamics, statistical signal processing and computer vision in his early career. His professional C++ experience started with teaching undergraduate courses in the late 1990s. Since then he has been designing and developing lots of R&D and end-user application software using C++ in world-class academic and industrial laboratories. These projects span both the Windows and Linux platforms.
About the reviewers Lou Mauget learned to program long ago at Michigan State University as a physics major, learning to use software in designing a cyclotron. Afterward, he worked for 34 years at IBM. He is currently consulting for Keyhole Software of Leawood, Kansas. Lou has coded in C++, Java, JavaScript, Python, and newer languages, as each was conceived. Current interests include reactive functional programming, containers, Node.js, NoSQL, geospatial systems, mobile, and any new language or framework. He has coauthored three computer books. He has written two IBM DeveloperWorks XML tutorials and co-written several J2EE certification tests for IBM. He has been a reviewer for Packt Publishing and others. Scott Hutchinson leads a team of C++ and F# developers in Oxnard, California. After a few years as a VB/VBA developer, he started developing with .NET Framework immediately after its launch in 2002. Since 2016, he has done most of his development in C++. He is a mentor for the F# track on Exercism, and teaches functional programming in F# to his team at work. His main professional interests are functional programming and machine learning. When he's not learning some new software development skill, he's usually hiking in the mountains of Southern California. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents Preface Section 1: Section 1: Under the Hood of C++ Programming Chapter 1: Introduction to Building C++ Applications 1 8 9 9 9 9 10 11 12 13 15 18 20 20 21 21 22 22 23 26 27 28 29 29 29 30 31 31 31 34 36 38 42 43 45 46 Technical requirements Introduction to C++20 Concepts Coroutines Ranges More C++20 features Building and running programs Understanding preprocessing Header files Using modules in C++20 Understanding Compiling Tokenization Syntax analysis Semantic analysis Intermediate code generation Optimization Machine code generation Platforms and object files Linking libraries Introducing Linking Summary Questions Further reading Chapter 2: Low-Level Programming with C++ Technical requirements Program execution main() Special properties of main() constexpr Recursion Working with data Virtual memory Addressing Data types
分享到:
收藏