0% found this document useful (0 votes)
212 views5 pages

Type Conversion

Conversion between data types in C++ can occur in four ways: 1) Conversion from one basic type to another like int to float can be done implicitly through type conversion or explicitly through type casting. 2) Conversion from a basic type to a class type uses a constructor with a single parameter of the basic type. 3) Conversion from a class type to a basic type overloads the unary type casting operator() and specifies the basic return type. 4) Conversion between class types may involve multiple constructors and type conversion operators.

Uploaded by

Vinay Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views5 pages

Type Conversion

Conversion between data types in C++ can occur in four ways: 1) Conversion from one basic type to another like int to float can be done implicitly through type conversion or explicitly through type casting. 2) Conversion from a basic type to a class type uses a constructor with a single parameter of the basic type. 3) Conversion from a class type to a basic type overloads the unary type casting operator() and specifies the basic return type. 4) Conversion between class types may involve multiple constructors and type conversion operators.

Uploaded by

Vinay Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

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