 The process of 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)
 It convert data 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.
 In this case, 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.
Expression Intermediate Type
Char + Float Float
Int - Long Long
Int * Double Double
Float / Long Double Long Double
 Suppose x is 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.
 #include<iostream>
Using namespace std;
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;
}
The Equality is 1.
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.
#include<iostream>
Using namespace std;
int main()
{
int x, y;
A a(10);
B b=a;
b.getXY(x,y);
cout<<"x: "<<x<<" y: "<<y<<endl
return 0;
}
 It can be 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.
 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;
}
 In the above 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;
 Traditional explicit type 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.
 There are Four 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.
 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.
 cout<<“The End”;

Data Type Conversion in C++

  • 2.
     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.
  • 6.
    Expression Intermediate Type Char+ Float Float Int - Long Long Int * Double Double Float / Long Double Long Double
  • 7.
     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.
  • 10.
    #include<iostream> Using namespace std; intmain() { int x, y; A a(10); B b=a; b.getXY(x,y); cout<<"x: "<<x<<" y: "<<y<<endl return 0; }
  • 11.
     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.
  • 17.