C++ Type Conversion

In this lesson, you will learn about Data Type conversion in C++, the type of conversions available to us, advantages and disadvantages.

C++ is a strongly typed language, which means we have associated Data Types with the variable names before using them in our programs. The C++ Type Conversion refers to converting one Data type to another. This terminology’s primary purpose is to make one Data Type compatible with another to perform some operations and tasks.

Let’s suppose we need to add two variables from different data types, for example, int and float. Before doing so, we will have to convert the int data type into a float to make them of the same Data Type.

More like this:

Types of type conversion in C++

In C++, there are two types of type casting/conversion:

  • Implicit type conversion
  • Explicit type conversion

Implicit Type Conversion in C++

In an implicit type conversion, the compiler will automatically convert the Data Types type to another Data type. For example, suppose the user assigned an integer value to a floating-point variable. In that case, the compiler will automatically insert the code to convert the integer data type to floating.

Example of Implicit Type conversion in C++

#include <iostream>

using namespace std;
int main() {
  int num = 10;
  char ch = 'A';
  cout << "10 + 'A' = " << num + ch << endl;
}

Output

10 + ‘A’ = 75

The above example elaborates on implicit type conversion. An integer num and a character ‘A’ variables are declared. When adding these two variables, implicit type casting takes place. The compiler will convert a variable into the largest data type. As the integer is larger, the character variable is converted into its equivalent data type like the ASCII value of A is 65. When we add 65 and 10, the result is 75.

Explicit Type Conversion in C++

This type of casting is a user-defined type conversion. In explicit typecasting, the user converts one Data Type of variable into another Data Type.

There are two different types of explicit type conversion.

  • Converting by Assignment
  • Converting by Cast Operator

Converting by Assignment in C++

Converting by assignment is performed by defining the required Data Type in front of expression by using parenthesis. This is considered a forceful type of conversion. The syntax is;

(type) expression

where type is the Data Type to which it’s converted.

Example of Converting by Assignment in C++

#include <iostream>
using namespace std;
int
main ()
{
  double x = 1.2;
// Explicit conversion from double to int
  int sum = (int) x + 1;
  cout << "Sum = " << sum;
  return 0;
}

Output

Sum = 2

Conversion by Cast Operator in C++

A cast operator is a unary operator whose purpose is to force one Data Type to be converted into another.

Cast operators support four types of casting.

  • Static Cast
  • Dynamic Cast
  • Constant Cast
  • Reinterpret cast

Example of Conversion by Cast Operator in C++

#include <iostream>
using namespace std;
int
main ()
{
  float f = 3.5;
// using cast operator
  int b = static_cast < int >(f);
  cout << b;
}

Output

3

Advantages of Type Conversion in C++

  • Helps to compute the expressions that contain variables of different data types.
  • Features of Data Type hierarchies can be achieved.
  • More convenient.

Disadvantages of Type Conversion in C++

  • Typecasting makes the system more complex.
  • Errors can occur due to unexpected casting.
  • Types of casting can be called directly from objects.

Points to remember

  • C++ is a strongly typed language. Means errors can occur if we assign the wrong Data Type to a variable.
  • In C++, There are two types of type conversion: implicit type casting and explicit typecasting.
  • Explicit type casting is further divided into two groups: conversion by assignment and conversion by using cast operators.
  • Static Cast, Dynamic Cast, Constant Cast, Reinterpret cast are four types of conversion using cast operator.
Back to: Learn C++ Programming > Introduction to C++

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.