Carlos Enrique Hernández Ibarra bio photo

Carlos Enrique Hernández Ibarra

Passionated Software Engineer with experience in the Automotive Industry. Interested in the state of the art of design, development, and test of technology based on C/C++, Autosar, and Matlab/Simulink. Nevertheless, always with an open mind to new technologies, industries and solutions.

Email LinkedIn Github

C++ is a versatile, multi-paradigm programming language commonly referred to as a compiled language. In a typical C++ program, various source files are compiled by a compiler, producing object files. These object files are then combined by a linker to generate an executable program. The following figure illustrates the process of creating an executable file in C++:

01

In C++, there are two main types of entities:

  1. Core Language Features: This includes built-in types, loops, conditional statements, and basic keywords that are fundamental to the language itself.
  2. Standard Library Components: These are components from the Standard Template Library (STL), encompassing containers, algorithms, I/O operations, and essentially everything that is added using the #include directive to incorporate functionality from the standard library.

Practical Introduction

For example, the following code snippet illustrates how to declare a function, utilize the standard output stream (cout), and provide a basic implementation of the main function, which serves as the entry point for the executable program.

02

Type, Variables & Arithmetic

Some fundamental C++ data types include:

  • bool: Represents true or false values.
  • char: Represents individual characters, such as ‘a’, ‘f’, or ‘&’.
  • int: Represents integer values, like 1, 5, or 1245.
  • double: Represents floating-point numbers, for example, 3.1415.

It’s important to note that the size of these types can vary across different machines, and their sizes can be determined using the sizeof operator.

For instance, a typical size for a char is 8 bits, and the sizes of other variables are usually quoted in multiples of the size of a char. If a char is 8 bits, an int is commonly 32 bits, and a double is often 64 bits. The following image illustrates this concept:

03

Universal Initializer

With the advent of C++11, the syntax options for object initialization have expanded, but braced initialization is the most recommended and versatile syntax. This type of initialization helps prevent narrowing conversions and unintended calls. The following code snippet illustrates this idea:

04

Points and Arrays

Pointers can be challenging for inexperienced C++ developers, as it’s easy to introduce errors. In my C++ programs, a significant percentage of issues often revolve around pointers. For instance, consider the following code, which explains various operations but ultimately accesses prohibited memory:

05

It’s crucial to note that the prefix unary “*” signifies “contents of,” and the prefix unary “&” denotes “address of.” Additionally, when we want to express the concept of “no object available,” it is recommended to assign the pointer the value nullptr.

I/O Stream

Directly writing or reading from/to a specific source might be acceptable for specialized applications. However, as a general practice, it is advisable to separate the way our program reads and writes from the actual input and output device. This approach helps us avoid the need to address specific implementations for each different device, preventing the necessity of modifying our program for every screen or disk in the market. It also ensures that we are not limited to using only a specific set of screens or disks that we happen to prefer, thereby avoiding restrictions on our clients.

In contemporary computing, operating systems segregate the intricacies of managing specific I/O devices into device drivers, enabling programs to access them through a generic I/O library. This practice promotes more efficient and versatile development.

The C++ Standard provides I/O libraries that define output and input for every built-in type. In this article, we will specifically focus on three stream libraries:

  1. fstream: Used for reading from a file and writing to a file within the same stream.
  2. ofstream: Converts objects in memory into streams of bytes and writes them to the file.
  3. ifstream: Takes streams of bytes from the file and assembles objects from it.

Write a File

The following snippet shows the basic- recommend steps to write a file using C++:

06

Read a file steps

The coming snippet shows the basic- recommend steps to read a file using C++, this program reads the content of the file and shows the same content in the output console window:

07

Exercises

The following exercise involves generating a data file in a designated format <hour (0-23), temperature (ºC)>.

In line (1), the process illustrates file creation with specified version and mode parameters, specifically configured for writing the file.

In line (2), error handling is demonstrated, including the display of an error message and program termination. Many C++ experts advise error checking immediately after file creation or opening.

Line (3) represents the format utilized for writing data into the file <12 56C>.

08

C++ Template

Templates are the foundation of generic programming. They enable the creation of various versions of classes and functions with parameterized types and a shared behavior. Plantillas deben de estar típicamente en los header files, debido a que los compiladores necesitan saber como una plantilla es en orden de instanciarlo al momento de usarlo. The syntax for a template function is as follows:

09

Where the template indicates that the function is a template, and the T before power indicates that the function returns a value of type T:

A template parameter not only allows passing values but also their type. For example, consider the following template function swapi:

10

In any case, T can be any type that implements the functions involved in such a function, in this case, namely the constructor, copy constructor, and the = operator. Fraction implements these functions:

11

Like template functions, there are also template classes. In UML, template classes have a T in their upper-right corner.

Similar to STL containers, a parameterized class can accommodate different types of variables (for example, a list can work with variables of type double, int, string, and even user-defined types).