January 13, 2021

C++ Type Conversion

In this lesson, we are going to cover the basics of type conversion in C++. We would examine various examples to clarify this.

In C++, you can convert data from one type to another (read Data Types here). This is called Type Conversion.

In C++, there are two types of type conversion, namely

  1. Implicit Conversion
  2. Explicit Conversion or Type Casting

 

1. Implicit Type Conversion

This conversion type is done implicitly by the C++ compiler. It is sometimes called automatic conversion.

Let’s take an example.

Example – Conversion from Int to double

// Example of Implicit type conversion

#include <iostream>
using namespace std;

int main() {
   // assigning an int value to int_num
   int int_num = 9;

   // declare a double data type
   double double_num;
 
   // implicit conversion
   // assigning an int value to double variable
   double_num = int_num;

   cout << "Integer value = " << int_num << endl;
   cout << "Double value = " << double_num << endl;

   return 0;
}

Output

Integer value = 15
Double value = 15

In this example, we assign an int value to a double variable. The int value is automatically converted into a double by the C++ compiler before it is assigned to the double_num variable. This is a typical example of implicit type conversion

 

Example 2 – Implicit conversion from double to int

#include <iostream>
using namespace std;

int main() {
   // assigning an int value to int_num
   int int_num;

   // declare a double data type
   double double_num = 12.75;

   // implicit conversion
   // assigning a double value to an int variable
   int_num = double_num;

   cout << "Integer value = " << int_num << endl;
   cout << "Double value = " << double_num << endl;

   return 0;
}

Output

Integer value = 12
Double value = 12.75

In this case, the double value is implicitly converted to an int type by the compiler before it is assigned to the int_num variable.

It is important to not that the the value was truncated after the conversion since int value cannot contain decimals.

As a rule: when a conversion is made from a larger data type to a smaller one, there would be data loss.

 

1. Explicit Conversion in C++

In this type, we manually write the code to convert from one data type to another. This is also known as type casting. There are three key ways to carry out explicit conversion, namely:

  • Using C-style type casting(or cast notations)
  • Function notation
  • Type conversion operators

 

Using C-style type casting(or cast notations)

This is an inherited type casting method from the C programming language. Some examples are given below:

// initialize an int variable
int num_int = 54;

// declare double variable
double num_double;

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

 

Function notationĀ 

This is achieved using type casting functions. An example is given below

// initialize an int variable
int num_int = 56;

// declare double variable
double num_double;

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

 

Type conversion operators

There are additional four operations use for type conversion C++. They are

  • static_cast
  • dynamic_cast
  • const_cast
  • reinterpret_cast

We would cover these operators later in subsequent tutorial.

One thought on “C++ Type Conversion

Leave a Reply

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