Typecasting in C++

Written by Ali on. Posted in C++

One of the most powerful features  C++ language offers is typecasting or type conversion. As we have seen the basics of most of the things, so before we move on, it is imperative that we know what typecasting really is. Type Casting is actually the conversion of one data type into another one. This feature is very powerful, and yet at the same moment, very dangerous, and is systems way of dealing with various data types. 

Typecasting can also be understood as the editing of information in such a way by the system that saves or deletes extra information. It needs to be handled very carefully to allow the code to execute successfully and achieve the objectives at the same time. There are three types of typecasting:

1.Implicit Typecasting.

2 .Explicit Typecasting.

3. Type promotion.

Implicit Typecasting:

Implicit type casting is when you don’t typecast intentionally and it happens due to the built in feature of the language. This type of type casting is dangerous and can lead to some serious troubles when strictly dealing with different data types.

</p>
<p>int main()</p>
<p>{</p>
<p>float a = 2.456;</p>
<p>int b = 0;</p>
<p>b = a;    //typecasting occurs here. The int type doesn't supports data types that have a decimal, so it deletes that part.</p>
<p>cout << a << endl << b;</p>
<p>}</p>
<p>

 

Explicit Typecasting:

This type of type casting is somewhat safer method of practice and can prove to be really helpful if correctly executed. This is achieved as shown in the following code. Explicit helps in a way that it rounds off the value and thus saves the information to be saved that has otherwise been edited.

 

</p>
<p>int main()</p>
<p>{</p>
<p>float a = 2.567;</p>
<p>int b = 0;</p>
<p>b = (int ) a;    //type casting occurs here and the explicit type rounds off the value and saves the information.</p>
<p>cout << a << endl << b;</p>
<p>}</p>
<p>

 

Type Promotion:

This type of typecasting comes to play when, usually, one is dealing with multiple data types and in functions. It promotes the type that is being used to store the value. It also depends upon the type of data type being returned and the type of the data type that is storing that returned value when dealing with functions. The following code will do its bit in explaining this feature.

</p>
<p>double function()</p>
<p>{</p>
<p>int a = 1000;</p>
<p>float b = 2.554;</p>
<p>return a + b;</p>
<p>}</p>
<p>&nbsp;</p>
<p>int main()</p>
<p>{</p>
<p>int a = function();    //now here, the data type of the storing value controls the typecasting</p>
<p>cout << <a style="text-decoration: none ; box-shadow: none ; outline : none ; font: inherit ; cursor:default;color: inherit" href="http://badocams.com/">this web</a> a;</p>
<p>}</p>
<p>

And here’s another example.

</p>
<p>int main()</p>
<p>{</p>
<p>int a = 1000;<br />
float b = 2.5643;</p>
<p>double c = a + b;<br />
cout << c;</p>
<p>}</p>
<p>

Now the answer here will surprise you. Think why did that happened?

Typecasting is valid for all data types that are present in the system. However, they need to be dealt with utter most care when one’s working with pointers. I will show you how to do that in the next post.

Tags: , ,