Type Casting in C++This section will discuss the type casting of the variables in the C++ programming language. 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++. 2nd example: Type Casting is divided into two types: Implicit conversion or Implicit Type Casting and Explicit Type Conversion or Explicit Type Casting. Implicit Type Casting or Implicit Type Conversion
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.Program to use the implicit type casting in C++ Let's create an example to demonstrate the casting of one variable to another using the implicit type casting in C++. Output: Implicit Type Casting The value of x: 200 The value of y: 200 Type casting char to int data type ('a' to 20): 117 Type casting from int data to float type: 85 In the above program, we declared a short data type variable x is 200 and an integer variable y. After that, we assign x value to the y, and then the compiler automatically converts short data value x to the y, which returns y is 200. In the next expressions, we declared an int type variable num is 20, and the character type variable ch is 'a', which is equivalent to an integer value of 97. And then, we add these two variables to perform the implicit conversion, which returns the result of the expression is 117. Similarly, in the third expression, we add the integer variable num is 20, and the character variable ch is 65, and then assign the result to the float variable val. Thus the result of the expression is automatically converted to the float type by the compiler. Explicit Type Casting or Explicit Type Conversion
Syntax of the explicit type casting type: It represents the user-defined data that converts the given expression. expression: It represents the constant value, variable, or an expression whose data type is converted. For example, we have a floating pointing number is 4.534, and to convert an integer value, the statement as: 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. Program to demonstrate the use of the explicit type casting in C++ Let's create a simple program to cast one type variable into another type using the explicit type casting in the C++ programming language. Output: Implicit Type Casting: Result: 4 Explicit Type Casting: The value of float variable (res): 4.2 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. Program to cast double data into int and float type using the cast operator Let's consider an example to get the area of the rectangle by casting double data into float and int type in C++ programming. Output: The length of the rectangle is: 57.3456 The breadth of the rectangle is: 12.9874 The area of the rectangle is: 740 The length of the rectangle is: 57.3456 The breadth of the rectangle is: 12.9874 The area of the rectangle is: 744.77 Some different types of the Type Casting 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:
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). Syntax of the Static Cast Program to demonstrate the use of the Static Cast Let's create a simple example to use the static cast of the type casting in C++ programming. Output: Before using the static cast: The value of l = 39.375 After using the static cast: The value of tot = 39 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. Program to demonstrate the use of the Dynamic Cast in C++ Let's create a simple program to perform the dynamic_cast in the C++ programming language. Output: Dynamic casting is done successfully. Reinterpret Cast Type 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. Syntax of the reinterpret_cast type Program to use the Reinterpret Cast in C++ Let's write a program to demonstrate the conversion of a pointer using the reinterpret in C++ language. Output: The value of pt: 0x5cfed0 The value of ch: A The value of *ptr: 65 The value of *ch: A 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. Syntax of the Const Cast type Program to use the Const Cast in C++ Let's write a program to cast a source pointer to a non-cast pointer using the const_cast in C++. Output: The value of ptr cast: 500 Next TopicBinary Operator Overloading in C++ |