Your browser doesn't support JavaScript Type Conversions - Windows Programming

Type Conversions

C++ supports two types of conversions

Implicit Type Conversion

Is an automatic type conversion by the compiler. Implicit type conversion happens automatically when a value is copied to a compatible data type.  It is possible for implicit conversions to lose information for instance when a signed is implicitly converted to unsigned and when a long is implicitly converted to float.

#include<stdio.h>
int main()
{
short a=50;
int b;
b=a; //implicit type casting
printf("%d\n",a);
printf("%d\n",b);
}

Explicit Type Conversion  

Also known as “type-casting” is a user-defined conversion with the programmer explicitly defining the casting.  The process is usually associated with information loss. 

Explicit conversion can be performed by using an assignment operator or by using a cast operator

Using the  Assignment Operator

The Assignment operator forces one data type to be converted into another and consists of the type name before an expression

#include <iostream>
using namespace std;
int main()
{ 
double x= 3.14159265359; 
int y = (int)x;  //using cast operator (int)
cout<<"x = "<<x <<"\n";
cout<<"y = "<<y;
return 0;
}

Using Cast Operator

C++ supports four types of casting using four specific casting operators: static_cast, dynamic_cast, reinterpret_cast,  and const_cast.

Static Cast – Used for implicit conversions between types such as int to float.  It also performs conversions between pointers of related classes by ‘upcasting’ from a derived to a base class and ‘downcasting’ from a base to a derived class.

#include <iostream>
using namespace std;
int main()
{     
double p = 3.14159265359; 
cout<<"Before casting: pi = "<<p<<endl;   
int total = static_cast<int>(p); 
cout <<"After static_cast:total = "<<total;
}

Dynamic Cast – Dynamic cast is a runtime cast performed only on class pointers and references. The most common use for dynamic casting is converting(downcasting) a base-class pointer into a derived class.

#include <iostream>
using namespace std;
class base {public: virtual void bmethod(){}};
class derived:public base{};
int main()
{  
base* b = new derived; 
derived* d = dynamic_cast<derived*>(b);    
if(d != NULL)      
cout<<"Dynamic cast successful"; 
else      
cout<<"Dynamic cast not successful";
}

Reinterpret Cast – reintepret_cast works on pointers and converts a pointer of any type to any other pointer type without checking if the pointer or the data pointed to by the pointer is the same or not. 

#include <iostream>
using namespace std; 
int main()
{ 
int* ptr = new int(100); 
char* ch = reinterpret_cast<char*>(ptr);   
cout << *ptr << endl; 
cout << *ch << endl; 
return 0;
}

Const Cast – const_cast removes the constant attribute from references and pointers.  It is not permitted to const_cast variables that are const.

#include <iostream>
using namespace std;
int main(void)
{
const double x = 3.14159265359;
const double* y = &x;
cout << "old value is " << * y << "\n";
double* z=const_cast<double *>(y);
*z=3.14;
cout<<"new value is "<<*y;
}