Type Casting in C++
Type Casting in C++
Type casting refers to the conversion of one data type to another in a program.
Typecasting can be done in two ways: automatically by the compiler and manually by
the programmer or user. Type Casting is also known as Type Conversion.
For example, suppose the given data is an integer type, and we want to convert it into
float type. So, we need to manually cast int data to the float type, and this type of
casting is called the Type Casting in C++.
int num = 5;
float x;
x = float(num);
x = 5.0
2nd example:
Type Casting is divided into two types: Implicit conversion or Implicit Type Casting and
Explicit Type Conversion or Explicit Type Casting. 10s
1. char - sort int -> int -> unsigned int -> long int -> float -> double -
> long double, etc.
Note: Implicit Type Casting should be done from low to higher data types. Otherwise, it
affects the fundamental data type, which may lose precision or data, and the compiler
might flash a warning to this effect.
1. (type) expression;
type: It represents the user-defined data that converts the given expression.
For example, we have a floating pointing number is 4.534, and to convert an integer
value, the statement as:
int num;
num = (int) 4.534; // cast into int data type
cout << num;
When the above statements are executed, the floating-point value will be cast into an
integer data type using the cast () operator. And the float value is assigned to an
integer num that truncates the decimal portion and displays only 4 as the integer value.
Let's create a simple program to cast one type variable into another type using the
explicit type casting in the C++ programming language.
#include <iostream>
using namespace std;
int main ()
{
// declaration of the variables
int a, b;
float res;
a = 21;
b = 5;
cout << " Implicit Type Casting: " << endl;
cout << " Result: " << a / b << endl; // it loses some information
return 0;
}
Output:
In the above program, we take two integer variables, a and b, whose values are 21 and
2. And then, divide a by b (21/2) that returns a 4 int type value.
In the second expression, we declare a float type variable res that stores the results of
a and b without losing any data using the cast operator in the explicit type cast
method.
Not in Syllabus
In type cast, there is a cast operator that forces one data type to be converted into
another data type according to the program's needs. C++ has four different types of
the cast operator:
1. Static_cast
2. dynamic_cast
3. const_cast
4. reinterpret_cast
Static Cast:
The static_cast is a simple compile-time cast that converts or cast one data type to
another. It means it does not check the data type at runtime whether the cast
performed is valid or not. Thus the programmer or user has the responsibility to ensure
that the conversion was safe and valid.
The static_cast is capable enough that can perform all the conversions carried out by
the implicit cast. And it also performs the conversions between pointers of classes
related to each other (upcast - > from derived to base class or downcast - > from base
to derived class).
Let's create a simple example to use the static cast of the type casting in C++
programming.
#include <iostream>
using namespace std;
int main ()
{
// declare a variable
double l;
l = 2.5 * 3.5 * 4.5;
int tot;
cout << " Before using the static cast:" << endl;
cout << " The value of l = " << l << endl;
return 0;
}
Output:
Dynamic Cast
The dynamic_cast is a runtime cast operator used to perform conversion of one type
variable to another only on class pointers and references. It means it checks the valid
casting of the variables at the run time, and if the casting fails, it returns a NULL value.
Dynamic casting is based on RTTI (Runtime Type Identification) mechanism.
Let's create a simple program to perform the dynamic_cast in the C++ programming
language.
#include <iostream>
using namespace std;
class parent
{
public: virtual void print()
{
}
};
class derived: public parent
{
};
int main ()
{
// create an object ptr
parent *ptr = new derived;
// use the dynamic cast to convert class data
derived* d = dynamic_cast <derived*> (ptr);
Output:
The reinterpret_cast type casting is used to cast a pointer to any other type of pointer
whether the given pointer belongs to each other or not. It means it does not check
whether the pointer or the data pointed to by the pointer is the same or not. And it
also cast a pointer to an integer type or vice versa.
#include <iostream>
using namespace std;
int main ()
{
// declaration of the pointer variables
int *pt = new int (65);
cout << " The value of pt: " << pt << endl;
cout << " The value of ch: " << ch << endl;
Output:
Const Cast
The const_cast is used to change or manipulate the const behavior of the source
pointer. It means we can perform the const in two ways: setting a const pointer to a
non-const pointer or deleting or removing the const from a const pointer.
#include <iostream>
using namespace std;
// define a function
int disp(int *pt)
{
return (*pt * 10);
}
int main ()
{
// declare a const variable
const int num = 50;
const int *pt = # // get the address of num
Output: