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:
C++
// C++ program to illustrate the
// type-conversion
#include <bits/stdc++.h>
using
namespace
std;
// Time Class
class
Time {
int
hour;
int
mins;
public
:
// Default Constructor
Time()
{
hour = 0;
mins = 0;
}
// Parameterized Constructor
Time(
int
t)
{
hour = t / 60;
mins = t % 60;
}
// Function to print the value
// of class variables
void
Display()
{
cout <<
"Time = "
<< hour
<<
" hrs and "
<< mins <<
" mins\n"
;
}
};
// Driver Code
int
main()
{
// Object of Time class
Time T1;
int
dur = 95;
// Conversion of int type to
// class type
T1 = dur;
T1.Display();
return
0;
}
chevron_rightfilter_noneOutput: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:
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:
C++
// C++ program to illustrate the
// above conversion
#include <bits/stdc++.h>
using
namespace
std;
// Tie Class
class
Time {
int
hrs, mins;
public
:
// Constructor
Time(
int
,
int
);
// Casting operator
operator
int
();
// Destructor
~Time()
{
cout <<
"Destructor is called."
<< endl;
}
};
// Function that assigns value to the
// member variable of the class
Time::Time(
int
a,
int
b)
{
hrs = a;
mins = b;
}
// int() operator is used for Data
// conversion of class to primitive
Time::operator
int
()
{
cout <<
"Conversion of Class"
<<
" Type to Primitive Type"
<< endl;
return
(hrs * 60 + mins);
}
// Function perfomrs type conversion
// from the Time class type object
// to int data type
void
TypeConversion(
int
hour,
int
mins)
{
int
duration;
// Create Time Class object
Time t(hour, mins);
// Conversion OR duration = (int)t
duration = t;
cout <<
"Total Minutes are "
<< duration << endl;
// Conversion from Class type to
// Primitive type
cout <<
"2nd method operator"
<<
" overloading "
<< endl;
duration = t.operator
int
();
cout <<
"Total Minutes are "
<< duration << endl;
return
;
}
// Driver Code
int
main()
{
// Input value
int
hour, mins;
hour = 2;
mins = 20;
// Function call to illustrate
// type conversion
TypeConversion(hour, mins);
return
0;
}
chevron_rightfilter_noneOutput: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 as follow:
// Objects of different types objectX = objectY;
See the below example in which we have two classes Time and Minute respectively and will convert one class Time to another Minute class.
C++
// C++ program to illustrate the
// above conversion
#include <bits/stdc++.h>
using
namespace
std;
// Time Class
class
Time {
int
hr, mins;
public
:
// Constructors
Time(
int
h,
int
m)
{
hr = h;
mins = m;
}
Time()
{
cout <<
"\nTime's Object Created"
;
}
int
Minute()
{
int
totalmin = (hr * 60) + mins;
return
totalmin;
}
// Function to print the value of
// hours and minutes
void
show()
{
cout <<
"Hour: "
<< hr << endl;
cout <<
"Minute : "
<< mins << endl;
}
};
// Minutes Class
class
Minute {
int
mins;
public
:
// Constructors
Minute()
{
mins = 0;
}
void
operator=(Time T)
{
mins = T.Minute();
}
// Function to print the value of
// hours and minutes
void
show()
{
cout <<
"\nTotal Minute : "
<< mins << endl;
}
};
// Function perfomrs type conversion
// from the Time class type object
// to int data type
void
TypeConversion(
int
hour,
int
mins)
{
Time t1(hour, mins);
t1.show();
Minute m1;
m1.show();
// Conversion from Time class
// to Minute class
m1 = t1;
t1.show();
m1.show();
}
// Driver Code
int
main()
{
// Input value
int
hour, mins;
hour = 3, mins = 40;
// Function Call
TypeConversion(hour, mins);
return
0;
}
chevron_rightfilter_noneOutput:Hour: 3 Minute : 40 Total Minute : 0 Hour: 3 Minute : 40 Total Minute : 220
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.