The C++ Programming Language

cpp

C++ is a general-purpose programming language originally created by Bjarne Stroustroup. The initial concept was to create an extension to the C programming language that would assist object-oriented programming using classes. The programming world soon realized the potential of the new language and so C++ took off on its own, gaining its place in the software industry and eventually its own standardization division in the International Standardization Organization (ISO).

C was developed in the early 70s, and it was designed to be light and simple and close to the metal as possible. This made it extremely popular because although it was a general-purpose and humanly readable, it created fast and efficient code. C++ started up around 1980 and was publicly available in the mid-80s. It was built to be compatible with C and take advantage of its best characteristics, while offering some extra features that would make software development easier and more robust.

Over the years the compilers for these languages have evolved dramatically, producing code of exceptional quality. Apart from that the languages have evolved by a great margin as well. The standardization committees have proposed changes that give them some fantastic characteristics that make the code compact and mistakes harder to make.

C++ is one of the 'Higher-Level languages' as we call them. In the early days of computers people wrote code in assembly language, which is the actual list of direct commands to the processor. That kind of programming requires a lot of repetitive work and above all the programs cannot be moved to another system. The advent of 'Higher-Level languages' has made things a lot easier, since these languages are modeled after human understanding and are more accessible. Another benefit is the portability of code because all you must do is create the translator for each language for every system. Then code portability is a straightforward task. These translators are the compilers and the interpreters.

C and C++ are compiled while Python is interpreted. This means that there is always an intermediate program that actually converts the code we develop into something the computer can execute. In the case of C/C++ it is a compiler, a program that converts our code into one piece of machine executable altogether, while in the case of Python it converts and executes one line at a time.

For the purposes of this tutorial, we will work on Windows 11, using Visual Studio 2022 Community Edition.

C++ is a simple language. You can learn the basics and create a valid program in a noticeably short time. Learning the most complicated features of the language surely takes a while because you need to accumulate programming experience to completely understand and appreciate them.

One of the things C++ inherited from C is the lack of any built-in functionality. There is no ‘command’ to print anything on the screen or read any input from the keyboard. All these tasks are imported into our programs from external libraries. The standardizations committee has made sure that trivial operations like reading the keyboard or printing to the screen or file input and output, as well as a great number of useful functions is in every implementation of the language and lies within the standard library. The structure of this library is strictly defined, and you can be sure that is cross-platform.

The code we develop in C++ is stored in text files usually referred to as source files. The compiler reads these files and translates their contents into machine executable instructions. Then we use another program called linker. The linker takes the output files generated by the compiler, checks for libraries that might be needed and puts all the machine code in one file which is the executable file. This file contains everything required by the system to do what we intended.

In everyday life the first time we compile and run our programs they do anything but what we intended. This is natural bearing in mind that it was a human that built it. Unless your program does nothing but display a greeting to the user, but does something more complex instead, chances are that something will go wrong. As programs grow bigger and more complex and more people are involved in their development the number of errors or bugs as we usually call them, grows.

These bugs trigger the so-called debugging cycle. Every time we complete the development of a feature in our software, and we build the executable, we perform a series of tests to make sure that everything runs as expected. Not only do we test the new features but the old ones as well, to make sure we have not broken anything. At the end we collect the errors we have found; we design and implement solutions, and we start testing again. This cycle is repeated until we are satisfied with the result.

Creating bug free code is not an easy task. There are many methodologies proposed over the years. The iterative process described before is the simplest one to use. It is very straight forward and if we design a complete set of tests and routinely perform them we can have a particularly good result. There are several well-established methodologies for developing robust software, but I should have to author another book just to scratch their surface. So, I will stick to this simple method which is adequate for the code in this tutorial and more than enough to get you started with the idea of creating stable software. Going deeper into this topic is far beyond the scope of this tutorial.

You can download all the examples for this tutorial from GitHub. The link to the repository is https://github.com/cosfer65/cpp_tutorial