C++

Typecasting in C++

Typecasting is a phenomenon in which the data type is converted from one type to another to make your computation of source code easy. Typecasting is supported by many languages including C++. This conversion of data type is helpful in the case when the user has provided the value in a single data type, but according to requirement, you need a second data type. So, you try to do typecasting in the programs. In this guide, with the help of examples, we will explain two major types of typecasting.

We are going to explain this concept on the Linux operating system, so you need to have Ubuntu installed and in the running form on your system. So you must install Virtual Box and after downloading and installation now configure it. Now, add the Ubuntu file to it. You can access Ubuntu’s official website, and download the file according to your system requirement and operating system. It will take hours, then after installation, configure it on the virtual machine. In the configuration process, make sure you have created the user because it is essential for any operation on the Ubuntu terminal. Moreover, Ubuntu needs the authentication of the user before doing any installation.

We have used the 20.04 version of Ubuntu; you may use the latest one. For the implementation, you need to have a text editor and must have access to the Linux terminal, because we will be able to see the output of the source codes on the terminal through the query. The user must have basic knowledge of the C++ programming language.

Typecasting Types

Both the types are explained in this guide along with the examples to elaborate clearly.

Implicit Type Conversion

This is called automatic conversion because it is executed automatically by the compiler without any external force provided by the user. This type of typecasting is used mostly in the program wherein the expression, there are two or more data types present. So, we use typecasting so that our data is not lost. Data types of all the variables are upgraded to the data type of that variable that contains the largest data type amongst them. However, in the case of implicit conversions, there are chances of data loss, different operational signs are lost. An overflow of data can also occur when the long is converted to the float.

Explicit Conversion

The major point that discriminates this type of conversion from the implicit conversion is that it is a user-defined conversion. The user involvement is done in such a way that the user can typecast the output to convert it into a particular data type. This is done in two ways.

Conversion by assignment: This is done by defining the required data type explicitly inside the parenthesis. The syntax is given as (type) expression.  The ‘type’ refers to the data type, to which the resultant value is converted.

Conversion by cast operator: It is known to be a unary operator, that indicates that one data type can be converted into another data type. This type is further divided into 4 sub-parts.

  • Static Cast: In this type, the pointer of the base class is cast in the derived class.
  • Dynamic Cast: This type is used in the code at run time.
  • Constant Cast: This type is referred to as an overriding constant.
  • Reinterpret cast: The pointer of one type is converted into another type.

Examples of Typecasting

Now, we will use some examples here to explain the concept of typecasting.

Example 1
Here, we have passed a number to the C++ code and this number is converted into an ASCII character equivalent to itself. The type of data in which we need to convert is written inside the parentheses along with the number that is to be converted. We have used the number ’75’. Firstly a library is used for editing as input and output stream. In the main program, we have used data type characters here.

Now, write the above-mentioned code in the file and save it with the extension of C++/ C. The execution of this program is done on the terminal of Ubuntu. This is done by the C++ compiler that is G++.

$ g++ -o  typecast typecast.c
$ ./typecast

The resultant value can be seen on the screen of the terminal. The ’75’ number becomes ‘K’ when converted into character.

Example 2
This is an example of implicit conversion. This example deals with the use of arithmetic operations in the program in C++ language. Two variables, one is an integer and the other one is a character, are used to store values according to their data type. The value stored in the character value is converted into ASCII value. The value of both the variables is added and then stored in the variable x. And then the data type float is used for a new variable to add the new value of x with a constant value.

All three values of variables are displayed at the end. X contains its modified value. And the value of y is already defined at the start and the value of Z is calculated through the arithmetic operation. Now, you can see the resultant value in the terminal of Ubuntu.

Example 3
This is an example of explicit conversion. We use a double number at the start and then convert it into the integer format. The small brackets contain the data type to which the value is to be transformed.

In the end, the sum of the whole process is displayed. On execution of the code through the compiler, you can see that an integer value is obtained although we have used double value as input in the code.  As a result of some operations, the value is 12. This is done by having a process of first converting 7.7 in 7. For this purpose, the value after the point is removed. And then add 5 in 7 to form 12.

Example 4
This is also an example of explicit conversion. A value is changed twice in this program. We have used a double value.  For every conversion, a constant number is used. The first time, the value of ‘a’ is added with a constant of 10, whereas in the second case, it is added with 120.

int total = (int)a =10;
float total1 =(float)a +120.0;

Now compile the code and then execute it in the terminal. The desired answer is obtained, you can see it on the Ubuntu terminal.

$ g++ -o typecast typecast.c
$ ./typecast

Conclusion

Typecasting phenomenon is explained in the C++ programming language. These programs are implemented by creating a Linux environment. We have used a text editor and terminal for the elaboration of codes. All four examples are added to enhance users’ current knowledge of C++. Typecasting, as described earlier, is used to change one data type into another in the source codes according to the given requirement. We aim to explain basic knowledge of typecasting in the C++ language. We are hoping this article will be a good effort for the user in gaining knowledge regarding typecasting.

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.