用户定义的用户定义数据类型可满足其需求,编译器不支持此类数据类型的自动类型转换,因此,如果需要,用户需要自行设计转换例程。
在不兼容的数据类型之间进行数据转换时,可能会出现3种情况:
- 从原始数据类型到用户定义类型的转换:要执行此转换,其想法是在对象创建期间使用构造函数执行类型转换。下面是将int转换为用户定义的数据类型的示例:
例子:
C++
// C++ program to illustrate the
// type-conversion
#include
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;
}
C++
// C++ program to illustrate the
// above conversion
#include
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;
}
C++
#include
using namespace std;
//cgs system
class CGS
{
int mts; //meters
int cms; //centimeters
public:
void showdata()
{
cout<<"Meters and centimeters in CGS system:";
std::cout << mts<<" meters "<
C++
// C++ program to illustrate the
// above conversion
#include
using namespace std;
//minutes class
class Minute {
public:
int mins;
// Constructors
Minute()
{
mins = 0;
}
// Function to print the value of
// hours and minutes
void show()
{
cout << "\nTotal Minute : " << mins << endl;
}
};
// Time Class
class Time {
int hr, mins;
public:
// Constructors
Time(int h, int m)
{
hr = h;
mins = m;
}
Time()
{
cout << "\nTime's Object Created";
}
operator Minute () //overloading minute class
{
Minute m;
m.mins = (hr * 60) + mins;
return m;
} //driver code
// Function to print the value of
// hours and minutes
void show()
{
cout << "Hour: " << hr << endl;
cout << "Minute : " << mins << endl;
}
};
// Minutes Class
int main()
{
Time T1(3,40);
Minute m;
m=T1; //mintue class is destination and Time class is source class
T1.show();
m.show();
return 0;
}
输出
Time = 1 hrs and 35 mins
- 类对象到原始数据类型的转换:在这种转换中, from类型是一个类对象,而to类型是原始数据类型。一个重载铸造运算符函数的正常形式,也称为转换函数。以下是相同的语法:
句法:
operator typename()
{
// Code
}
- 现在,此函数将用户定义的数据类型转换为原始数据类型。例如,运算符double()将类对象转换为double类型,运算符int()将类类型对象转换为int类型,依此类推。下面是说明相同内容的程序:
例子:
C++
// C++ program to illustrate the
// above conversion
#include
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;
}
输出
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.
输出:
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.
现在,该函数会将矢量转换为标量大小。运算符double()可以用作:
double len = double(S1);
Or,
double len = S1;
where S1 is an object of type vector.
一种类类型到另一种类类型的转换:在这种类型中,一种类类型被转换成另一种类类型。可以通过2种方式完成:
1,使用构造函数
2.使用重载铸造运算符
1,使用构造函数
在Destination类中,我们使用构造方法
//Objects of different types
ObjectX=ObjectY;
Here ObjectX is Destination object and ObjectY is source object
例子:
C++
#include
using namespace std;
//cgs system
class CGS
{
int mts; //meters
int cms; //centimeters
public:
void showdata()
{
cout<<"Meters and centimeters in CGS system:";
std::cout << mts<<" meters "<
输出
Meters and centimeters in CGS system:9 meters 10 centimeters
feet and inches in FPS system:30 feet 4 inches
2.使用重载铸造运算符
// Objects of different types
objectX = objectY;
- 这里我们在源类中使用重载转换运算符,即在源类中重载目标类
请参见下面的示例,在该示例中,我们分别有两个类Time和Minute,并将一个类Time转换为另一个Minute类。
在下面的示例中,分钟类是目标类,时间类是源类
所以我们需要在源类中重载目标类
这里我们不应该告诉返回类型,但是我们返回重载的类对象
即在不指定返回类型的情况下返回值
C++
// C++ program to illustrate the
// above conversion
#include
using namespace std;
//minutes class
class Minute {
public:
int mins;
// Constructors
Minute()
{
mins = 0;
}
// Function to print the value of
// hours and minutes
void show()
{
cout << "\nTotal Minute : " << mins << endl;
}
};
// Time Class
class Time {
int hr, mins;
public:
// Constructors
Time(int h, int m)
{
hr = h;
mins = m;
}
Time()
{
cout << "\nTime's Object Created";
}
operator Minute () //overloading minute class
{
Minute m;
m.mins = (hr * 60) + mins;
return m;
} //driver code
// Function to print the value of
// hours and minutes
void show()
{
cout << "Hour: " << hr << endl;
cout << "Minute : " << mins << endl;
}
};
// Minutes Class
int main()
{
Time T1(3,40);
Minute m;
m=T1; //mintue class is destination and Time class is source class
T1.show();
m.show();
return 0;
}
输出
Hour: 3
Minute : 40
Total Minute : 220
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。