Like other programming languages popular today, C++ has quite a story behind it, stretching back to 1979. But is it still relevant today? In this article, we will try to answer this question. You will learn more about this programming language, its history, advantages of using it, and why it’s still a great language to learn.
What is C++, and where is it used?
C++ is a general-purpose object-oriented programming language that was created in the 80s by Bjarne Stroustrup.
It’s an extension of C, which has been the main system programming language for quite some time. In contrast to C, it supports object-oriented programming features like classes. Like C, it is used when a low-level programming language is necessary. While C++ is commonly used for graphics-heavy software such as games, photo and video editing apps, browsers, C is more widely used for embedded devices and OS kernels.
A brief history of C++
C with Classes In 1979, Bjarne Stroustrup, a Danish computer scientist working at Bell Labs, started work on a new programming language called "C with Classes". A large inspiration for the language was Simula, which is considered to be the first object-oriented programming language ever. While Stroustrup was programming for his PhD thesis, he found Simula pleasant to program in, but the language was too slow for practical use. Therefore, he set out to add Simula-like features to C, in that way making a language that was both performant and relatively high-level for the time.
Initially, C with Classes just added features to the C compiler. The features added included classes, options for inference, and other things like strong typing.
In 1982, Stroustrup started to build the next version of his language. He built a new compiler based on the existing C compiler and added a ton of new features. After cycling through a couple of names, he ended up with "C++". The name comes from the C’s incrementation operator—it means C plus one.
https://youtu.be/JBjjnqG0BP8 C++ In 1985, the first commercial implementation of C++ was released. The same year also saw the release of the first reference book of the language – The C++ Programming Language. But Stroustrup didn’t stop there. In the 80s and 90s, C++ went through multiple releases, ending up on a standard – C++ 98, which was later revised in 2003 to create C++ 03. This was the common standard for quite a while, until the release of C++ 11 in 2011. Since then, C++ has released a new standard every three years, adding more and more modern features each time.
Advantages and disadvantages of C++
Like any programming language, C++ is not the perfect tool for all of the jobs. Let’s look at the advantages and disadvantages of writing your program in C++.
Performance. C++ is one of the fastest and most predictable languages out there, only contested by other low-level programming languages like Rust.
Large ecosystem. C++ has a ton of developers that use it, upgrade it, and write open-source libraries. While learning or using the language, you can draw on the work already done by these people.
Memory unsafe. Since C++ offers manual memory management, it is also memory unsafe. This introduces a specific class of bugs called undefined behavior—unpredictable actions that are caused by doing actions that are not a part of the language, for example, accessing the 10th element of a 5 element array.
Large projects can be very complex. Since C++ is closer to C than high-level languages, the code is rather detailed. Therefore, it can be hard for developers to orient in large projects.
Slower development times. It is reasonably faster to use high-level languages like Java and Python for creating applications. Therefore, if performance is not extremely important, it makes more sense to develop in a high-level language – you’ll get there faster.
Alternatives to C++
So what kind of choices do you have other than C++? Let’s quickly compare C++ with other modern languages.
C++ vs. C
As we have said before, these languages are quite similar. C++ is a superset of C, which means that it is C with extra features. Therefore, every valid C program is a valid C++ program. So why wouldn’t developers choose to use C++ instead of C? There are a few reasons.
- C is even more performant than C++. While C++ is built around very efficient abstractions, using the extra functionality that C++ provides can be detrimental to the performance. Therefore, if you want to squeeze out the last bits of performance in your small IoT device, you will choose to use C.
- Some devices do not support it. If you program for small devices, it can happen that the hardware can only support C code, so the choice is, in a sense, made for you.
C++ vs. Rust
Rust is a bit of a newcomer to the arena of low-level programming languages. Instead of using manual memory allocation, its compiler analyzes your memory management and rejects programs that could cause problems somewhere down the line. Therefore, it is much safer to use than C++.
In addition, Rust is a newer language that is not encumbered by decades of history and legacy code that it needs to support. Therefore, it can add modern features much more quickly and support them better.
But don’t get convinced so quickly. Because Rust is so new, it lacks a lot of libraries and ecosystem support for a lot of tasks that C++ is good for. In addition, modern C++ has a lot of tooling that can help escape its downfalls, and it is getting better every year.
C++ vs. high-level languages
C++ is always faster than these languages since it doesn’t use a garbage collector. While in C++, you manage your own memory manually, garbage-collected languages do that for you. It usually comes with a performance downside because the program has to stop at some time and garbage collect, making the performance unpredictable.
This is unacceptable in games or operating systems. Imagine if your game or operating system became slower for random periods of time—that wouldn’t really be the best experience. But, in contrast, the code in high-level languages is faster to write in. Therefore, for things like web apps, we optimize for the fastest time to develop, release, and test the app, rather than focus on small performance benefits. Afterward, it is possible to rewrite parts of the app in C++ to scale.
Why you should learn C++ in 2021
C++ is still relevant since many applications still bottleneck on processing speed, and the problems with that are not going away. At this time, it is one of the best choices for fast software, with alternatives like Rust seriously lacking ecosystem support for a lot of use cases.
The language has been around for almost 40 years, which means that most of the software problems have already been solved by open-source libraries and frameworks. In contrast, to build a high-quality game in Rust, one would have to recreate a framework like Unreal Engine in it.
Besides, there are significant gains from learning a language with manual memory management. C++ gets you much closer to bare metal, which will help better understand how computers work. With that knowledge, you can be much more effective while using other languages like Java and C#.
What is more, you don't even lose that much on learning C++ first. Once you have learned it, it is relatively easy to switch to another language since a lot is shared across different object-oriented programming languages.
All in all, if developing high-performance applications such as browsers or game engines interests you, C++ will be the best choice for you. If not, perhaps you need to look into an alternative.
How to get started
Here are a couple of resources to get started with C++:
- A 10-hour tutorial on C++ on Youtube. It includes enough info to learn the basics of C++ and start doing object-oriented programming with classes and inheritance.
- The C++ track on Exercism. Exercism has 50+ C++ exercises and mentors to help you make sure your code is using the concepts correctly and looks idiomatic.
- Google's C++ class. Google has a free class on C++ that features a lot of learning materials that should get you through the basics of the language.
- Programming: Principles and Practice Using C++ (2nd Edition). At one point, it will be helpful to pick up a book on C++. This book is written by the creator of the language and is battle-tested on thousands of university students, so it shouldn’t be the worst one out there.