🐆 2 min, 🐌 5 min

C++: Crash course

C++ is a compiled object-oriented language that has been in wide use ever since its creation.

Official site here although the really good docs are at C++ reference page .

C++ is definitely one of the most technical language but an extremely powerful tool. So if you're not familiar with any programming language checkout python first.

How language came to be?

In 1979, a Danish scientist named Bjarne Stroustrup (while at AT&T Bell Labs) began his work on the C with classes. The official first release of the object-oriented language C++ came out in 1978.

An important side note. AT&T Bell Labs is a place that also produced Unix, C, transistor and a lot other essential concepts of modern computing. AT&T Bell Labs was an innovation powerhouse that had a massive role in the development of the software world.

First, there are more than two versions of C++. And this is where the mess really begins.

C++ versions

  • 1979 1998 C++ was developed as an extension of C.
  • 1998 C++98 We get the famous C++ version 98. This version gave a bit of a bad rap too C++ because it didn't get any major upgrades for 13 years. Then when updates came, people staid stuck in the past. So if you ever see a really crappy C++ code. It's probably written by a programmer that didn't learn the modern C++.
  • 2003 C++03 minor update of the language but nothing major.
  • 2011 C++11 or the modern C++. Since then C++ is on a three-year release schedule and started to gain all bells and whistles of a modern programming language. A massive enlargement of the standard library: regular expressions, thread support, atomic operations, ... and much more.
  • 2014 C++14 generic lambdas, variable templates, ...
  • 2017 C++17 filesystem library, type-safe union called variant, ...
  • 2020 C++20 ranges library, ...
  • 2023 C++23 experimental and in development.

In the last 20 years, C++ developed massively. So whoever says that the language is dead is wrong. Language is starting to awaken from the dead 🙂

Run C++

Compared to python, C++ is a compiled language (also a complicated language). Once we write our file hello_world.cpp we need to convert the file into an executable. To do that we use a compiler.

Depending on the platform, either gcc or g++ should be available. But before we dive into the compiler, let's write a simple Hello, world! example.

How to write Hello, world! in C++?

Create a file hello_world.cpp and place the following into it:

#include 

int main(){

std::cout << "Hello, world!" << std::endl;

return 0;
}

We'll get into details of what's what in a second. Let's first run the program.

How to compile a C++ program?

First, we need to convert our C++ program into machine-readable code. We do this by using something called a compiler.

On macOS us g++ on linux use gcc:

> g++ hello_world.cpp

Above will create an executable file called a.out in the same folder as you ran the script. Now to execute the program run:

> ./a.out
Hello, world!

That's it.

./ in front of a.out is needed to tell the command line to run the script from the current directory.

The extra step is required because with command g++ hello_world.cpp we first compile the program or convert it into machine-readable code and then with ./a.out we execute the program.

The bones of the hello_wordl.cpp

Let's take:

#include 

int main(){

std::cout << "Hello, world!" << std::endl;

return 0;
}

apart line, by line. First line:

#include 

is an import statement. We tell C++ to include library iostream. The library we need to print characters on the screen.

Then we have:

int main(){

Here we define a function named main() that doesn't get any input parameters. Its return value is of type int.

The { curly bracket opens up the scope and } closes the function's scope. So the whole function is:

int main(){

// function body

return 0;
}

{} bracket as mentioned defines the scope. You can imagine a scope as a box in which variables are global. Once the scope ends the variables and everything that was created inside the scope is deleted. Unless you used pointers in the wrong way, but that's another story.

Then we have the print statement:

std::cout << "Hello, world!" << std::endl;

The whole thing works much like print() in python. The wired symbols and what they mean will be discussed in the future.

We'll go further into the details in the next posts.

I hope you're not afraid and scared shitless at this point because C++ is a complicated beast. But once you master it, you get access to entirely new computing frontiers that are just a wet dream in terms of performance in other languages.

Get notified & read regularly 👇