The document discusses data type conversion in C++. It explains that data type conversion can be either implicit (automatic) or explicit (user-defined). Implicit conversion occurs automatically during operations with mixed data types, changing operands to the larger type. Explicit conversion requires a cast operator to manually change a value's type. Four main cast operators are discussed: dynamic_cast, static_cast, reinterpret_cast, and const_cast.
The processof converting the data type of
a value in another data type is known as
data type conversion.
The conversion can be performed in two
ways:
Implicit (Automatic)
Explicit (User-defined)
3.
It convertdata type to another data
type automatically.
Implicit conversion do not required any
operator.
The operands in arithmetic operation
must be of similar types.
An expression in which different data
types is called Mixed-type expression.
4.
In thiscase, the result of an expression is
evaluated to larger data type in the
expression .
Suppose an expression contains an
integer and double as operands.
The result will be evaluated to double
data type.
Suppose xis an integer and y is long and
we want to add up these two variables.
x+y.
In the above data type of x is lower then
data type y. So the value of x will be
changed or converted int long during
executation.
8.
#include<iostream>
Using namespacestd;
Int main()
{
Int iv=16777217;
Float fv=16777216.0;
Cout<<“The Integer value is=“<<iv;
Cout<<“The Float value is=“<<fv;
Cout<<“The Equality is=“<<iv==fv;
Return 0;
}
9.
The Equality is1.
This odd behavior is caused by an implicit
cast of iv to float when it is compared
with fv; a cast which loses precision,
making the values being compared the
same. This odd behavior is caused by an
implicit cast of iv to float when it is
compared with fv; a cast which loses
precision, making the values being
compared the same.
It canbe converted or performed by the
programmer .
It will be performed by the using of cast
operator or operators.
Cast operator tells the computer to
convert the data type of a value.
Syntax : (type)expression .
Type : It indicates the data type to which
operand is to be converted.
12.
Expression :It indicates the constant,
variable or expression or whose data
type is to be converted.
Example:
#include<iostream>
Using namespace std;
Int main() {
float a,b;
int c;
c=(int)a % (int)b;
Cout<<“Result is”<<c;
Return 0;
}
13.
In theabove program two float value
are declared .
Above Program generate an error
because of using the reminder operator
with float values.
Reminder operator can be used with
integer values so we converted these
values in int data type.
(int)a + (int)b;
14.
Traditional explicittype casting allow to
convert any pointer into any other
pointer type, independently of type they
point to.
Pointer : It is a simple variable which is
declared by using the asterisk sign
before the variable.
15.
There areFour Types of casting:
dynamic_cast : It can be used only with
pointers and references to object.
Static_cast : It can perform conversions
between pointers to related classes, not
only from the derived class to its base,
but also from a base class to its derived.
16.
reinterpret_cast :It convert any pointer
type to any other pointer type, even of
unrelated classes.
const_cast : It minipulates the constness
of an object, either to be set or to be
moved.