C++ Type Conversion
In this tutorial, we will learn about the basics of C++ type conversion, types of conversion, implicit conversion and explicit conversion with the help of examples.
Type Conversion
C++ allows us to convert data of one type to that of another. This is known as type conversion. Type conversion is basically a way to convert an expression of a given type into another type. When a variable is converted into a different type, the compiler basically treats the variable as of new data type.
Types of Type conversion
There are two types of type conversion in C++.
1) Implicit Conversion
2) Explicit Conversion (also known as Type Casting)
C++ Implicit Type Conversion
1) The type conversion that is done automatically done by the compiler is known as implicit type conversion.
2) The implicit conversion is also known as automatic conversion.
3) Generally takes place when in an expression more than one data type is present. In such condition type conversion takes place to avoid lose of data.
4) All the data types of the variables are upgradad to the data type of the variable with largest data type.
5) It is possible to lose information in implicit conversions, signs can be lost (when signed is converted to unsigned), and overflow can occur (when long long is converted to float).
Let us look at two examples of implicit type conversion.
Example 1: Conversion From int to double
// Example1: Working of implicit type-conversion
#include <iostream>
using namespace std;
int main() {
// assigning an int value to age_int
int age_int = 2;
// declaring a double type variable
double age_double;
// implicit conversion
// assigning int value to a double variable
age_double = age_int;
cout << "age_int = " << age_int << endl;
cout << "age_double = " << age_double << endl;
return 0;
}
Output
age_int = 2 age_double = 2
In the program, we have assigned an int
data to a double
variable.
age_double = age_int;
Here, the int
value is automatically converted to double
by the compiler before it is assigned to the age_double variable.
Example 2: Automatic Conversion from double to int
//Example2: Working of Implicit type-conversion
#include <iostream>
using namespace std;
int main() {
int distance_int;
double distance_double = 3.33;
// implicit conversion
// assigning a double value to an int variable
distance_int = distance_double;
cout << "distance_int = " << distance_int << endl;
cout << "distance_double = " << distance_double << endl;
return 0;
}
Output
distance_int = 3 distance_double = 3.33
distance_double = distance_int;
Here, the double
value is automatically converted to int
by the compiler before it is assigned to the distance_int variable.
Note: Since int
cannot have a decimal part, the digits after the decimal point are truncated in the above example.
Data Loss During Conversion (Narrowing Conversion)
1) As we have seen from the above example, conversion from one data type to another is prone to data loss.
2) This happens when data of a larger data type is converted to data of a smaller data type.
C++ Explicit Type Conversion
When the user manually changes data from one type to another, this is known as explicit conversion. The Explicit conversion is also known as type casting. Here, the user can typecast the result to make it of a particular data type.
There are three major ways in which we can use explicit conversion in C++. They are:
1) C-style type casting (also known as cast notation)
2) Function notation (also known as old C++ style type casting)
3) Type conversion operators
1. C-style Type Casting
As the name suggests, this type of casting is favored by the C programming language. It is also known as cast notation.
The syntax for -style Type Casting is:
(data_type)expression;
For example,
// initializing int variable
int num_int = 20;
// declaring double variable
double num_double;
// converting from int to double
num_double = (double)num_int;
2. Function-style Casting
We can also use the function like notation to cast data from one type to another.
The syntax for this style is:
data_type(expression);
For example,
// initializing int variable
int num_int = 20;
// declaring double variable
double num_double;
// converting from int to double
num_double = double(num_int);
Example 3: Type Casting
#include <iostream>
using namespace std;
int main() {
// initializing a double variable
double num_double = 5.12;
cout << "num_double = " << num_double << endl;
// C-style type conversion from double to int
int num_int1 = (int)num_double;
cout << "num_int1 = " << num_int1 << endl;
// function-style type conversion from double to int
int num_int2 = int(num_double);
cout << "num_int2 = " << num_int2 << endl
return 0;
}
Output
num_double = 5.12 num_int1 = 5 num_int2 = 5
We used both the C style type conversion and the function-style casting for type conversion and displayed the results. Since they perform the same task, both give us the same output.
3. Type Conversion Operators
Cast operator is an unary operator which forces one data type to be converted into another data type. Besides these two type castings, C++ also has four operators for type conversion. They are known as type conversion operators.
They are:
static_cast
dynamic_cast
const_cast
reinterpret_cast
We will learn about these cast operators in later tutorials.
Advantages of Type-Conversion
1) This is done to take advantage of certain features of type hierarchies or type representations.
2) It helps to compute expressions containing variables of different data types.
Recommended Tutorials:
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Type Conversion in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Operators
.