Tutorial Study Image

C++ Type Conversion


August 7, 2022, Learn eTutorial
1674

In this tutorial, we will look at how to convert one data type to another in the C++ programming language. Type conversion is the process of converting one variable's predefined data type into an appropriate data type. The basic idea behind type conversion is to convert two different data type variables into a single data type in order to quickly solve mathematical and logical expressions without data loss.

For example, if we are adding two numbers and if one variable is of the int type and the other is of the float type, we must convert or typecast the int variable into a float in order to add them.  

C++ enables us to convert data from one type to another. This is referred to as type conversion.

There are primarily two types of type conversion in C++.

  • Implicit Type Conversion
  • Explicit Type Conversion

In C++, type conversion can be accomplished mainly in two ways: implicit type conversion and explicit type conversion. The implicit type conversion, which is also known as automatic type conversion, is carried out by the compiler itself. The explicit or user-defined type conversion is one that is performed by the user or requires user intervention. Let us now look at implicit and explicit type conversion in C++ in detail.

Implicit Type Conversion

The compiler performs implicit type conversions automatically and without human involvement. It means that an implicit conversion automatically converts one data type into another based on some predefined C++ compiler rules. As a result, it is also known as automatic type conversion.

For an example,


int x = 50;  
short int y = 5;  
int z = x + y;  
 

In the above-given example, there are two different data types variables, x, and y, where x is an int and y is a short int. And the resulting variable z also belongs to the integer type, which will be storing the x and y variables. However, before calculating the sum of two numbers, the C++ compiler converts the lower rank data type (short int) value to the higher rank data type (int). As a result, it avoids data loss, overflow, as well as sign loss in C++ implicit type conversion.

Implicit Type Conversion

Example 1 : Let's write a program that uses implicit type conversion for Converting an int to a double

Implicit type conversion program


#include <iostream>
using namespace std;

int main() {
   // here we are assigning an int value to num_int
   int num_int = 7;

   // declaring a double type variable
   double num_double;
 
   // implicit conversion
   //  int value to a double variable is assigned
   num_double = num_int;

   cout << "num_int = " << num_int << endl;
   cout << "num_double = " << num_double << endl;

   return 0;
}

Output:


num_int = 7
num_double = 7

We have assigned an int data to a double variable in the above program.


num_double = num_int;

Here, in example 1 the compiler automatically converts the int value to a double before assigning it to the num_double variable. This is an illustration of implicit type conversion.

Example 2: Let's write a program for automatic conversion from double to int using implicit type conversion.

Implicit type conversion program


#include <iostream>
using namespace std;

int main() {

   int num_int;
   double num_double = 8.88;

   // implicit conversion
   // assigning a double value to an int variable
   num_int = num_double;

   cout << "num_int = " << num_int << endl;
   cout << "num_double = " << num_double << endl;

   return 0;
}

Output:


num_int = 8
num_double = 8.88

We have assigned a double data to an int variable in the above programme.


num_int = num_double;

The compiler automatically converts the double value to int before assigning it to the num int variable. This is also a very good example of implicit type conversion.

The digits after the decimal point in the example above are truncated because int cannot have a decimal part.

How data Loss Occurs during Conversion?

As we can see from the preceding example, converting from one data type to another is prone to data loss. This mainly occurs when larger-type data is converted to smaller-type data.

Implicit Type Conversion

Data Loss Occurs During Conversion

This fig shows data Loss that Occur During Type Conversion.

Explicit Type Conversion

The explicit type conversion refers to conversions that require user intervention to change the data type of one variable to another. In other words, an explicit conversion enables the programmer to change or typecast the data type of one variable to another. As a result, it is also known as typecasting. To convert data from one type to another, we typically force the explicit type conversion because the implicit conversion rule is not followed by it.

In C++, we can use explicit conversion in three different ways and  they are:

  1. C-style type casting 
  2. Function notation /function style casting
  3. Type conversion operators

1.    C-style type casting 

As the name implies, the C programming language prefers this style of casting. It is also referred to as cast notation.

Syntax


(data_type)expression;
 

For an example


// initializing int variable
int num_int = 36;

// declaring double variable
double num_double;

// here converting from int to double is done
num_double = (double)num_int;
 

1.    Function style casting  

The function like notation can be also used to cast data from one type to another.

Syntax


(data_type)expression;
 

For an example


// initializing int variable
int num_int = 36;

// declaring double variable
double num_double;

// converting from int to double
num_double = double(num_int);
 

Example 3 : an example program for type casting in C++


#include <iostream>

using namespace std;

int main() {
    // initializing a double variable
    double num_double = 4.67;
    cout << "num_double = " << num_double << endl;

    // C-style conversion from double to int
    int num_int1 = (int)num_double;
    cout << "num_int1   = " << num_int1 << endl;

    // function-style conversion from double to int
    int num_int2 = int(num_double);
    cout << "num_int2   = " << num_int2 << endl;

    return 0;
}

Output:


num_double = 4.67
num_int1   = 4
num_int2   = 4

The above example, it displays the results of type conversion using both C-style type conversion and function-style casting. They both produce the same result because they perform the same task.

3. Type conversion operators

C++ has four operators for type conversion in addition to these two type castings. They are mainly  known as  the type conversion operators. 

  1. static_cast
  2. dynamic_cast
  3. const_cast
  4. reinterpret_cast

Type conversion operators are explained in the next tutorial in detail.(link)

Benefits of  the Type Conversion:

  • It is possible to achieve the features of data Type Hierarchies.
  • It is useful to compute expressions with variables of various data types.
  • It is very convenient

Disadvantages of the Type Conversion

  • The system becomes more complex as a result of typecasting.
  • Unexpected casting can cause errors.
  • Casting types can be accessed directly from objects.