How many types of data conversion are there in C++?

What is Data Conversion in C++

How many types of data conversion are there in C++?  The equality operator is used to assign a constant value to a variable or a one variable value to another variable. For example if we write

floatabc=floatbcd;

it means the value of float type variable ‘bcd’ is assigned to the float type variable ‘abc’. The general rule of thumb is to assign the value only of the same type. Similarly it is also possible to assign value of one user-defined object to another object.  One the value of one object is assigned to another object, then the values of data items are automatically copied in that object. There is no need of specific instruction to the compiler to perform such types of assignments.

What are the data conversion types in C++?

So the conversion between basic data types and user defined data types are easy as long as the both data types are same on = operator. But what happens when the data types are different on both sides of = operator? Is it possible to perform such kinds of conversions? Luckily such types of conversions are possible but practically they are no of much use.

Conversion between basic data types (float, int, double etc)

Conversion between basic data types is also known as implicit conversion. The basic data types are int, float, double, long int etc. Consider the following example.

floatvariable=intvariable;

Here variable of int type is being converted into float first. For implementing such kinds of conversion, the compiler has its built-in routines that are called by the compiler in such cases.  Cast operator can also be used to force the compiler to perform conversions between float and double or float and int and many more. conversion through casting are called explicit conversion. Its syntax is as follows:

floatvar=static_cast<float>(intvariable);

Both implicit and explicit conversion techniques use the same built-in routines.

Conversion between objects and basic data types (int, float,double, etc)

While performing conversion between objects and basic data types we need to provide more information to the compiler. As the compiler does not know much about user-defined data type except what we tell it about it. Lets have look of following example

Example

class travel

{

private:

const float metertofeet;

int ft;

float inch;

public:

travel(): ft(0),inch(0.0),metertofeet (4.56789F)

{

}

travel(float meters): metertofeet(4.56789F)

{
float fitfeet=metertofeet*meters;

ft=int(fitfeet);

inch=12*(fitfeet-ft):

}

travel (int feet, float in): ft (feet), inch(in), metertofeet(4.56789F)

{

}

void getdistance()

{

cout<<“enter the value of feet”<<endl;

cin>>ft;

cout<<“enter the value of inches”<<endl;

cin>>inch;

}

void showdata()const

cout<<ft<<inch<<endl;

operator float () const

{

float fractionfeet=inches/12;

fractionfeet+=static_cast<float>(ft);

return fractionfeet/metertofeet;

}

};

void main()

{

clrscr();

float mtrss;

travel distance1=2.15F;

cout<<“distance1.showdata()<<endl;

cout<<mtrss<<endl;

travel distance2( 6,7.6);

mtrss=distance2;

cout<<mtrss<<endl;

distance2=mtrss;

getch();

}

in above example the conversion between a user defined data type meter and objects distance is performed.

Conversion from basic data type to user-defined data type

Such conversion is performed by using a constructor with one argument in it. The constructors that perform this type of conversion are called conversion constructors.

travel(float meters): metertofeet(4.56789F)

{
float fitfeet=metertofeet*meters;

ft=int(fitfeet);

inch=12*(fitfeet-ft):

}

when an object of the class is created then the constructor is executed automatically. Here is it assumed as the distance is in meters and it will be converted into feet and inches.

Conversion from user-defined data type to basic data type

conversion operator performs this task.

operator float()

{

float fractionfeet=inches/12;

fractionfeet+=float(ft);

return fractionfeet/metertofeet;

}

The operator takes object value, converts it to a float and then returns this value.

Conversion between C-strings and strings objects

This is a very interesting case where both approaches are used i.e., conversion operator and constructor with one argument. Lets consider an example for understanding this concept.

This example will depict conversion between ordinary string and and string class.

class string

{

private:

enum{abc=90};

char array[abc];

public:

string()

{ array[0]=’\0′;

}

string(char g[])

{

strcpy(array,g);

}

void showdata() const

{

cout<<array;

}

operator char*()

{

return array;

}

};

void main()

{

clrscr();

char sxtr1[]=”hello world”;

z1=sxtr1;

z1.showdata();

string z2=”i am pretty good”

cout<<static_cast<char*>(z2);

cout<<endl;

getch();

}

Also read here

How to implement stacks in c++?

 

Leave a Reply

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