about
10/16/2022
C++ Story Prologue
C++ Story Prologue
starting, story content, references
0.0 Prologue
Why C++ ?
- It has excellent performance, due in part to the fact that it compiles to native machine code rather than to bytecode interpreted by a virtual machine.
- C++ is very expressive. Its operator syntax, use of aliases, and syntactic separation of interface from implementation, allow a designer to express design intents very clearly.
- The language supports class designs that allow users to be oblivious of the acquisition and handling of resources. Its standard libraries often make a lot of that almost automatic.
- It provides direct control over the acquisition and release of resources, and those actions are deterministic. The designer has a lot of control over the release of resources that are shared within a process, like file handles.
- It is the only modern language that supports creating user-defined value behavior with classes that may also participate in polymorphic class hierarchies. Value types are copyable and assignable. Many C++ classes are value types by default. For those that are not, the language provides constructs to support that behavior, e.g., copy constructors and overloadable assignment operators.
- C++ is still evolving to become more efficient, designer-friendly, and provide more artifacts that support safe, reliable operation, like the standard smart pointers, move operations, and better type deduction. Its current standard is C++20 and the next, C++23, is moving toward completion.
Other Languages:
-
Virtual machines provide services that are available for all programs that run in
that environment.
- Reflection
- Exception handling
- Garbage Collection
- Interoperation with platform services
- When multiple languages use the same virtual machine, interoperation between them becomes significantly simpler, because they use the same platform abstraction.
- Performance is slower because bytecode runs in a stack-based platform abstraction layer and because garbage collection requires background processing to track references and destroy instances that are no longer targets for program references.
- All instances of classes are reference types meaning they are stored in the managed heap and have non-deterministic life times. When types share system resources like files and database connections, it is important to release the resources as quickly as possible so they are available for other code. Garbage collection can make timely release uncertain1. Classes that implement IDispose support the Dispose method which does provide a deterministic release of resources, but unfortunately every user of a Disposable type has to remember to call dispose().
- Reference types are not inherently copyable2. That means that the designer may have to provide a clone function that creates an instance of the type to be cloned and then copies source elements into the new instance. That only works if the elements are value types or clonable objects like strings.
- Garbage collection works surprisingly well most of the time, but most implementations are tiered. C# garbage collection for instances in the second tier runs once for every ten times the first tier is collected. The third tier collection runs once for every ten times the second tier is collected. Large instances and instances with long lifetimes are likely to migrate to higher tiers and take significantly longer to release their resources.
- The default performance of Object.MemberwiseClone() does not clone. It simpy copies the instance handle, meaning that there is only one underlying instance with an additional reference. In order to make a genuine clone the designer has to override MemberwiseClone() and provide the cloning semantics described above.
0.1 Getting Started
-
Basic C++ Knowledge:
This story provides a lot of what you need, along with code examples in the CppBasicDemos repository. Also see the references at the end of this page. -
C++ Tool Chain:
If you are working in the Windows environment, download Visual Studio Community Edition - 2019 and look at this tutorial. All of the C++ code in the C++ repositories was built with Visual Studio 2019. If you work in other platforms, install gcc, g++, and libc using the local package manager, e.g., sudo apt-get install gcc . Then install Visual Studio Code and look at this VS Code tutorial. -
Now start:
Download the CppBasicDemos and open by double-clicking the solution - *.sln - in any of the folders that seem interesting.
0.2 Story Content
-
Prologue
Motivation for, and layout of, C++ story. -
C++ Models
Key concepts and background information needed for the story. -
Survey
Fairly brief discussion of each of the major parts of the C++ language. -
Data
Focus on C++ Types, their values, and initialization. -
Operations
Discusses functions, methods, functors, lambdas, and related topics. -
Classes
Covers class details: methods, special operations, examples, value types, and resource management. -
Class Relationships
C++ relationships: inheritance, composition, aggregation, using, and friend-ship. -
Templates
Discusses generation of functions and classes with templates. Includes discussion of function overloading and class specialization. -
Template Metaprogramming
Writing code that affects compile-time construction of functions and classes from templates. -
Libraries
Summary of the C++ libraries and a few third-party libraries. -
Streams Library
Discusses streams for interacting with the console, files, and strings. -
STL Library
Covers the structure of the STL libraries and how some of them are used. -
Interesting
Mentions a few topics and provides links to interesting ideas and methods.
0.3 References
Reference | Description |
---|---|
Cplusplus.com | Fairly simple and complete language tutorial. |
w3schools.com | Quick tutorial with examples. |
tutorialspoint.com | Fairly quick with examples. |
learncpp.com | Lots of details. |
beginnersbook.com | gentle introduction |
isocpp.org | Frequently asked questions - covers a lot of territory. |
cppreference.com | This is a (very good) reference, not a tutorial. I use it frequently. |
Visual Studio IDE |
Microsoft tutorial on Visual Studio 2019 IDE |