Introduction to Type Casting in C++
As the name itself suggest, typecasting means the conversion of a variable type to another. In other words, making an integer type variable to act like another such as character for a single set of operation when required. So basically there are two types of conversion in C++. One is known as Implicit Type conversion whereas the other one is Explicit Type conversion. Implicit conversion is also known as automatic type conversion because it is done by the compiler on its own without any user intervention. If more than one data type is present implicit will work.
Let’s have a look at the syntax for defining typecasting in C++:
Syntax:
int num1;
float num2;
num2 = (float) num1;
In the above syntax, the value of num1 has promoted from int to float directly which is also known as standard conversion.
There is one more type of typecasting in C++ which is known as conversion using the cast operator which is like a unary operator that also convert from one to another data type. There are basically 4 sub-types of casting in cast operator.
- Static Cast: It is used to cast a pointer of base class into derived class.
- Dynamic Cast: It is used in runtime casting.
- Constant Cast: It is used in explicitly overriding constant in a cast.
- Reinterpret Cast: It is used to change a pointer to any other type of pointer.
Examples of Type Casting in C++
Now we will see how exactly type conversion works in C++ in both implicit and explicit conversion way through C++ programs with explanation in detail.
Example #1
here is c++ program to demonstrate the working of implicit & explicit type conversion:
Code:
#include <iostream>
using namespace std ;
int main()
{
int a = 15 ;
char b = 'c' ;
a = a + b ; // implicitly conversion of a.
float z = a + 3.0 ; // implicitly conversion of z
cout << " The Value of a is = " << a << endl
<< " The Value of b is = " << b << endl
<< " The Value of z is = " << z << endl ;
return 0 ;
}
Output:
As you can see in the above code in the main we declared an integer with value equals to 15 and then a character b whose value is equal to c. After that, we are changing the value of a to a plus b which is an example of implicit type conversion and in the second part where we are adding a float value 3.0 to the declared integer a which is also an example of implicit type conversion in C programming. Finally, we are printing the converted values of a, b, and z on the user screen.
Example #2
Here is c++ program to demonstrate the working of explicit type casting:
Code:
#include <iostream>
using namespace std ;
int main()
{
double a = 52.20 ;
// Explicit conversion from double to int
int total = (int)a + 10 ;
// Explicit conversion from double to float
float total1 = (float)a + 2.0 ;
cout << " The total value of a after conversion is = " << total ;
cout << " \n The total value of a after conversion is = " << total1 ;
return 0 ;
}
Output:
As you can see in the above code in the main we declared a double “ a ” with value equals to 52.20 and then an integer variable named “ total ” which will help in the explicit conversion of double value to an integer value. After that one float is declared with the name “ total1 ” which will also help in type conversion but from double to float. After that, we are changing the value of total to a plus 10 which is an example of explicit type conversion and in the second part where we are adding a float value 2.0 to total1. Finally, we are printing the converted values of an on the user screen.
Example #3
Here is c++ program to demonstrate the working of explicit typecasting:
Code:
#include <iostream>
using namespace std ;
class school
{
private :
int id_number ;
public :
school( int r ) : id_number( r )
{
}
void function() const
{
( const_cast <school*> (this) )->id_number = 92 ;
}
int getid_number()
{
return id_number ;
}
};
int main(void)
{
school sc( 2 ) ;
cout << " The previous id_number number is : " << sc.getid_number() << endl ;
sc.function() ;
cout << " The current id_number number is : " << sc.getid_number() << endl ;
return 0 ;
}
Output:
As you can see in the above code in the main we declared a private integer variable named “ id_number ” which will help in the explicit conversion of value during the function call. After that, we have declared a public constructor with the same class name “ school ” in which we are passing an argument named “ r ” of the integer data type. After that, we are declaring a constant function with name “ function () ” for changing the value of id_number with the help of const_cast. Then we are returning the casted value of id_number through int getid_number () function. Finally, we are printing the converted values of id_number on the user screen though int main by creating an object “ sc ” of class school. We are printing both the id_number values in which first is the oldest value and the second one is the current id_number value which is shown after typecasting using cout function.
Conclusion
Typecasting plays an important role in programming irrespective of programming language because when we want to perform the same operation again and again but for different data types then typecasting save a huge amount of execution time and helps in calculating expressions containing different data types of variable.
Recommended Articles
This is a guide to Type Casting in C++. Here we also discuss the Introduction and how type casting works in c++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –