C++ : Type Conversions

We know that when constants and variables of different types are mixed in an expression, C applies automatic type conversion to the operands as per certain rules.Similarly,an assignment operation also causes the automatic type conversion.THe type of data to the right of an assignment operator is automatically converted to the type of the variable on the left.For example,the statement

int m;
float x = 3.4159;
m = x;

convert x to an integer before its value is assigned to m.Thus,the fractional part is truncated.The type conversion are automatic as long as the data types involved are built-in types.
What happens when they are user-defined data types?

COnsider the foloowing statement that adds two objects and then assigns the result to a third object.

v3 = v1 + v2;       // v1, v2 and v3 are class type objects

when the object are of the same class type, the conversion of addition and assignment are carried out smoothly and the compiler does make any complaints.Since the user-defined data types are designed by us to suit our requirements, the compiler does not support automatic type conversions for such data type.We must design the conversion routines by ourselves,if such operation are required.
Three type of situation might arise in the data conversion between uncompatible types:

1. Conversion from basic type to class type.
2. Conversion from class type to basic type
3.Conversion from one class type to another class type

Basic to Class Type

The conversion from basic type to class type is easy to accomplish.It may be recalled that the use of constructors was illustrated in a number of example to initialized objects.For example, a constructor was used to build a vector object from a int type array.Similarly, we used another constructor to build a string type object from char* type variable.These are all examples where constructors perform a defacto type conversion from the arguments type to the constructor class type.
Consider the following constructor:


string :: string(char *a)
length = strlen(a);
p = new char[length+1];


This constructor builds a string type object from a char* type variable a. The variables length and p are data members of theb class string.Once this constructor has been defined in the string class,it can be used for conversion from char* type to string type.Example,

string s1, s2;
char* name1 = “PC”;
char* name2 = “Laptop”;
s1 = string(name1);
s2 = name2;

The statement

s1 = string(name1);

first convert name1 from char* type to string type and then assigns the string type values to the object s1.The statement
s2 = name2;
also does tha same job by invoking the constructor implicitly.
Let us consider the another exampe of converting an int type to class type


class time
int hrs;
int mins;

time(int t)
hrs = t/60;        // t in minutes
mins = t%60;


The following conversion statement can be used in a function:

time T1;
int duration = 85;
T1 = duration;

After this conversion, the hrs member of T1 will contain a value of 1 and mins member contains a value of 25, denoting 1 hour and 25 minutes.

Class to Basic Type

The constructor function do not support this operation.C++ allows us to define an overloaded casting operator that could be used to convert a class type data to a basic type.
The general form of an overloaded casting operator function, usually referred to as a conversion function, is


operator typename()

…        // function statement



This function converts a class type data to typename.For example, the operator double() converts a class object to type double, the operator int() converts a class type object to type int and so on.
Consider the following conversion function:


vector :: operator double()
double sum = 0;
for(int i=0; i<size; i++);
sum = sum + v[i] * v[i];
return sqrt(sum);


This function converts the vector to the corresponding scalar magnitude.Recall that the magnitude of a vector is given by the square root of the sum of the squares of its components.The operator double() can be used as follows:

double length = double(V1);
double length = V1;

where V1 is an object of type vector.Both the statement have exactly the same effect.When the compiler encounters a statement that requires the conversion of a class type to a basic type, it quietly calls the casting operator function to do the job.
The casting operator function should satisfy the following consitions:

1. It must be a class member
2. It must not specify a return type
3. It must not have any arguments

Since it a member function, it is invoked by the object and therefore the values used for conversion inside the function belong to the object that invoked the function. This means that the function does not need an arguments.
In the string example described in the previous section, we can do the conversion from string to char* as follows:

string :: operator char*()

One Class to Another Class Type

We have justg seen tghe data conversion techniques from basic to class type and a class to basec type But there are situations where we would like to convert one class type to another class type

objX = objY;        // Object of different types

objX is an object of class X and objY is an object of class Y.The class Y type data is converted to the class X type data and the converted value is assigned to the objX.Since the conversion takes place from class Y to class X, Y is known as the source class and X is known as destination class.

Conversion required

Source Class

Destination Class

Basic -> Class

Not applicable


Class -> Basic

Casting Operator

Not Applicable

Class -> Class

Casting Operator


Leave a Comment

Your email address will not be published. Required fields are marked *