In this lesson, you will learn how to convert data from one type to another in C++. Type conversion is the term for this operation. We will study the fundamentals of C++ type conversion with examples.
In C++, there are two different methods of type conversion.
Implicit type conversion refers to conversion that occurs automatically during compilation; automatic conversion is another name for this conversion. Consider the following two instances of implicit type conversion.
Implicit type casting includes two types of casting.
A “widening” conversion, also known as a typecast, is a conversion when the “destination” data type has a broader range than the “source” data type. For instance, int to long.
//Example of Widening type-conversion
#include <iostream>
using namespace std;
int main() {
float varFloat = 0;
int varInteger = 50;
varFloat = varInteger;
cout << "Var Float: " << varFloat << endl;
cout << "Var Integer: " << varInteger << endl;
return 0;
}
Output
Var Float: 50 Var Integer: 50
No data loss could occur during this conversion when the “source” data type has a smaller range than the “destination” data type.
Like in the above example, the integer is assigned to float.
Another Widening Casting Example
//Example of Implicit type-conversion (Integer to Float)
#include <iostream>
using namespace std;
int main() {
int varInteger = 1289;
float varFloat;
// Automatic conversion: assigning an int value to a float variable
varFloat = varInteger;
cout << "varInteger = " << varInteger << endl;
cout << "varFloat = " << varFloat << endl;
return 0;
}
A float variable varFloat in the program has an integer variable varInteger assigned to it. The C++ compiler automatically converts the integer value to a float before assigning it to the float variable.
varInteger = 1289 varFloat = 1289
A typecast or narrowing is the opposite of a “widening” conversion. For example, long to int, double to float.
Example
//Example of Narrowing type-conversion
#include <iostream>
using namespace std;
int main() {
float varFloat = 12.53 F;
int varInteger = 0;
varInteger = varFloat;
cout << "Var Float: " << varFloat << endl;
cout << "Var Integer: " << varInteger << endl;
return 0;
}
Output
Var Float: 12.53 Var Integer: 12
Another illustration of narrowing conversion is this. The compiler automatically converts the float value to an integer before assigning it to the integer variable. Following the floating-point example, the digits have been shortened because int cannot have a decimal portion. So, data is lost.
Another Narrowing Casting Example
//Example of Implicit type-conversion (Float to int)
#include <iostream>
using namespace std;
int main() {
int varInteger;
float varFloat = 1289.12;
// Automatic conversion: assigning a float value to an int variable
varInteger = varFloat;
cout << "varInteger = " << varInteger << endl;
cout << "varFloat = " << varFloat << endl;
return 0;
}
This concludes the C++ Implicit Conversion lesson. In the next lesson, you will learn about Explicit Conversion in C++.