M.Preetha
M.Sc(Information Technology)
NADAR SARASWATHI COLLEGE OF
ARTS &SCIENCE
The process of converting one predefined
type into another is called type conversion.
When constants and variable of different
type are mixed in an expression they are
converted the same type.
One type are mixed with variable of
another type.
 The type of the data to the right of an
assignment operator is automatically
converted type of the variable on the left.
Int m;
Float x=3.1419;
M=x;
Implicit type conversion
Explicit type conversion
 An implicit type conversion is a conversion
Performed by the compiler without programmer
intervention.
 An implicit conversion is applied differing data
type intermixed an expression.
Ch=x; ch and char
 If either operand is of type long double other
converted to long double.
 If either operand is float the other converted to
float.
 The either operand is unsigned long other
converted to unsigned long.
 The constructor build a string type of object
From a char* type variable a.
 The variable length and p are data member
of the class string.
String::string(char *a)
{
Length=strlen(a);
P=new char[length+1];
Strcpy(p,a);
} c
Class time
{
Int hrs;
Int mins;
Public:
……
……
Time (int t) //constructor
{
Hours=t/60; //t in minutes
Mins=t%60;
}
};
 The constructor fine a type conversion from
A basic to class type.
 The constructor function do not support
this operation.
 C++ allows overloaded casting operator
used to convert class type data to a basic
type.
Operator type name()
{
.........
………
………
}
 Data conversion techniques from a basic to
class type and class to basic type.
Ex:
Objx=objY;//objects of different type
 ObjX is an object of class x and objy is an
object of class y .
Operator typename()
#include<iostrteam.h>
#include<conio.h>
Void main()
{
Clrscr();
Float res;
Float f1=15.5,f2=2;
Res=(int)f1/(int)f2;
Cout<<res<<end1;
Res=(int)(f1/f2);
Cout<<res<<end1;
Res=f1/f2;
Cout<<res;
Getch();
}
Type  conversion

Type conversion

  • 1.
  • 2.
    The process ofconverting one predefined type into another is called type conversion. When constants and variable of different type are mixed in an expression they are converted the same type. One type are mixed with variable of another type.  The type of the data to the right of an assignment operator is automatically converted type of the variable on the left. Int m; Float x=3.1419; M=x;
  • 3.
  • 4.
     An implicittype conversion is a conversion Performed by the compiler without programmer intervention.  An implicit conversion is applied differing data type intermixed an expression. Ch=x; ch and char
  • 5.
     If eitheroperand is of type long double other converted to long double.  If either operand is float the other converted to float.  The either operand is unsigned long other converted to unsigned long.
  • 6.
     The constructorbuild a string type of object From a char* type variable a.  The variable length and p are data member of the class string. String::string(char *a) { Length=strlen(a); P=new char[length+1]; Strcpy(p,a); } c
  • 7.
    Class time { Int hrs; Intmins; Public: …… …… Time (int t) //constructor { Hours=t/60; //t in minutes Mins=t%60; } };
  • 8.
     The constructorfine a type conversion from A basic to class type.  The constructor function do not support this operation.  C++ allows overloaded casting operator used to convert class type data to a basic type. Operator type name() { ......... ……… ……… }
  • 9.
     Data conversiontechniques from a basic to class type and class to basic type. Ex: Objx=objY;//objects of different type  ObjX is an object of class x and objy is an object of class y . Operator typename()
  • 10.
    #include<iostrteam.h> #include<conio.h> Void main() { Clrscr(); Float res; Floatf1=15.5,f2=2; Res=(int)f1/(int)f2; Cout<<res<<end1; Res=(int)(f1/f2); Cout<<res<<end1; Res=f1/f2; Cout<<res; Getch(); }