Object
Oriented
Programming
Using
C ++
Data Conversion/Type Casting
Abdul Wahab
Lecturer
University of Science and Technology Bannu
[email protected]
1
Type Casting
Converting one data type into another data type is called ‘Type Casting’
or ‘Data Conversion’
We have the following types of Data Conversion
1. Conversion from Basic data type to Basic data type
2. Conversion from Basic data type to User-defined data type ( class type)
3. Conversion from Class type to basic data type
4. Conversion from one Class type to another class type
2
1. Conversion from Basic to Basic data type
Implicit data conversion
conversion by the compiler
Explicit data conversion
conversion by the user
3
Example:
Implicit Data Conversion Explicit Data Conversion
void main() void main()
{ {
clrscr(); clrscr();
int x=3 ; int x = 66 ;
char z;
char y='A'; char y = 'A';
z = x + y; cout<< char(x) <<endl;
cout<<z; cout<< int(y)
} }
Ouput Ouput
D B
65
4
2. Conversion from Basic data type to User-defined data type
The conversion from basic to class type is easily carried out.
It is automatically done by the compiler with the help of in-built routines or
by applying typecasting.
In this type the left-hand operand of = sign is always class type and right-
hand operand is always basic type.
The program given below explains the conversion from basic to class
type.
5
Example:
class data
{
int x; void main()
float f; {
public : clrscr();
data()
data Z;
{ x=0; f=0; }
Z=1;
data(float m) Z.show();
{
x=2; Z=5.6;
f=m;
Z.show();
}
}
void show()
{
cout<<"\nX= "<<x<<" F="<<f;
}
};
6
Output
7
3. Conversion from Class type to basic data type
In this type of conversion, the programmer explicitly needs to tell the
compiler how to perform conversion from class to basic type.
These instructions are written in a member function. Such type of
conversion is also known as overloading of type cast operators.
8
3. Conversion from Class type to basic data type
The compiler first searches for the operator keyword followed by data
type and if it is not defined, it applies the conversion functions.
In this type, the left-hand operand is always of basic data type and right-
hand operand is always of class type. While carrying this
conversion, the statement should satisfy the following conditions:
– The conversion function should not have any argument.
– Do not mention return type.
– It should be a class member function.
9
Example:
class data void main()
{
{
int x;
float f; clrscr();
int j;
public : float k;
data() data Z;
{ x=0; f=0;}
Z=6.6;
data(float m)
{ x=2; f=m; } j=Z;
k=Z;
operator int() cout<<"\n Value of j: "<<j;
{ return (x); }
cout<<"\n Value of k: "<<k;
operator float() }
{ return (f); }
};
10
Output
11
4. Conversion from one Class type to another Class type
When an object of one class is assigned to object of another class, it is
necessary to give clear-cut instructions to the compiler.
Consider the following example:
X = A;
Here X is an object of class XYZ and A is an object of class ABC. The
class ABC data type is converted to class XYZ.
The conversion happens from class ABC to XYZ. The ABC is a source
class and XYZ is a destination class.
12
4. Conversion from one Class type to another Class type
We know the operator function operator data-type(). Here, data type may
be built-in data type or user-defined data type.
In the above declaration, the data-type indicates target type of object.
13
Example:
class hours
class minutes
{
{
int m; float h;
public : public:
minutes() void operator = (minutes x)
{ { h = x.get() / 60.0; }
m=240;
void show ()
}
{ cout<< "\n Hours = "<<h; }
get() };
{ void main()
return (m); {
}
clrscr();
void show() minutes m;
{ hours h;
cout <<"\n Minutes = "<<m; h = m;
} m.show(); h.show();
};
}
14
Output
15
Have a Good Day!