C++ Track

Story, Bites, and Repositories

Story Index Prologue Introduction Models Models of structure and semantics Data Types & instances Operations Callable objects Structures Class structure Libraries Standard libraries References Resource links    
Bite Index Introduction C++ overview Hello Hello World program Data C++ data types and instances more to come not for a while STR Class not for a while Glossary Glossary of C++ terms Summary Language summary    
Figure 1. C++ Demo Code
"C++ is a general-purpose programming language with a bias towards systems programming that is a better C; supports data abstraction, object-oriented programming, and generic programming."
- Bjarne Stroustrup, creator of C++

C++ is a general purpose programming language designed with strong abstraction mechanisms to model complex domain entities. It does not use garbage collection and compiles to native code. C++ supports building domain models with muliple inheritance of implementation, composition, aggregation, and using relationships. An example uses base behavior classes as inherited "mixins" for a core domain class model: class Widget : public Drawable<Widget>, public seralizable<Widget> { ... } Other languages can build these kinds of domain models, but require much more boilerplate code if they don't support multiple inheritance. The C++ language is standardized by the International Organization for Standardization (ISO). It's development is guided by the C++ Standards Committee, which releases a new edition every three years. The standard C++ libraries provide a lot of well engineered facilities including the Standard Template Library (STL). C++ modules library has had a long and difficult gestation, now available for compilers MSVC, gcc, and clang. Tooling in some popular editors have not yet stabilized, e.g., VS Code intellisence pluggins do not yet recognize module imports. The standard libraries do not, at this time, provide support for network programming. That is available throught the 3rd party Boost Library's ASIO. There are five ways of viewing C++ content in this site:
  1. Cpp Story
    An ebook with seven chapters that covers Cpp programming at intermediate level.
  2. Cpp Bites
    A large collection of pages each focused on one feature of the Cpp programming language, starting with basics.
  3. C++ Repositories
    A collection of code for components, projects, and demonstrations.
  4. Blogs
    There is now, as of 07 Jan 2025, one C++/Rust blog. That is expected to change eventually.
  5. Compiler Explorer
    An on-line code editor and executor
It's easy to sample each of these views by using the links above, or more selectively, using links in the CppExplorer panel on the left (if you don't see it, click on Toggle panel in bottom menu on left).
What I like about C++
  • Domain Modeling
    • Supports class relationships: inheritance of implementation, composition, aggregation, and using
      • Classes inherit both declarations and method and data definitions. Inherited instances reside within the memory footprint of the inheriting class instance.
      • A class composes an instance of another class which resides within the composer's memory footprint.
      • C++ classes aggregate instances of other classes that reside in the native heap.
      • Uses instances of other classes by accepting a reference, pointer, or smart pointer in method arguments. It is responsibility of designer to ensure their validity.
  • Good Performance
    • native code, no garbage collection
    • good code optimization potential
  • Safety - memory and data race safety constructs provided by C++ and Std::Library
    • RAII ties resource (memory, file handles, ...) lifetime to lifetime of owning object
      • Resource allocations managed with constructors and destructor, as specified in that code.
      • provides void, copy, move constructors and destructor
      • moved resources must not be used after move - that is not compiler enforced
    • no dangling pointers by construction
      • pass data to functions using const references
      • hold heap data with std::unique_ptr or std::shared_ptr which delete resources when they go out of scope.
      • use std::iterator to enumerate containers, starting with begin() and comparing to end().
    • range-based for loop avoids indexing errors
      • std containers will throw exceptions under common indexing errors, e.g., when another iterator inserts or deletes changing the value referenced by the iterator.
  • Code does more-or-less what its statements declare
    • Strong but not strict typing
      • sanctioned operations are limited to those defined by the type operated upon
      • but many silent conversions
      • casting has guard-rails but they can be overridden
      • there are many context dependencies that affect semantics of operations
    • can have syntax sugar
      • developers create abstractions and those can be well thought out or not
      • the language and std library have, over time, created relatively bullet-proof abstractions, starting with C++11.
    • Compiler error messages are often obscure
      • template type error message are often impenatrable - a problrem that has diminished with Concepts.
  • Good code resources
    • Godbolt Compiler Explorer
    • MSBuild, gcc, and CMake
      • Using Developer's Cmd prompt and bash as integrated terminal in VS Code
      • Building from command line
      • Building using JSON launch file
    • clang-tidy, Cppcheck, clang-format
      • content coming soon
    • std library
      • STL: containers, algorithms, and iterators
      • Metaprogramming Library: construction and analysis at compile-time
      • I/O Library: print functions, streams, FileSystems
    • Open Source Libraries
      • Boost, Qt, POCO, Google Abseil, ...
    • Visual Studio Code
    • Code Examples
      • coming soon
  • Getting easier to start and manage projects
    • more coming soon
  • Learning materials
    • more coming
The code above on the right illustrates a C++ Hello World program, often encountered when learning a new language.

Table 1. C++ Resources from this site

Site Resources Content
C++ Models Summary of features with screenshots and examples - pdf
C++ Operators Summary of operators with screenshots and examples - pdf
C++ Templates Summary of C++ Templates with screenshots and examples - pdf
C++ STL Summary of Standard Template Library with screenshots and examples - pdf
first chapter in C++ Story Cpp ebook in 12 chapters
first C++ Bite Code Bites about C++
C++ Glossary Definitions of common terms
C++ FlashCards Basic types and data structures
Bits of Code Compares small C++, Cpp, C#, Python, and JavaScript codes  
Code Examples Content
C++ Repositories Index of all the Cpp code repositories
Cpp Code examples
online code execution Compiler Explorer, tutorialspoint
Other Resources Content
Tooling Using Visual Studio Code to create and build Cpp code
cppreference Semi-formal, but surprisingly readable, reference for Cpp
C++ Core Guidelines Guidelines for crafting Cpp code, from the source
How to learn modern Cpp A collection of links to explorations of intermediate and advanced language materials
 

Exercises

  1. Write a Cpp program to find the largest file in a specified directory tree, using walkdir crate from crates.io.
  2. Ammend Project #1 by accepting arguments from the command line, using args crate.
  3. Use ChatGPT to create a directory traversal crate and use that instead of walkdir.
  4. Write a Cpp program to read a source code file and count the number of lines and number of scopes for each function. You can count scopes by counting open braces "{". Start by searhing for fn and then read each line searching for "{" and "}". Push each open brace on a stack, and pop when a closed brace is encountered. When the stack is empty, the end of the function has been reached.
  5. Modify Project #4 by counting lines and scopes for each file in a directory tree rooted at a specified folder, usually named "src".