C++

Static_cast C++

In this article, we will be discussing the concept of Static cast in the C++ programming language. To begin with this concept, we must know about the concept of casting and its type. In simple words, casting is altering the data type of a variable into another. There are two types of casting in the C++ programming language one is implicit casting, and the second is explicit casting.

Type Casting in C++:

In the C++ programming language, there are two forms of casting or type conversion which are:

  1. Implicit type casting.
  2. Explicit type casting.
  3. Implicit Type Casting:

The implicit type casting is also called the automated type conversion. It is done by the compiler on real run time compilation and does not require any input or action from the user. This type of casting happens when two types of data types are present in an expression. For example:

int k = 8;

char alph = “U”;

k = k + alph;

This is a perfect example of implicit or automated type conversion. The compiler automatically converts the character variable’s data type to an integer data type by turning it on the run time to the ASCII standard value of the alphabet.

1. Explicit Type Casting:

The explicit type casting or type conversion is not the automated type conversion; the user has to do that itself in the code to convert a variable’s data type to another variable’s data type. So, the explicit type casting or type conversion is usually done when a smaller order data type is used to hold any information; however, it must be transformed to a higher data type to prevent data loss and to store more data. The conversion by assignment is done in an expression in the code, and the way to write this expression is shown below:

# (data type) expression

Now we’ll look at the different types of conversions that the cast operators in the C++ programming language may do. Cast operators, also known as unary operators, compel a variable’s data type to transition from one type to another. Cast operators can do four different casting types: Static cast, Dynamic cast, Re-interpret cast, and Const cast.

Static cast operator in C++:

The static cast is usually used when we have to convert the enum variable into floats, integer, or double data type variables in the C++ programming language. It is also used when we have to change a pointer variable from a base class to a pointer in a derived class.

In the static cast method, there is no check during the run time of a program for the type casting as compared to the dynamic type casting. The syntax used to execute a static cast conversion in the C++ programming language is written below:

# static_cast<type> (object);

In the above expression, we write the data type in which we want to convert our variable in between the angle brackets, and in the round bracket, we write the variable’s name or pointer’s name, which data type has to be converted.

Converting the float variable’s data type into an Integer using static cast method in Ubuntu 20.04:

In the C ++program, we sometimes face a situation in which we have to change a variable data type explicitly, so we must use the static cast method. To implement this concept in Ubuntu 20.04, open the terminal, go to the desktop directory by writing “cd Desktop”, then make a .cpp file by executing the command “touch” with the filename and extension of .cpp. Then, Locate and open your .cpp file on the desktop

We have added a library of iostream to perform input and output operations. After that, we have stated the namespace std. Then we have started our main function with the integer data type. Inside our main function, we have a variable “f” with a float data type and assign the value of “3.5” to it. Then we have another variable, “b” with the integer data type. And passed a static casting to the value of variable “f”. Then, we printed the variable “b” by using cout.

Graphical user interface

Description automatically generated with low confidence

The above code must be saved; click on the save button allocated on the top navigation bar of the file. Then go to your terminal again, compile the file and then execute it if there are no errors by using the following commands respectively “g++ file name” with your file extension that is “.cpp” and “./” for executing the readable file name that has the “.out” as its extension.

Text Description automatically generated

As you can see in the above result, we have successfully converted the float variable into the integer data type variable. The output shows that only the number is an integer variable before the decimal point is printed. We can also interchange the data type from one another, as the float variable to be converted into integer will be vice versa in which we will convert the integer variable to a float data type variable.

Converting an integer variable’s data type into float using static cast method:

Now we will invert the conversion that we have performed in the above example; we will be following the similar steps from the previous example as shown below:

Graphical user interface, text Description automatically generated

In the above code, we have initialized an integer variable “i” and assigned the value of “4”. Then we have another variable “a” with float data type and then converted its data type to float, and then printed it in the output by using cout statement. After compiling and executing the above code, the output will be displayed as:

A screenshot of a computer Description automatically generated with medium confidence

We have converted the integer variable into a float variable as the compiler shows no error. However, there still be some data loss as the integer is a lower order data type, whereas the float data type is a higher-order one.

Using static cast to divide two integers without data loss:

In this example, we will be dividing two integers by converting them to a float variable to save the data that an integer cannot hold. To do this, we will execute the following code where we will be initializing two integers named “i” and “k” with a value of “41” and “2”. Then we have another variable with a float data type titled as “a” and convert into a float variable and divide it by the other integer variable. Then to get the output to be displayed on the screen, we have used the cout statement.

Graphical user interface, text Description automatically generated

After compiling the code in the above example and executing it, we will get the following output:

Text Description automatically generated with medium confidence

The above output explains that we have divided the integer variable. Still, before dividing, we converted the integer variable to float data type and used the static cast operator in the expression to divide it as well. The result shows a number with the decimal point as it is now a float variable, and it can store numbers after the decimal point.

Conclusion:

In this article, we got to know about the type casting mechanism in the C++ programming language. Many types of casting were also explored in-depth, focusing on why we utilize such methods in C++ programming. We also implemented the concept of static casting by using the static cast operator in an expression in the C++ programming language in the Ubuntu 20.04 environment.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.