Type Conversion in C++

The data type of any variable tells us about the value you can store in that variable and what operations you can perform on it. It might be possible that one operation is supported by more than one data type and to handle such cases C++ allows us to convert one datatype to another datatype. This process of converting data type is known as Type Conversion or Type Casting.


In C++, there are two types of Type Conversion:

  • Implicit Type Conversion.
  • Explicit Type Conversion.


Implicit Type Conversion in C++.

Implicit type conversion is done automatically by the compiler wherever it is required in the program. It takes place when more than one data type is present in an expression. 

Let's understand with some examples of implicit type conversion. 

//Example of Implicit Type Conversion
#include<iostream>
using namespace std;

int main(){

  //conversion of int to bool type
  int value = 25;
  bool check = value;

  cout<<check<<endl;

  //conversion of double to int
  double pi = 3.14;
  int i = pi;

  cout<<i<<endl;

  //conversion of signed to unsigned
  unsigned int num = -1;

  cout<<num<<endl;

  return 0;
}
Output:
1
3
4294967295

We have observed the following points from the above example:
  • When we assign one of the non-boolean arithmetic types to a boolean object, the result is false if the value is 0 and true otherwise. 
  • When we assign a boolean to one of the arithmetic types, the result value is 1 if the boolean is true and 0 if the boolean is false. 
  • When we assign a floating-point value to an object of integral type, the value is truncated. It means the value before the decimal point will get stored. 
  • When we assign an integral value to a floating type then the fraction part is zero. 
  • When we assign a signed value to an unsigned value then it stores 2's complement of the value. 

You will observe from the above code that there is a chance of losing information in implicit type conversion. For example, when we try to store a floating value in an integer value we lose the value present after the decimal point. This usually happens when data of a larger type get converted to a smaller type. 

You can follow the below chart to check the Data Lose During Type Conversion.
  
Type Conversion Chart
Data Lose During Type Conversion

Explicit Type Conversion in C++.

In Explicit Type Conversion user manually change the data type from one type to another type based on the requirement. This process is also known as Type Casting. 

There are multiple ways for performing Explicit type conversion and here we are going to discuss them one by one.
  • Forceful Casting: In this type of casting you explicitly define the required datatype in front of the expression. 
syntax: (data_type) expression; 

//Example of Explicit Type Conversion
#include<iostream>
using namespace std;

int main(){

  float x = 3.14;

  int ans = (int)x * 4 * 4;
  cout<<ans<<endl;

  return 0;
}
Output:
48

Type Conversion Operators

C++ have four operators for performing type casting and they are:
  • Static cast
  • Dynamic cast
  • Const cast
  • Reinterpret cast
Example:
//C++ Example of Operator Type Conversion
#include<iostream>
using namespace std;

int main(){

  float x = 3.14;

  int ans = static_cast<int>(x);
  cout<<ans<<endl;

  return 0;
}
Output:
3

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS