You are on page 1of 5

TYPE CONVERSION

Conversion from basic to basic type


Conversion from basic type to class type
Conversion from class type to basic type
Conversion from class type to class type
Basic to Basic

• Conversion from one basic type to another


• Example: int to float, float to int, float to double,[ or
any basic to any basic] etc
• Done with the help of two ways:
 Type Conversion(or implicit conversion): Done by
compiler itself
 Type Casting(or explicit conversion) using cast
operator
BASIC TO USER DEFINED(or CLASS)

• It is accomplished by the use of an appropriate


constructor with one parameter.
• This constructor with one parameter should be the
member function of the class and parameters it
takes must be of basic data type which is to be
converted.
USER DEFINED(or CLASS) TO BASIC
TYPE
• It can be accomplished by overloading the unary
type casting operator().
• The cast operator function must be defined in that
class whose object needs to be converted.
• The overloaded cast operator function is defined by
specifying the keyword operator followed by the
datatype to which conversion is desired.
• The syntax is:
• operator typename(){…}
• Here typename is any of the basic data type.
• This overloded typecast operator function doestnot
have any return type(not even void)
• Because the returntype is the typename to whih
the object is being converted.
• Moreover it does not take any parameter

You might also like