Tuesday, December 15, 2009

TYPE CONVERSIONS IN C++


Three types of type conversions are possible in C++

1. Class type to Basic type
2. Basic type to Class type
3. One class type to another class type

Basic types refer to fundamental data types like int , float , double , char etc. Class type refers to a user defined class type.We use the terms "source type" to denote the initial type and the term "destination type" to denote the final converted type.

The type conversions can be performed in two ways using

1. Costructor(only when the destination type is class type)
2. Overloaded Operator function(only when the source type is a class type)

depending on the type of the conversion.

Class type to Basic type:
The conversion from a class type to a basic type can be performed only using an overloaded operator function.It cannot be done using a constructor,since the destination type is a basic type.

Consider the following example,

class test
{

int x;
public:
test(int y)
{
x=y;
}
operator int()
{
return x;
}

};

void main()
{

int d;
test t(100);
d=t; //calls operator function.converts "test" type to "int" type
//d has value 100

}

Here we have converted a class type to a basic type.While writing the operator function,keep in mind the following points,

1) The operator function should not be explicitly specified a return type. However the operator function should return the destination type,but the return type should not be mentioned in the function declaration.

2)The operator function should be defined inside the class.

Basic type to Class type :
The conversion from a basic type to a class type can be performed using a constructor.It cannot be done using an operator function since the source type is a basic type.

Consider the following example,

class test
{

int x;
public:
test()
{
}
test(int y)//constructor for type conversion
{
x=y;
}

};

void main()
{

int d=100;
test t;
t=d; //calls the constructor

}

Here we have converted the "int" type to "test" type ie from a basic type to a class type.

Class type to Class type :
Conversion between two class types can be done by using either a constructor or a operator function.

Using a constructor :
The conversion is done in the constructor of the destination class.

Consider the following example
//Conversion from metre to centimetre

class centimetre;
class metre
{

float m;
public:
metre(float x)
{
m=x;
}
friend class centimetre;
};

class centimetre
{

float cm;
public:
centimetre()
{
}
centimetre(metre ob) // constructor for type conversion
{
centimetre temp;
temp.cm=ob.m * 100;
return temp;
}

};

void main()
{

metre d(12.345);
centimetre t;
t=d; //conversion from metre to centimetre
//t object has a centimetre of 1234.5

}

Thus we have converted from one class type to another class type using a constructor . Remember to perform the type conversion in the destination class constructor.

Using operator function :
The operator function should be given in the source class.

Consider the following example,
// conversion from metre to centimetre

class centimetre;
class metre
{

float m;
public:
metre(float x)
{
m=x;
}
operator centimetre();

};

class centimetre
{

float cm;
public:
centimetre()
{
}

friend class metre;

};

metre::operator centimetre() //operator function for type conversion
{
centimetre temp;
temp.cm= m*100;
return temp;
}

void main()
{

centimetre t;
metre r(12.345);
t=r; //calls the operator function
//t object has a centimetre of 1234.5

}

Thus we have converted one class type to another class type using a operator function. Remember to use the operator function inside the source class.

No comments:

Post a Comment