C++ is a language I've always wanted to learn. So many modern languages are influenced by C++ and their designs are often predicated upon the strengths and weaknesses of C++. For example, I recently wrote about how interfaces and the lack of multiple inheritance in Java is due to C++.
C++ is a low level language closely related to the C programming language. Originally called "C with Classes," C++ added object oriented concepts on top of C1. In most cases C++ is still a true superset of C. One of the main design philosophies of C++ was to make it so low level that no language would be needed below it2. Because of this philosophy, C++ is commonly used for low-level tasks such as system programming. However, being low-level causes C++ to contain some complexities.
This post is my first impressions of C++. I compare it to other languages such as C and Java, since C influenced C++ and Java was influenced by C++. I also mention all the basic C++ features that I find interesting.
When first writing C++, the similarities to C are immediately apparent. However, C++ does add new syntax which can be used on top of the traditional C syntax. For example, C++ adds a new variable initialization expression to complement the existing C expression.
C++ has some modern syntax as well. For example, types don't need to be explicitly declared with the help of the
auto is similar to
var in Java and C# (note: all these languages are still statically typed).
Just like C, you can define constant variables with the
const keyword. C++ adds the
constexpr keyword, which defines an expression or function that is evaluated at compile time3.
constexpr functions must be pure functions that only handle immutable variables. I created a compile time function that calculates the mile pace of a run.
constexpr in C++ was likely influenced by functional programming. Creating compile time functions is also possible in Haskell, a functional programming language4. You can check out the Haskell code on GitHub.
Compile time functions are commonly used as a performance strategy. We can prove
pace() executes at compile time by using static assertions. Static assertions are just like regular assertions except executed at compile time instead of runtime.
Static assertions are a really cool language feature that I've never seen before. The greatest thing about them is how IDEs check if they pass in the code itself.
C++ also adds additional pointer functionality to the C model. While you can still use pointers just like in C, there is an additional "references to" operator specified with the unary suffix
Anyone who's heard of C++ but never used it usually thinks its an object oriented version of C. This assumption is true, as C++ provides an enhanced custom type system with classes. I decided to create an API for a running exercise in both C++ and C. This example demonstrates some differences between the two languages.
The C++ version uses a class to encapsulate the run information and available methods.
The C version uses a
struct to represent the run and maintains the
pace() function in the same header file.
I'm really excited to continue learning C++ and see how it influenced all the languages I use on a daily basis. All the code from this article is available on GitHub.