Beginning modern C++ (1)

Xiaoou&AI
2 min readFeb 25, 2021

In machine learning python is the standard language to go to when it comes to learning, however c++ is everywhere (many python codes run c++ underneath and besides, people often switch to c++ when performance matters, that is to say, in production).

C++ has a nasty reputation of being hard to be learned, so I want to write a modern introduction using modern IDEs like Clion.

First, install Clion here: https://www.jetbrains.com/clion/download/

When starting Clion for the first time, choose C++ executable and C++14 (you really don’t need to understand why now).

So here is the first lesson. Actually it’s quite simple. C++ is a strong-typed language, so very variable declaration should be preceded by its type. The return type should also be specified for functions. The rest of the code is self-explaining. The include header is called a directive and since we use printf here which is not part of the C++’s internal library we should include the <cstdio> library.

The %d in printf is a placeholder to be replaced by a int variable.

This is the first lesson. I’ll continue this series and very soon you’ll understand the power and the complexity of C++ coming along with its performance.

#include <cstdio>

int step_function(int x) {
int result = 0;
if(x < 0) {
result = -1;
} else if(x > 0) {
result = 1;
}
return result;
}

int main() {
int num1 = 42;
int result1 = step_function(num1);

int num2 = 0;
int result2 = step_function(num2);

int num3 = -32767;
int result3 = step_function(num3);

printf("Num1: %d, Step: %d\n", num1, result1);
printf("Num2: %d, Step: %d\n", num2, result2);
printf("Num3: %d, Step: %d\n", num3, result3);
return 0;
}

--

--