Loading....
Coupon Accepted Successfully!

 

Type Conversion

We learnt that when constants and variables of various data types are clubbed in a single expression, automatic type conversion takes place. This is so for basic data types. The compiler is unknown about the user-defined data type and about their conversion to other data types. The programmer should write the routines that convert basic data type to user-defined data type or vice versa. There are three possibilities of data conversion as given below:

  1. Conversion from basic data type to user-defined data type (class type)
  2. Conversion from class type to basic data type
  3. Conversion from one class type to another class type

Conversion from Basic to Class Type

The conversion from basic to class type is easily carried out. It is automatically done by the compiler with the help of in-built routines or by applying type casting. In this type, the left-hand operand of = sign is always class type and the right-hand operand is always basic type. The below-given program explains the conversion from basic to class type.

10.13 Write a program to define constructor with no argument and with float argument. Explain how compiler invokes constructor depending on data type.

 

Explanation: In the above program, the class data has two member variables each of integer and float types respectively. It also has two constructors one with no argument and the other with float argument. The member function show() displays the contents of the data members. In function main(), z is an object of class data. When z is created, the constructor with no argument is called and data members are initialized to zero. When z is initialized to one, the constructor with float argument is invoked. The integer value is converted to float type and assigned member variable f. Again when z is assigned to 2.5, same process is repeated. Thus, the conversion from basic to class type is carried out.

Conversion from Class Type to Basic Data Type

In the previous example, we studied how compiler makes conversion from basic to class type. The compiler does not have any knowledge about the user-defined data type built using classes. In this type of conversion, the programmer needs to explicitly tell the compiler how to perform conversion from class to basic type. These instructions are written in a member function. Such type of conversion is also known as overloading of type cast operators. The compiler first searches for the operator keyword followed by data type and if it is not defined, it applies the conversion functions. In this type, the left-hand operand is always of basic data type and the right-hand operand is always of class type. During this conversion, the statement should satisfy the following conditions:

  1. The conversion function should not have any argument.
  2. Do not mention return type.
  3. It should be a class member function.

10.14 Write a program to convert class type data to basic type data.

 

Explanation: In the above program, the class data has two member variables each of integer and float data type. It also contains constructors as per described in the last example. In addition, it contains overloaded data types int and float. These functions are useful for conversion of data from class type to basic type. Consider the following statements:

 

(a) j=a;

(b) f=a;


In the first statement object a is assigned to integer variable j. We know that class type data is a combination of one or more basic data types. The class contains two member functions operator int() and operator float(). Both these function are able to convert data types from class to basic. In statement (a), variable j is of integer type, the function operator int() is invoked and integer value data member is returned. In statement (b), f is of float type, the member function operator float() is invoked.

Conversion from One Class Type to Another Class Type

We learnt how to convert basic data type to class type and vice versa. Now the third method is conversion from class type to another class type. When an object of one class is assigned to object of another class, it is necessary to give clear-cut instructions to the compiler about how to make conversion between these two user-defined data types. The method must be instructed to the compiler. There are two ways to convert object data type from one class to another. One is to define a conversion operator function in source class or a one-argument constructor in a destination class. Consider the following example:

 

X=A;


Here, X is an object of class XYZ and A is an object of class ABC. The class ABC data type is converted to class XYZ. The conversion happens from class ABC to XYZ. The ABC is a source class and XYZ is a destination class.


We know the operator function operator data-type(). Here, data type may be built-in data type or user-defined data type. In the above declaration, the data type indicates target type of object. Here, the conversion takes place from class ABC (source class) to class XYZ (destination class).

10.15 Write a program to convert integer to date and vice versa using conversion function in source class.

 

Explanation: In the above program, date and dmy are two classes declared. In function main(), D1 is an object of class date and D2 is an object of class dmy. The object D2 is initialized.

The statement D1=D2 initializes D1 with D2. Here, both the objects D1 and D2 are of different types hence the conversion function date() is called to perform the conversion from one object to another object.


10.16 Write a program to convert integer to date and vice versa using conversion function in destination class.

Explanation: In the above program, as soon as the statement D1=D2 is executed, the one-argument constructor defined in the class date is invoked. The one-argument constructor carries the conversion. The constructor calls three-member function of dmy class to get the day, month, and year of the date. Here, in this program conversion is done by using the constructor in destination object.

10.17 Write a program to convert class data type to another class data type.

 

Explanation: In the above program two classes are declared. The class minutes has one integer member variable m and two member functions get() and show(). It also contains constructor without argument. The class hours has one integer member variable and show() member function. The class hours contain overloaded operator function. In function main(), minute is an object of class minutes and hour is an object of class hours. The program converts minutes to hours. The equation hour=minute invokes the operator function. In function operator(), x is an object of class minutes. The object x invokes the member function get() that returns total number of minutes. Number of hours is obtained by dividing the total number of minutes by 60. The equation h=x.get()/60 performs this task and assigns result to h. Thus, the result of the program is as per given above.





Test Your Skills Now!
Take a Quiz now
Reviewer Name