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++:
In C++, there are two main types of entities:
- Core Language Features: This includes built-in types, loops, conditional statements, and basic keywords that are fundamental to the language itself.
- 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.
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:
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:
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:
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:
- fstream: Used for reading from a file and writing to a file within the same stream.
- ofstream: Converts objects in memory into streams of bytes and writes them to the file.
- 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++:
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:
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>.
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:
Where the template
A template parameter not only allows passing values but also their type. For example, consider the following template function swapi:
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:
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).