Type Casting In C++
Topic asked in February 2022 (CBCS) , July 2022 (CBCS) and March 2021 (CBCS) question paper.
Type casting, also known as type conversion, is the process of converting a variable from one data type to another. This is a common operation in programming, especially when dealing with different types of data or when performing operations that require operands of a certain type.
Implicit Casting
Implicit casting, also known as automatic type conversion, occurs when the compiler automatically converts one data type to another without requiring any explicit instructions from the programmer. This type of casting typically happens when there's a compatible type conversion, such as converting a smaller data type to a larger one, or when the conversion is part of an expression involving operands of different types.
For Example:-
In this example, the integer value 7
is implicitly converted to a double
when assigned to num2
.
C++ Code:-
Output:
Explanation:-
- In this example, num1 is an integer (int) variable with a value of 10, and num2 is a floating-point (double) variable with a value of 3.5.
- The expression num1 + num2 involves adding an integer (num1) to a floating-point number (num2).
- The compiler automatically converts num1 from an integer to a double before performing the addition because the operation requires operands of the same type.
- The result of the addition is stored in the variable result, which is also of type double.
- When the program is executed, it prints the result of the addition, which is 13.5.
Explicit Casting
Explicit casting involves the programmer explicitly instructing the compiler to perform a type conversion. This type of casting is used when there's a need to convert between types that wouldn't happen automatically, such as converting from a larger data type to a smaller one, or when converting between incompatible types.
In C++, explicit type conversion, also known as explicit type casting, can indeed be done in two primary ways:
- Converting by Assignment
- Conversion Using Cast Operator
Converting by Assignment
This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting.
Syntax:- (type) expression
For Example:-
Output:-
Conversion Using Cast Operator
A cast operator is a unary operator that forces one data type to be converted into another data type. In C++, there are several casting operators available for explicit type conversion:
- static_cast: Used for general type conversions between related types, such as numeric types or pointer types. It performs checks at compile time.
- dynamic_cast: Used for casting polymorphic types during runtime. It performs runtime checks to ensure the safety of the conversion.
- const_cast: Used to add or remove const or volatile qualifiers from variables.
- reinterpret_cast: Used to convert between unrelated types, such as converting pointer types of different classes. It's highly unsafe and should be used with caution.