{"id":176,"date":"2020-08-29T21:04:35","date_gmt":"2020-08-29T21:04:35","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=176"},"modified":"2020-08-29T21:04:35","modified_gmt":"2020-08-29T21:04:35","slug":"c-type-conversion","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-type-conversion\/","title":{"rendered":"C++ Type Conversion"},"content":{"rendered":"<p>In this lesson, we are going to cover the basics of type conversion in C++. We would examine various examples to clarify this.<\/p>\n<p>In C++, you can convert data from one type to another (<a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-data-types\/\" target=\"_blank\" rel=\"noopener noreferrer\">read Data Types here<\/a>). This is called Type Conversion.<\/p>\n<p>In C++, there are two types of type conversion, namely<\/p>\n<ol>\n<li><a href=\"#t1\">Implicit Conversion<\/a><\/li>\n<li><a href=\"#t2\">Explicit Conversion or Type Casting<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Implicit Type Conversion<\/strong><\/h4>\n<p>This conversion type is done implicitly by the C++ compiler. It is sometimes called automatic conversion.<\/p>\n<p>Let&#8217;s take an example.<\/p>\n<p><strong>Example &#8211; Conversion from Int to double<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Example of Implicit type conversion<\/span>\r\n\r\n<span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n   <span style=\"color: #888888;\">\/\/ assigning an int value to int_num<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> int_num <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">9<\/span>;\r\n\r\n   <span style=\"color: #888888;\">\/\/ declare a double data type<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">double<\/span> double_num;\r\n \r\n   <span style=\"color: #888888;\">\/\/ implicit conversion<\/span>\r\n   <span style=\"color: #888888;\">\/\/ assigning an int value to double variable<\/span>\r\n   double_num <span style=\"color: #333333;\">=<\/span> int_num;\r\n\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Integer value = \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> int_num <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Double value = \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> double_num <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Integer value = 15\r\nDouble value = 15\r\n<\/pre>\n<p>In this example, we assign an int value to a double variable. The int value is automatically converted into a double by the C++ compiler before it is assigned to the double_num variable. This is a typical example of implicit type conversion<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 2 &#8211; Implicit conversion from double to int<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n   <span style=\"color: #888888;\">\/\/ assigning an int value to int_num<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> int_num;\r\n\r\n   <span style=\"color: #888888;\">\/\/ declare a double data type<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">double<\/span> double_num <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">12.75<\/span>;\r\n\r\n   <span style=\"color: #888888;\">\/\/ implicit conversion<\/span>\r\n   <span style=\"color: #888888;\">\/\/ assigning a double value to an int variable<\/span>\r\n   int_num <span style=\"color: #333333;\">=<\/span> double_num;\r\n\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Integer value = \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> int_num <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Double value = \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> double_num <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Integer value = 12\r\nDouble value = 12.75<\/pre>\n<p>In this case, the double value is implicitly converted to an int type by the compiler before it is assigned to the int_num variable.<\/p>\n<p>It is important to not that the the value was truncated after the conversion since int value cannot contain decimals.<\/p>\n<p>As a rule: when a conversion is made from a larger data type to a smaller one, there would be data loss.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">1. Explicit Conversion in C++<\/strong><\/h4>\n<p>In this type, we manually write the code to convert from one data type to another. This is also known as type casting. There are three key ways to carry out explicit conversion, namely:<\/p>\n<ul>\n<li>Using C-style type casting(or cast notations)<\/li>\n<li>Function notation<\/li>\n<li>Type conversion operators<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Using C-style type casting(or cast notations)<\/strong><\/p>\n<p>This is an inherited type casting method from the C programming language. Some examples are given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ initialize an int variable<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> num_int <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">54<\/span>;\r\n\r\n<span style=\"color: #888888;\">\/\/ declare double variable<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> num_double;\r\n\r\n<span style=\"color: #888888;\">\/\/ convert from int to double<\/span>\r\nnum_double <span style=\"color: #333333;\">=<\/span> (<span style=\"color: #333399; font-weight: bold;\">double<\/span>)num_int;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Function notation\u00a0<\/strong><\/p>\n<p>This is achieved using type casting functions. An example is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ initialize an int variable<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> num_int <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">56<\/span>;\r\n\r\n<span style=\"color: #888888;\">\/\/ declare double variable<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> num_double;\r\n\r\n<span style=\"color: #888888;\">\/\/ convert from int to double<\/span>\r\nnum_double <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333399; font-weight: bold;\">double<\/span>(num_int);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Type conversion operators<\/strong><\/p>\n<p>There are additional four operations use for type conversion C++. They are<\/p>\n<ul>\n<li>static_cast<\/li>\n<li>dynamic_cast<\/li>\n<li>const_cast<\/li>\n<li>reinterpret_cast<\/li>\n<\/ul>\n<p>We would cover these operators later in subsequent tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we are going to cover the basics of type conversion in C++. We would examine various examples to clarify this. In C++, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-176","post","type-post","status-publish","format-standard","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/176\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}