Type Conversion in C++

In this tutorial, we will learn how to type convert in C++.

Type Conversion in C++

C++ allows us to convert data from one type to another. This is known as type conversion in C++. Type conversion is mainly done to make variables of one type work with variables of another type in order to carry out an operation.

There are two ways of type conversion:

1. Implicit
2. Explicit

1. Implicit Type Conversion

It is also referred to as automatic type conversion as the compiler does it on its own. The user does not need to provide any external trigger. When multiple data types are present in an expression, type conversion occurs to avoid loss of data.

Data types of all variables are upgraded to the largest data type in the expression. Following is the order of implicit type conversion:

bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double

Example of implicit type conversion in C++

#include <iostream>
using namespace std;

int main() {
  int a = 8;
  char b = 'm';
  float c = 2.45;
  float result = a+b+c;   //types of a and b are implicitly converted
  cout<<result;
  return 0;
}

Output

119.45

In implicit type conversion, data can be lost. For example, when signed is automatically converted to unsigned, we can lose sign or when long long gets converted to float, overflow can take place. This can also happen when a data of larger type is converted to smaller type. For example, when double is converted to int, the decimal part is lost.

2. Explicit Type Conversion

This is also known as Type Casting in C++. It is done by the user i.e., we can explicitly convert one type to another.
We can do this in one of the following ways:

a. Generic type casting in C++

We can consider this as forceful casting. There are two syntaxes for generic type casting:

i. Functional notation
Expression is kept in parentheses.
type (expression)

ii. C-like cast notation
Data type is kept in parentheses.
(type) expression

Example of generic type casting in C++

#include <iostream>
using namespace std;

int main() {
  float x = 10.3;
  int y;
  cout<<x<<endl;
  y = int (x);    //functional notation
  cout<<y<<endl;
  y = (int) x;    //C-like cast notation
  cout<<y;
  return 0;
}

Output

10.3
10
10

b. Using cast operators in C++

Cast operators are unary operators that convert one type to another. There are four types of cast operators supported in C++:

i. static_cast
static_cast performs conversion between pointers to classes that are related. It can execute upcast (converting pointer-to-derived to pointer-to-base) as well as downcast (converting pointer-to-base to pointer-to-derived).

During runtime, no checks are made to ensure that the object being converted is a full object of the destination type. As a result, it is the programmer’s responsibility to ensure safe conversion.

Syntax
static_cast <new_type> (expression)

ii. dynamic_cast

We can use dynamic_cast only with pointers and references to classes (or with void*). Its aim is to check that the type conversion result points to a legitimate full object of the target pointer type. This does upcasting (converting pointer-to-derived to pointer-to-base).

However, it can downcast (converting pointer-to-base to pointer-to-derived) polymorphic classes (classes containing virtual members) only when the pointed object is a legitimate full object of the target type.

Syntax
dynamic_cast <new_type> (expression)

iii. const_cast

It influences the constancy of the object pointed by a pointer, allowing it to be set or removed.

Syntax

const_cast <new_type> (expression)

iv. reinterpret_cast

reinterpret_cast changes any pointer type to any other pointer type, regardless of whether or not the classes are related.

A basic binary copy of the value from one pointer to the other is the result of the operation. All pointer conversions are permitted; neither the data pointed to nor the type of pointer is checked.

Syntax

reinterpret_cast <new_type> (expression)

Example of type casting using cast operators in C++

#include <iostream>
using namespace std;

int main() {
  float x = 24.37;
  int y = static_cast<int>(x);
  cout<<y;
  return 0;
}

Output

24

Advantages of Type Conversion in C++

  • Type conversion enables the use of some characteristics of type hierarchies or type representations.
  • It is useful in computing expressions that include variables of different data types.

Summary

We hope this article helped you understand type conversion in C++. There are two methods of type conversion, implicit and explicit. Implicit type conversion is automatically done by the compiler. We saw how it is done using an example.

Explicit type conversion, also known as Type Casting, is done by the user. We discussed the ways in which we can explicitly type cast with suitable examples. We also learnt that there are four types of cast operators available in C++, static_cast, dynamic_cast, const_cast and reinterpret_cast.