• In this and the following posts I am going to show you one another nice feature provided by the C++ programming language called “Type Conversion”.
  • In C programming language, when different types of constants and variables are used in expression, C automatically perform type conversion based on some fixed rules. In assignment operation, variable at the right hand side is automatically converted to the type of the variable on the left. The Same can also be possible in C++ programming language.
int a ;

float b = 3.14654;

a = b;
  • Here ‘b’ is float variable but it is at the right side in the assignment statement so converted into the integer format.
  • But this automated type promotion will work well if both data types are of primary data type or both are of same user-defined data type. But it will create problem when one data type is user-defined data type and another is primary data type. So for that we have to use some special function for type conversion as in such cases automatic type conversion can not be performed by the language itself.
  • There are three types of type conversion are possible:
  1. Conversion from basic type to the class type.
  2. Conversion from class type to basic type.
  3. Conversion from one class to another class type.

Conversion from Basic type to the Class type

  • In this type of conversion the source type is basic type and the destination type is class type. Means basic data type is converted into the class type.
  • For example we have class employee and one object of employee ‘emp’ and suppose we want to assign the employee code of employee ‘emp’ by any integer variable say ‘Ecode’ then the statement below is the example of the conversion from basic to class type.
emp = Ecode ;
  • Here the assignment will be done by converting “Ecode” which is of basic or primary data type into the class type.

Conversion from Class type to Basic type

  • In this type of conversion the source type is class type and the destination type is basic type. Means  class data type is converted into the basic type.
  • For example we have class employee and one object of employee ‘emp’ and suppose we want to assign the employee code of employee object ‘emp’ to any integer variable say ‘Ecode’ then the statement below is the example of the conversion from class to basic type.
Ecode = emp ;
  • Here the assignment will be done by converting “emp” object which is of class type into the basic or primary data type.

Conversion from one Class to another Class type

  • In this type of conversion both the type that is source type and the destination type are of class type. Means  the source type is of class type and the destination type is also of the class type. In other words, one class data type is converted into the another class type.
  • For example we have two classes one for “computer” and another for “mobile”. Suppose if we wish to assign “price” of computer to mobile then it can be achieved by the statement below which is the example of the conversion from one class to another class type.
mob = comp ;   // where mob and comp are the objects of mobile and computer classes respectively.
  • Here the assignment will be done by converting “comp” object which is of class type into the “mob” which is another class data type.
  • Fore more information on how type conversion achieved please stay tuned.