about
10/29/2022
C++ Introduction
C++ Bites Code

CppBites - Introduction

statically typed, native code, fast, complex language

These Bites are a linked series of brief web pages hosting presentations about the C++ programming language. They are intended to ease entry into the craft of writing C++. Each Bite provides an introduction to, and discusses the ideas behind, some aspect of the language. A menu on this page (lower right - Pages) shows the things we will be covering in this sequence.

Why C++ ?

Fig 1. Point Class Definition
Fig 2. Using Code
Fig 3. Output
C++ is a large, powerful, and complex language. It supports generic programming with templates and object oriented programming with classes and inheritance.

Things you need to know:


  • C++ is designed to support creation of user-defined value types that support construction and assignment of independent instance copies. Managed languages, like C# and Java, copy handles to instances on the heap, so an assignment in those languages results in two handles pointing to the same instance.
  • The C++ language compiles to native code which runs directly in a new process. Code for managed languages run in a virtual machine hosted by a new process. That has consequences for both latency and throughput.
  • C++ is complex, partly because it provides a lot of control over how platfrom resources are used, but also because the way you should write code for many operations depends on the context in which it is written.

Figures 1, 2, and 3 provide a sample of how C++ code is structured. Figure 1. holds some of the code for a template Point class definition. Figure 2 shows how using code creates and uses instances of the class, and Figure 3 displays the output when the code in Figure 2 runs. Here is Point code online in godbolt Compiler Explorer. Don't be concerned if these figures are hard to understand. We will be covering all of the details in subsequent Bites and in the C++ Story. In order to implement template functionality, C++ has created a compile-time functional language, use of which is called TemplateMetaProgramming, since TMP creates code at compile-Time.
TemplateMetaProgramming
  1. At design-time code is constructed that, when compiled, adds and modifies other code, which eventually runs when the program executes
  2. You will find a number of examples in CppStory_TemplateMetaprog.html

You can even use C++ to write C-like code, although for that you should probably just use C.

C++ highs

  1. C++ is very fast because it compiles to native code, does not use garbage collection, and much of its syntax and semantics are designed to operate as efficiently as is practical.
  2. It has strong static typing so syntax errors are evaluated at compile-time, not run-time as in dynamically typed languages.
  3. The language has a moderately large syntax footprint and a large collection of standard libraries. So you can find ways within the language to meet your implementation goals.
  4. It is widely used - a lot of currently running applications have been built with C++, there are many experienced developers with C++ expertise, and the tooling has been refined for years.
  5. Many third party libraries exist for math and scientific programming, graphics, games, databases, ...
  6. There are many sources of learning and reference material in the open literature. This site is one of the best: cppreference.com.

C++ lows

  1. C++ is a very complex programming language. Much of its complexity is due to context dependencies.
    Context Dependency Examples
    1. The keyword "static" means three different things, depending on its context:
      • As qualifier in a variable declaration in namespace or block scope, it means the variable is placed in static memory and has lifetime of its enclosing program. The variable is only initialized once, even if declaration occurs repeatedly in the program execution flow, and the variable is only visible in the scope in which it is defined.
      • A static method can access only static member data, and is used with "::" instead of the "." operator.
      • A static member variable has a single, possibly mutable, value that is shared by all instances of its class.
    2. Operations like construction and assignment may copy the source or may move ownership of the source's resources to the target. Which depends on whether the source is a temporary or a named entity or if the operation is in a context where return value optimization (RVO) applies.
    3. The rules for type deduction depend on whether the type is templated or not. And some of the rules are far from obvious.
    4. C++ provides strong, but not strict, static typing. The types of data can be modified at run-time through the use of casts; and many type conversions are made implicitly. That can make analysis of program operation more difficult and lead to some surprising results.
    5. The special methods you implement for a class - see below - depend on the types of its data members. And the compiler allows you, without warning, to fail to implement them when you should.
    6. and the list goes on ...

  2. The language has a moderately large syntax footprint and a large collection of standard libraries. So you have to invest a lot of effort and years of experience to use most of it effectively.
  3. There are many paths to undefined behavior, e.g., reading and writing to memory the program does not own. Here's a demonstration.

A Taste of C++ Syntax

C++ classes have a number of special methods. For a class X:
  • X(), X(const &X), X(&&X), and X(T)
    are default, copy, move, and promotion constructors
  • X& operator=(const &X) and X& operator=(&&X)
    are copy and move assignment operators.
  • T& operator[](size_t i) and T operator[](size_t i) const
    are index operators for mutable and const instances.
  • ~X() is the class instance destructor.
Most of these the compiler will generate for you using member and base wise operations on the class's members and base classes. For certain types of members, like primitives and containers from the Standard Template Library (STL), that works well, but if the class has pointer or lock members, the designer must provide most of them, as the compiler generated methods will be incorrect.
These are the most common special methods but there are several others including arithmetic operators for contexts where that makes sense.

STR Class

Implementation of all these methods is illustrated with the STR class. STR is a custom string class, not the std::string class. I wrote STR before there was a std::string class. You should use the std::string in your code, but STR serves as a very nice concrete presentation of how the special C++ methods can be implemented. All of this will be discussed in more detail, with examples, in the following bites.

References

reference comments
Wikipedia C++ Survey of language origin, evolution, and properties
C++ 11 - Herb Sutter video Survey of important advancements
Back to Basics - Herb Sutter video Thoughtful advice about developing with C++
C++ 11 - Wikipedia Survey of C++ 11 standard
Writing good C++14 by default - Herb Sutter video Writing relatively simple and clear C++ using features of C++14
C++ 14 - Wikipedia Survey of C++ 14 standard
C++ 17 the good and ugly - Nicolai Josuttis video Informed opinions about C++ 17
C++ 17 features - Bryce Lelbach video Survey of selected features
C++ 17 - Wikipedia Survey of C++ 17 standard
Almost complete overview of C++20 - Marc Gregoire video Survey of C++ 20 standard
C++ 20 - Wikipedia Survey of C++ 20 standard
   
  Next Prev Pages Sections About Keys