A simple introduction into C++

Hello folks, this time I want to start with a series about programming with C++. While Python is a general purpose programming language, C++ is often chosen when you need tighter control over performance and latency.

We start simple by creating a short program that spits out the limits for the different data types. Install Microsoft code so that have one simple IDE you can work with…​

And create a file main.cpp with the following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <limits>
using namespace std;

int main() {
    cout << "Limits for integer types:" << "\n";
    cout << "int minimum = " << numeric_limits<int>::min() << "\n";
    cout << "int maximum = " <<  numeric_limits<int>::max() << "\n";
    cout << "long minimum = " << numeric_limits<long>::min() << "\n";
    cout << "long maximum = " << numeric_limits<long>::max()  << "\n";
    cout << "long long minimum = " << numeric_limits<long long>::min() << "\n";
    cout << "long long maximum = " << numeric_limits<long long>::max() << "\n";
    cout << "unsigned maxima (minimum is 0):\n";
    cout << "unsigned int = " << numeric_limits<unsigned int>::max() << "\n";
    cout << "unsigned long = " << numeric_limits<unsigned long>::max() << "\n";
    cout << "unsigned long long = " << numeric_limits<unsigned long long>::max() << "\n";
    cout << "\nNumber of bytes for:\n";
    cout << "int        " << sizeof(int) << "\n";
    cout << "long        " << sizeof(long) << "\n";
    cout << "long long   " << sizeof(long long) << "\n";
}ś

Next you need to have the free gnu compiler gcc have installed.

We compile the file with a simple

1
g++ main.cpp -o main

and can execute the program:

1
./main

Exact sizes depend on platform and compiler; e.g., long is 8 bytes on many Unix-like 64-bit systems, but often 4 bytes on Windows, but following is the result on the authors machine (Debian bookworm):

Limits for integer types:
int minimum = -2147483648
int maximum = 2147483647
long minimum = -9223372036854775808
long maximum = 9223372036854775807
long long minimum = -9223372036854775808
long long maximum = 9223372036854775807
unsigned maxima (minimum is 0):
unsigned int = 4294967295
unsigned long = 18446744073709551615
unsigned long long = 18446744073709551615

Number of bytes for:
int        4
long        8
long long   8

Dissecting a "Hello World" Program

In this section we will examine a simple Hello World program running on a Linux-based platform with a 64-bit processor. Although the program is small, it already contains several important concepts that appear in every C++ application.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {

    cout << "Hello World" << "\n";
    return 0;
}

At first glance the program appears trivial, but even here multiple stages of compilation and execution are involved. We will therefore go through the code step by step in order to build a clear mental image of what happens.

The first line contains a so-called preprocessor directive. Preprocessor directives are handled before the actual compilation process begins and are used to modify the source code. The #include <iostream> statement instructs the preprocessor to take the contents of the iostream header file and insert them into the program. This header provides functionality for input and output operations.

The next line, using namespace std;, tells the compiler to use the standard namespace. Many components of the C++ standard library, including cout, reside inside this namespace. Without this instruction, each usage would need to be written explicitly as std::cout.

After these declarations we reach the core of the program: the function main(). Every C++ program begins execution in this function. When the operating system starts the program, control is transferred directly to main().

Inside the function body we find the statement cout << "Hello World" << "\n";. The object cout represents the standard output stream and is used to write text to the console. The insertion operator << forwards the string to the output stream, while "\n" produces a line break.

Finally, the statement return 0; signals that the program has terminated successfully and returns control to the operating system.

We compile the file with a simple

1
g++ hello.cpp -o hello

and can execute the program:

1
./hello

so we get an

1
Hello World!

As we can see by now, the Assembler differs on the different platt forms (e.g. Interrupt 80 on Linux plattforms and interrupt 21 on DOS / Windows plattforms.

So we can see the big disadvantages of Assembler

  • Extreme Efficiency and Speed
  • Direct Hardware Manipulation
  • Total control
  • Smaller Executable Size:
  • Lack of Portability
  • High Complexity and Learning Curve
  • Difficult to Debug and Maintain
  • Slow Development Time