C++ type conversion

Type casting

In this article, you will learn about the concept of the type casting in the C++ language.

Typecasting is the conversion of the data type of the expression into the other data type.Type casting is mainly two types 
  1. Implicit conversion
  2. Explicit conversion

Implicit conversion

Implicit conversion does not need any additional work, All the work done by the compiler automatically.
These conversions of the data done by the compiler are known as implicit conversions.

During the fundamental type conversion in the c++ language, Implicit conversion is very helpful.such as type conversion (int to float,float to int,double to int,short to int ,and some pointers conversion).

How implicit conversion use in c++?

Example:

#include<iostream>
using namespace std;

int main()
{
int num=92;               //declare and define the int number
double number2;        //declare double number
        
number2=num;           //assign the int number to the double number
cout<<" integer number "<<num<<endl;        //display the int number
cout<<" floating number "<<number2<<endl;        //display the double number

return 0;
}

Output:
92
92

Here, the compiler automatically converts the type of the integer to the double by just assigning the value of int to the double number. This is also called an automatic conversion.
This expression number2=num;  cause of the implicit conversion.


Example: Conversion double to int

#include<iostream>
using namespace std;

int main()
{
int number;                                //Declare int number
double number2=9.999;            //Declare and define double number
number=number2;                    //Assigning the double to the int number
cout<<"float number "<<number2<<endl;                //Display the double number
cout<<" integer number "<<number<<endl;                  //Dispaly the int number

return 0;
}

Output:

9.999
9

Here, Notice that when number converts from the double to the int then decimal part of the number truncate due to type casting. Because there is no decimal part in the int numbers and the compiler automatically truncates the decimal part of the number.

Take care of the data lost during the type casting in the program.

What is type conversion?

Type conversion operator

C++  has four operators for the type casting.Following are the type conversion operators of the c++ language.
const_cast
dynamic_cast
static_cast
reinterpret_cast

Explicit conversion

If the user wants to type casting manually then c++ language provides three-way to type casting
  1. c-style casting
  2. functional casting
  3. Type conversion operators 

C-style conversion

C-style is used in the c language for the type casting and C language is the mother language of the c++ language that's why c-style is used in the c++ language.

The syntax of the c-style type casting.

(datatype)expression;

Functional conversion

 In this conversion,functional style is used to type casting in the c++ language.The syntax of the function-style conversion is same like c-style :

datatype(expression);

Example:

#include<iostream>
using namespace std;

int main()
{
int number,number1=5;
double number2=9.999;
//function-style conversion
number=int(number2);                                        //conversion double to int
number2=double(number1);                                  //conversion int to double
cout<<" float number ="<<number2<<endl;         //display float number
cout<<" int number ="<<number<<endl;              //dispaly int number
//c-style of conversion
number1=8;
number2=3.33;
number=(int)number2;                           //conversion double to int
number2=(double)number1;                //conversion int to double
cout<<" float number ="<<number2<<endl;        //display the float number
        cout<<" int number ="<<number<<endl;              //dispaly the int number

return 0;
}

Output:
float number =5
int number =9
float number =8
int number =3


Please write the comment,if you find anything incorrect or you want to share more information about the above topic.

Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

Diferrence between flutter and react native