In this section, we will explore the transformation of a data type into another within the C++ programming language. Data type conversion involves changing the predefined data type of a variable into a suitable alternative. The primary purpose of type conversion is to unify two distinct data type variables into a singular type, facilitating the seamless resolution of mathematical and logical operations without compromising data integrity.
For instance, when adding two numbers, one being an integer type and the other a float type, it is necessary to convert or typecast the integer variable into a float. This ensures that both variables are of the same data type, allowing for their addition.
Type conversion in C++ can be achieved through two methods: implicit type conversion and explicit type conversion. Implicit type conversion, also known as automatic type conversion, is performed by the compiler without requiring any user intervention. On the other hand, explicit type conversion, also referred to as user-defined type conversion, is initiated by the user and involves user interaction. In the context of C++, both implicit and explicit type conversions play crucial roles in data manipulation and program execution.
Implicit Type Conversion
Implicit type conversion refers to the automatic conversion performed by the compiler without manual intervention. This process involves converting one data type to another based on predefined rules within C++ compiler. This mechanism is also recognized as automatic type conversion.
For example:
int x = 20;
short int y = 5;
int z = x + y;
In the provided scenario, there exist two distinct data type variables, namely x and y. Here, x is defined as an integer type, while y is designated as a short integer data type. The resultant variable z is also declared as an integer type, responsible for storing the values of both x and y variables. It is imperative to note that during the addition operation, the C++ compiler inherently converts the lower rank data type (short int) value into a higher type (int) to prevent any potential data loss, overflow, or sign loss that may occur due to implicit type conversion in C++.
Order of the typecast in implicit conversion
The correct sequence of data types from lower precedence to higher precedence is as follows:
bool -> char -> short int -> int -> unsigned int -> long int -> unsigned long int -> long long int -> float -> double -> long double
Program to convert int to float type using implicit type conversion
Let's develop a software to transform lower-ranking data types into higher-ranking types through implicit type conversion.
Program1.cpp
#include <iostream>
using namespace std;
int main ()
{
// assign the integer value
int num1 = 25;
// declare a float variable
float num2;
// convert int value into float variable using implicit conversion
num2 = num1;
cout << " The value of num1 is: " << num1 << endl;
cout << " The value of num2 is: " << num2 << endl;
return 0;
}
Output
The value of num1 is: 25
The value of num2 is: 25
Program to convert double to int data type using implicit type conversion
Let's develop a software application that can automatically convert a higher data type into a lower data type using implicit type conversion.
Program2.cpp
#include <iostream>
using namespace std;
int main()
{
int num; // declare int type variable
double num2 = 15.25; // declare and assign the double variable
// use implicit type conversion to assign a double value to int variable
num = num2;
cout << " The value of the int variable is: " << num << endl;
cout << " The value of the double variable is: " << num2 << endl;
return 0;
}
Output
The value of the int variable is: 15
The value of the double variable is: 15.25
In the provided code snippet, we defined 'num' as an integer variable and 'num2' as a double variable, initializing 'num2' with the value 15.25. Subsequently, we assigned the value of 'num2' to 'num' using the assignment operator. When this conversion occurs, the C++ compiler automatically converts the double value to an integer before assigning it to 'num', resulting in the truncated value of 15 being displayed.
Explicit type conversion
Converting data types with user intervention to change one variable to another is referred to as explicit type conversion. In simpler terms, explicit conversion enables programmers to manually alter or cast the data type from one variable to a different type. This process is commonly known as typecasting. Explicit type conversion is typically enforced to convert data from one type to another when it does not adhere to the implicit conversion rule.
The process of explicit type conversion is categorized into two methods:
- Direct conversion utilizing the cast operator
- Direct conversion employing the assignment operator
Program to convert float value into int type using the cast operator
In the C++ programming language, a cast operator is a unary operator that explicitly transforms one data type into another data type.
Let's explore an illustration of converting a float data type to an integer type using the cast operator for explicit conversion in the C++ programming language.
Program3.cpp
#include <iostream>
using namespace std;
int main ()
{
float f2 = 6.7;
// use cast operator to convert data from one type to another
int x = static_cast <int> (f2);
cout << " The value of x is: " << x;
return 0;
}
Output
The value of x is: 6
Program to convert one data type into another using the assignment operator
Let's explore an illustration where we change the data type of a variable to another using the assignment operator within a C++ code.
Program4.cpp
#include <iostream>
using namespace std;
int main ()
{
// declare a float variable
float num2;
// initialize an int variable
int num1 = 25;
// convert data type from int to float
num2 = (float) num1;
cout << " The value of int num1 is: " << num1 << endl;
cout << " The value of float num2 is: " << num2 << endl;
return 0;
}
Output
The value of int num1 is: 25
The value of float num2 is: 25.0