Introduction
• In an expression constants and variables of different types can be mixed.• The type of data to the right of an assignment operator is automatically converted into the type of the variable on the left.
For example:
int m;
float x=4.45667;
m=x;
• Converts x to an integer before its value is assigned to m.
• Thus, the fractional part is truncated.
• The type conversions are automatic for the data types involved are built-in type.
• Lets discuss further, what happens when they are user-defined data types.
• Let C1,C2 and C3 are the user defined data types(objects) and C3 = C1 +C2; the automatic conversion will not happen here.
• When the objects are of the same class type, the operations of assignment are carried out smoothly and the compiler does not give any error message.
• The assignment of data items are handled by the compiler with no effort on the part of the programmer, whether they are basic or user defined if both the source and destination data items are of the same data-type.
• In case the data items are of different types, data conversion mechanism must be explicitly specified by the user.
• These include:
– Conversions between basic and user-defined types
– Conversions between the user defined to user defined types.
Conversion between basic data types
• Consider the statement
float x;
int y,z;
y=x+z;
• Here following automatic conversions take place for statement y=x+z;
1. z is converted into float - float(z)
2. The sum x+z is performed
3. The result of x+z is converted into int and assign to y since y is int.
• For the basic types, the automatic conversion takes place from smaller size to higher size data when get together with an operator.
• Exception: The assignment operator (=) where conversion always takes place from right side type to left side type.
• Eg. int x; float y;
x =y; // conversion will takes place from float to int since left side variable x is of type int and right side variable y is of type float.
• However we can use type cast operators to explicitly convert one basic data types into another data types.
• E.g. int x,y; float z; y=x+ int(z);
or y= x+ (int)z; // c – like type casting
• In above statement z is explicitly directed to convert into int before addition operation.
Conversion between Objects and Basic types
• The compiler does not know anything about the logical meaning of user defined data types.
• So compiler cannot perform automatic conversion between basic types and user defined types(objects)
• The user cannot rely on the compiler to perform conversion from user-defined data types to basic data types and vice-versa.
• To perform meaningful conversion, the user must supply the necessary conversion function.
Conversion basic to user-defined data types- The Constructor Function
• To convert data from a basic type to a user-defined type, the conversion function should be defined in user-defined class in the form of the constructor.
• The constructor function takes a single argument of basic data type as:
constructor_name(Basic_Type argv)
{
//converting statements
}
Basic to User-defined: Example
Conversion user-defined to basic data types – Operator function
• The conversion function should be defined in user-defined class in the form of the operator function.• The operator function is defined as an overloaded basic data type which takes no arguments.
• The basic type itself will be recognized as the return type of operator function.
• It converts the data members of an object to basic data types and returns a basic data-item.
Example: Conversion from User-Defined to Basic type
Conversion between objects of different classes (User-Defined Type to User-Defined Type)
• The C++ compiler does not support automatic data conversion between objects of user-defined classes.• So programmer should define the conversion routine in any one class to conversion of one type object to another type.
• Consider the following:
classA obj_a;
classB obj_b;
……………
obj_a = obj_b;
• The conversion method can be either defined in classA or classB depending on whether it should be one-argument constructor or an operator function.
Example: Degree to Radian with Operator function
Conversion Routine in Source object: operator function
Example : continued
Conversion Routine in Destination Object: Constructor Function
• The conversion routine can be defined in the destination object’s class as a one-argument constructor using belowsyntax
• For objects of classA and classB obj_a and obj_b respectively, in an assignment statement such as,
obj_a=obj_b;
• Here , obj_b is the source object of the class classB and obj_a is the destination object of the class classA.
• The conversion constructor function classA(classB obj_b) exists in the destination object’s class executes on assignment and converts obj_b into type of obj_a.