A user-defined data types are designed by the user to suit their requirements, the compiler does not support automatic type conversions for such data types therefore, the user needs to design the conversion routines by themselves if required.
There can be 3 types of situations that may come in the data conversion between incompatible data types:
- Conversion of primitive data type to user-defined type: To perform this conversion, the idea is to use the constructor to perform type conversion during the object creation. Below is the example to convert int to user-defined data type:
Example:
C++
#include <bits/stdc++.h>
using namespace std;
class Time {
int hour;
int mins;
public :
Time()
{
hour = 0;
mins = 0;
}
Time( int t)
{
hour = t / 60;
mins = t % 60;
}
void Display()
{
cout << "Time = " << hour
<< " hrs and "
<< mins << " mins\n" ;
}
};
int main()
{
Time T1;
int dur = 95;
T1 = dur;
T1.Display();
return 0;
}
|
Output
Time = 1 hrs and 35 mins
- Conversion of class object to primitive data type: In this conversion, the from type is a class object and the to type is primitive data type. The normal form of an overloaded casting operator function, also known as a conversion function. Below is the syntax for the same:
Syntax:
operator typename()
{
// Code
}
- Now, this function converts a user-defined data type to a primitive data type. 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. Below is the program to illustrate the same:
Example:
C++
#include <bits/stdc++.h>
using namespace std;
class Time {
int hrs, mins;
public :
Time( int , int );
operator int ();
~Time()
{
cout << "Destructor is called."
<< endl;
}
};
Time::Time( int a, int b)
{
hrs = a;
mins = b;
}
Time::operator int ()
{
cout << "Conversion of Class"
<< " Type to Primitive Type"
<< endl;
return (hrs * 60 + mins);
}
void TypeConversion( int hour, int mins)
{
int duration;
Time t(hour, mins);
duration = t;
cout << "Total Minutes are "
<< duration << endl;
cout << "2nd method operator"
<< " overloading " << endl;
duration = t.operator int ();
cout << "Total Minutes are "
<< duration << endl;
return ;
}
int main()
{
int hour, mins;
hour = 2;
mins = 20;
TypeConversion(hour, mins);
return 0;
}
|
Output
Conversion of Class Type to Primitive Type
Total Minutes are 140
2nd method operator overloading
Conversion of Class Type to Primitive Type
Total Minutes are 140
Destructor is called.
Output
Conversion of Class Type to Primitive Type
Total Minutes are 140
2nd method operator overloading
Conversion of Class Type to Primitive Type
Total Minutes are 140
Destructor is called.
Now, the function will convert the vector to scalar magnitude. The operator double() can be used as:
double len = double(S1);
Or,
double len = S1;
where S1 is an object of type vector.
Conversion of one class type to another class type: In this type, one class type is converted into another class type. It can be done in 2 ways :
1.Using constructor
2.Using Overloading casting operator
1.Using constructor :
In the Destination class we use the constructor method
//Objects of different types
ObjectX=ObjectY;
Here ObjectX is Destination object and ObjectY is source object
Example:
C++
#include<iostream>
using namespace std;
class CGS
{
int mts;
int cms;
public :
void showdata()
{
cout<< "Meters and centimeters in CGS system:" ;
std::cout << mts<< " meters " <<cms<< " centimeters" << std::endl;
}
CGS( int x, int y)
{
mts=x;
cms=y;
}
int getcms()
{
return cms;
}
int getmts()
{
return mts;
}
};
class FPS
{
int feet;
int inches;
public :
FPS()
{
feet=0;
inches=0;
}
FPS(CGS d2)
{
int x;
x=d2.getcms()+d2.getmts()*100;
x=x/2.5;
feet=x/12;
inches=x%12;
}
void showdata()
{
cout<< "feet and inches in FPS system:" ;
std::cout << feet<< " feet " <<inches<< " inches" << std::endl;
}
};
int main()
{
CGS d1(9,10);
FPS d2;
d2=d1;
d1.showdata();
d2.showdata();
return 0;
}
|
Output
Meters and centimeters in CGS system:9 meters 10 centimeters
feet and inches in FPS system:30 feet 4 inches
2.Using Overloading casting operator
// Objects of different types
objectX = objectY;
- Here we use Overloading casting operator in source class i.e. overloading destination class in source class
See the below example in which we have two classes Time and Minute respectively and will convert one class Time to another Minute class.
In the below example minute class is destination class and time class is source class
so we need to overload the destination class in the source class
Here we should not tell the return type but we returns the overloaded class object
i.e. returning value without specifying return type
C++
#include <bits/stdc++.h>
using namespace std;
class Minute {
public :
int mins;
Minute()
{
mins = 0;
}
void show()
{
cout << "\nTotal Minute : " << mins << endl;
}
};
class Time {
int hr, mins;
public :
Time( int h, int m)
{
hr = h;
mins = m;
}
Time()
{
cout << "\nTime's Object Created" ;
}
operator Minute ()
{
Minute m;
m.mins = (hr * 60) + mins;
return m;
}
void show()
{
cout << "Hour: " << hr << endl;
cout << "Minute : " << mins << endl;
}
};
int main()
{
Time T1(3,40);
Minute m;
m=T1;
T1.show();
m.show();
return 0;
}
|
Output
Hour: 3
Minute : 40
Total Minute : 220
Last Updated :
02 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...