S R T B H P N

C++ Story Prologue

Why C++?, What's covered?

Chaps  Sects  close 
Prologue
Models
Survey
Data
Operations
Classes
ClassRel
Templates
TMP
Libraries
Streams Libraries
STL Libraries
Interesting
Top, Prologue, Contents, References

0Prologue: The C++ programming language is a widely used, and in some ways elegant, programming language.
C++ is Compelling:
  • 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++17 and the next, C++20, is moving toward completion.
This story is intended to help you get started or refresh your knowledge of C++. Most other languages, like C# and Java, compile to bytecode that runs in a virtual machine - CLR for C# and JVM for Java. There are both benefits and deficits of using virtual machines:
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.

  1. 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.
  2. 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.
My preferred languages are C++ and Rust, but frequently use other languages, especially C#, when working on a project that needs services provided by those languages. When building graphical user interfaces or communication processing I often use C# to get access to Windows Presentaton Foundation (WPF) and Windows Communication Foundation (WCF). Now that .Net Core is available, WPF can be used on non-Windows platforms as well. WCF is not available other than on Windows, but there is a third party framework available for hosting IPC services on the platforms supported by .Net Core, e.g., Windows, Linux, and MacOS.
0.1Getting Started: To get started, you need: basic knowledge of the language, a tool chain to build programs, and the will to start building with much less than perfect knowledge.
  1. 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.
  2. 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.
  3. Now start:
    Download the CppBasicDemos and open by double-clicking the solution - *.sln - in any of the folders that seem interesting.

0.2Story Content: This is a story about the C++ programming language. It orders content from this site into a sequence of chapters about the C++ programming language. Each chapter, presented as a single web page, is available from the Chaps dropup at the lower left of each story page, including this one. All code examples in this story are available as Visual Studio projects - 57 of them. You can find the code in CppStory Repository. Each project needs to have the C++17 option set. I've done that so you shouldn't have to, but if compilation fails, set the option on the opening page of the properties sheet for the project. The easiest way to access the code is to download the repository, open resulting zip file, and extract all. That will give you a single solution for all code and also a solution for each chapter, found in the chapter folders. Chapters:
  1. Prologue
    Motivation for, and layout of, C++ story.
  2. C++ Models
    Key concepts and background information needed for the story.
  3. Survey
    Fairly brief discussion of each of the major parts of the C++ language.
  4. Data
    Focus on C++ Types, their values, and initialization.
  5. Operations
    Discusses functions, methods, functors, lambdas, and related topics.
  6. Classes
    Covers class details: methods, special operations, examples, value types, and resource management.
  7. Class Relationships
    C++ relationships: inheritance, composition, aggregation, using, and friend-ship.
  8. Templates
    Discusses generation of functions and classes with templates. Includes discussion of function overloading and class specialization.
  9. Template Metaprogramming
    Writing code that affects compile-time construction of functions and classes from templates.
  10. Libraries
    Summary of the C++ libraries and a few third-party libraries.
  11. Streams Library
    Discusses streams for interacting with the console, files, and strings.
  12. STL Library
    Covers the structure of the STL libraries and how some of them are used.
  13. Interesting
    Mentions a few topics and provides links to interesting ideas and methods.
  14. Appendix   - C++ Examples, C++ References

0.3References:
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