Type Conversion In C++

Type Conversion

The type conversions are automatic as long as the data types involved are built-in types. If the data types are user-defined, the compiler does not support automatic type conversion and therefore, we must design the conversion routines by ourselves. Three types of situations might arise in the data conversion in this case. 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


Conversion from basic type to class type 

        In this case, it is necessary to use the constructor. The constructor, in this, takes a single argument whose type is to be converted. For example,

class distance 
{ 
private: 
int feet; 
int inch; public: 
distance(int f,int i) 
{ 
feet=f; 
inch=i; 
} 
distance(float m) 
{ 
feet = int(m); 
inch = 12 * (m - feet); 
} 
void display() 
{ 
cout<<"Feet = "<<feet<<endl <<"Inch = "<<inch; 
}
};

void main()
{ 
clrscr(); 
float f = 2.6; 
distance d = f; 
d.display();
getch(); }

Output: Feet = 2 Inches = 7

Conversion from class type to basic type

In this case, it is necessary to overload the casting operator. To do this, we use the conversion function. 
Example: 

class distance 
{ 
private: int feet; 
int inch; public: 
distance(int f,int i) 
{ 
feet=f; 
inch=i; 
}
operator float() 
{ 
float a= feet + inches/12.0;
return a; } };

void main() 
{ 
clrscr(); 
distance d(8, 6); 
float x = (float)d; cout<<"x = "<<x; 
getch();
}
Output: x = 8.5

Conversion from one class type to another class type

This type of conversion can be carried out either by a constructor or an operator function. It depends upon where we want the routine to be located – in the source class or in the destination class. a. Function in the source class: In this case, it is necessary that the operator function is placed in the source class.
For example,

class distance 
{ 
int feet; 
int inch; 
public: 
distance(int f, int i) 
{ 
feet = f; 
inch = i; 
}
void display()
{ 
cout<<"Feet = "<<feet<<endl<<"Inches = "<<inches; 
} };
class dist
{ 
int meter;
int centimeter; 
public: 
dist(int m, int c) 
{ 
meter = m; 
centimeter = c; 
}
operator distance( ) 
{ 
distance d; 
int f,i; 
f= meter*3.3.; 
i=centimeter*0.4; 
f=f+i/12; 
i=i%12; 
return distance(f,i); 
} };
void main() 
{ 
clrscr();
distance d1; 
dist d2(4,50); 
d1=d2; 
d1.display();
getche(); }

b. Function in the destination class:

In this case, it is necessary that the constructor is placed in the destination class. This constructor is a single argument constructor and serves as an instruction for converting the argument’s type to the class type of which it is a member. 
For example,

class distance
{ 
int meter;
float centimeter; 
public: 
distance(int m, int c) 
{ 
meter = m; 
centimeter = c; 
}
int getmeter() 
{ 
return meter; }
float getcentimeter() 
{ 
return centimeters; 
} };
class dist
{ 
int feet; int inch;
public: 
dist(int f, int i) 
{ 
feet = f; 
inch = i; 
}
dist(distance d) 
{ 
int m,c; 
m=d.getmeter(); 
c=d.getcentimeter(); 
feet= m*3.3; 
inch= c*0.4; 
feet=feet+inch/12; 
inch= inch%12; 
} 
void display() 
{ 
cout<<"Feet = "<<feet<<endl<<"Inches = "<<inches; 
} };
void main( ) 
{ 
clrscr(); 
distance d1(6,40); 
dist d2; 
d2=d1; 
d2.display();
getche();
}

Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming