C + + class conversion constructors and conversion functions review
#include <iostream>
#include <string>
using namespace Std;
Class Student
{
Private
String name;
int age;
Double grade;
Public
Student (string name_, int age_, double grade_): Name (name_), age (Age_), Grade (Grade_) {}
Student () {}
Student (double grade_)//conversion constructor
{
Grade = Grade_;
}
Student (int age_)
{
age = Age_;
}
Explicit Student (String name_)
{
name = Name_;
}
operator int ()//conversion function
{
return age;
}
Explicit operator string ()
{
return name;
}
String Get_name ()
{
return name;
}
int Get_age ()
{
return age;
}
Double Get_grade ()
{
return grade;
}
};
int main ()
{
Conversion constructors for type conversions
cout << "----------------------------" << Endl;
Student s1{"Linukey", 20, 100};
cout << S1.get_grade () << Endl;
S1 = 300.0;
cout << S1.get_grade () << Endl; Because a constructor with a single argument of double is declared in a class, you can assign a variable of type double to S1
Ii. implicit conversion of a explicit-constrained conversion constructor
cout << "----------------------------" << Endl;
Student s2{"Linukey", 20, 100};
cout << s2.get_name () << Endl;
String Temp1 = "Lin";
s2 = Temp1; This error occurs because explicit restricts the implicit conversion of the string type to the student type.
Third, when multiple similar types exist at the same time, the compiler will prefer the most appropriate, but sometimes also appear ambiguity
cout << "----------------------------" << Endl;
Student s3{"Linukey", 20, 100};
cout << S3.get_grade () << Endl;
cout << s3.get_age () << Endl;
s3 = 300;
cout << S3.get_grade () << Endl; Because the 300 above is of type int, the compiler takes precedence over the implicit conversion of the int type, so the following age output is 300, but the grade here output garbled
cout << s3.get_age () << Endl;
Iv. wrong ambiguity if you have an int type and a class constructor of type double, and we assign a long variable to student, an error occurs.
cout << "-----------------------------" <<endl;
Student s4{"Linukey", 20, 100};
Long temp2 = 100;
S4 = Temp2; The compiler will error because there are two semantics, and there is no fully suitable long type
V. Conversion functions we said earlier. A single-argument class constructor can assign other types to class types, but vice versa? This is where the conversion function is used.
cout << "-----------------------------" << Endl;
Student s5{"Linukey", 20, 100};
int Temp3 = S5; Because you declare a conversion function of type int, you can convert a variable of type student to an int type
cout << Temp3 << Endl;
Note that the conversion function here still has to follow the above two semantic requirements, otherwise there will be two of the error of righteousness
VI. explicit keyword limit conversion function Sometimes we don't want the conversion function to execute implicitly, but to show the execution, then we need to use the explicit keyword to limit
cout << "-----------------------------" << Endl;
Student s6{"Linukey", 20, 100};
string temp4 = S6; Note that this will be an error because we have used the explicit keyword to restrict the conversion function, so we cannot make an implicit conversion
String temp4 = (string) s6; I can use the display to achieve the purpose of the conversion, and higher security
cout << temp4 << Endl;
/*
Requirements for conversion functions:
1. The conversion function must be a class method
2. The conversion function can return a value, but cannot specify a return type
3. The conversion function cannot have parameters
*/
return 0;
}
C + + class conversion constructors and conversion functions review