Class To Basic-WPS Office
Class To Basic-WPS Office
The constructor functions do not support conversion from a class to basic type. C++ allows us to
define a overloaded casting operator that convert a class type data to basic type. The general form of
operator typename ( )
//Program statmerit .
This function converts a class type data to typename. For example, the operator double( ) converts a
double sum = 0 ;
for(int I = 0; ioize;
return sqrt(sum);
}
by the object and therefore, the values used for, Conversion inside the
function belongs to the object that invoked the function. As a result function
In the string example discussed earlier, we can convert the object string to char* as follows:
return (str) ;
We have just seen data conversion techniques from a basic to class type and a class to basic type. But
sometimes we would like to convert one class data type to another class type.
Example
Objl is an object of class one and Obj2 is an object of class two. The class two type data is converted
to class one type data and the converted value is assigned to the Objl. Since the conversion takes
place from class two to class one, two is known as the source and one is known as the destination
class.
Such conversion between objects of different classes can be carried out by either a
constructor or a conversion function. Which form to use, depends upon where we want the type-
conversion function to be located, whether in the source class or in the destination class.
Operator typename( )