How To Best Implement Type Conversion In C++?

Published on Sep 09,2019 10.5K Views

How To Best Implement Type Conversion In C++?

This article will introduce you to yet another interesting topic that Type Conversion In C++ and follow it with detailed practical demonstration. Following pointers will be covered in this article,

So let us get started then,

Type Conversion In C++

Type Conversion refers to conversion from one type to another. The main idea behind type conversion is to make variable of one type compatible with variable of another type to perform an operation. For example, to find the sum of two variables, one of int type & other of float type. So, you need to type cast int variable to float to make them both float type for finding the sum. In this blog we will learn how to perform type conversion in C++.

In C++, there are two types of type conversion i.e. implicit type conversion & explicit type conversion.

Implicit Type Conversion

Implicit type conversion or automatic type conversion is done by the compiler on its own. There is no external trigger required by the user to typecast a variable from one type to another. 

This occurs when an expression contains variables of more than one type. So, in those scenarios automatic type conversion takes place to avoid loss of data. In automatic type conversion, all the data types present in the expression are converted to data type of the variable with the largest data type.

Below is the order of the automatic type conversion. You can also say, smallest to largest data type for type conversion. 

bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double

Implicit conversions can lose information such as signs can be lost when signed type is implicitly converted to unsigned type and overflow can occur when long is implicitly converted to float.

Now let us look at an example to understand how implicit type conversion works in C++.

Example

#include <iostream>
using namespace std;
int main() 12w
{
int int1 = 100; // integer int1
char char1 = 'c'; // character char1
// char1 implicitly converted to int using ASCII value of 'c' i.e. 99
int1 = int1 + char1;
// int1 is implicitly converted to float
float flt1 = int1 + 2.7;
cout << "int1 = " << int1 << endl
<< "char1 = " << char1 << endl
<< "flt1 = " << flt1 << endl;
return 0;
}

Output

int1 = 199

char1 = c

flt1 = 201.7

Next in this type conversion in C++ article,

Explicit Type Conversion

Explicit type conversion or type casting is user defined type conversion. In explicit type conversion, the user converts one type of variable to another type. Explicit type conversion can be done in two ways in C++:

  • Converting by assignment
  • Conversion using Cast operator

Now let’s look at each of the ways to explicit type cast one type to another.

Converting by assignment

In this type conversion the required type is explicitly defined in front of the expression in parenthesis. Data loss happens in explicit type casting. It is considered as forceful casting. Let’s look at an example.

Example

#include <iostream>
using namespace std;
int main()
{
double dbl1 = 8.9;
// Explicit conversion from double to int
int res = (int)dbl1 + 1;
cout << "Result = " << res;
return 0;
}

Output

Result = 9

Next in this type conversion in C++ article,

Conversion using Cast Operator

Cast operator is an unary operator which forces one data type to be converted into another data type. There are four types of casting in C++, i.e. Static Cast, Dynamic Cast, Const Cast and Reinterpret Cast.

  • Static Cast This is the simplest type of cast which can be used. It not only performs upcasts, but also downcasts. It is a compile time cast. Checks are not performed during the runtime to guarantee that an object being converted is a full object of the destination type.
  • Dynamic Cast – It ensures that a result of the type conversion points to the valid, complete object of the destination pointer type.
  • Const Cast – manipulates that whether the object needs to be constant or non-constant. It ensures that either the constant needs to be set or to be removed.
  • Reinterpret Cast converts any pointer type to any other pointer type, even of unrelated classes. It does not check if the pointer type and data pointed by the pointer is same or not.

Let’s look at an example of static cast,

Example

#include <iostream>
using namespace std;
int main()
{
float flt = 30.11;
// using cast operator
int int1 = static_cast<int>(flt);
cout <<int1;
}

Output

30

This brings us to the end of this article on Type Conversion In C++. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this article and we will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Best Implement Type Conversion In C++?

edureka.co