function templates in C + +
The same problem is true for class declarations. Sometimes, there are two or more classes that have the same functionality, just different data types, as the following statement declares a class:
Copy Code code as follows:
Class Compare_int
{
Public
Compare (int a,int b)
{
X=a;
Y=b;
}
int Max ()
{
Return (x>y) x:y;
}
int min ()
{
Return (x<y) x:y;
}
Private
int x,y;
};
The effect is to compare two integers by calling the function member Max and Min to get the large and small number of two integers.
If you want to compare two floating-point numbers (float), in addition declare a class:
Copy Code code as follows:
Class Compare_float
{
Public
Compare (float a,float b)
{
X=a;
Y=b;
}
Float Max ()
{
Return (x>y) x:y;
}
float min ()
{
Return (x<y) x:y;
}
Private
float X,y;
};
Obviously this is essentially repetitive and there should be a way to reduce duplication of work. Class templates in C + + are added for this type of problem.
You can declare a generic class template that can have one or more type parameters of a virtual function,
Copy Code code as follows:
Class Compare
{
Public
Compare (T a,t B)
{
X=a;
Y=b;
}
T Max ()
{
Return (x>y) x:y;
}
T min ()
{
Return (x<y) x:y;
}
Private
T X,y;
};
Comparing this type of template with the first Compare_int class, you can see that there are two differences:
(1)
To increment a row when declaring a class template, Template<class type parameter name >
Template means "template", which is a keyword that must be written when declaring a class template. Inside the angle brackets behind the template is the parameter table column of the template, which the keyword class indicates is followed by the type parameter. In this case, T is a type parameter name, which is arbitrary, as long as it is a valid identifier.
T is not an actual type name that already exists, it is just a virtual type parameter name and will be replaced by an actual type name in the future.
(2)
The original type name int is replaced by the virtual type parameter name T. When a class object is established, if the actual type is specified as an int, the compiler replaces all the T with int, and if specified as float, replaces all t with float. This will enable "a class of multiple uses".
Because the class template contains type parameters, it is also called a parameterized class. If a class is an abstraction of an object, an object is an instance of a class, the class template is an abstract class, and the class is an instance of the class template. class templates allow you to build classes that contain a variety of data types.
So, when we declare a class template, how do we use it?
General methods for defining objects with a class:
Compare_int CMP1 (4,7); Compare_int is a declared class
The function is to establish an object CMP1 of the Compare_int class, and assign arguments 4 and 7 respectively to formal parameters A and B as two integers for comparison.
The method of defining objects with a class template is similar, but cannot be written directly
Compare CMP (4,7); Compare is the class template name
Compare is a class template name, not a specific class, and the type T in the class template body is not an actual type, but a virtual type that cannot be used to define objects. The virtual type must be replaced with the actual type name, as follows:
Compare <int> cmp1 (4,7);
That is, the actual type name is specified within the angle brackets after the class template name, and at compile time, the compiler replaces the type parameter T in the class template with int, thus materializing the class template or instantiating it.
At this point compare <int> is equivalent to the Compare_int class described earlier.
==================== Sample Code 1.1====================
Declare a class template, using it to achieve two integers, floating-point number, and character comparison, to find large and decimal
Copy Code code as follows:
#include <iostream>
using namespace Std;
Template<class t>//Declare a class template with a virtual type named T
Class Compare
{
Public
Compare (T a,t B)
{
X=a;
Y=b;
}
T Max ()
{
Return (x>y) x:y;
}
T min ()
{
Return (x<y) x:y;
}
Private
T X,y;
};
int main ()
{
Compare <int> cmp1 (3,7); Define object CMP1, for comparison of two integers
cout<< "Max:" <<cmp1.max () <<endl;
cout<< "Min:" <<cmp1.min () <<endl<<endl;
Compare <float> CMP2 (45.89,88.76)///define object CMP2 For comparison of two floating-point numbers
cout<< "Max:" <<cmp2.max () <<endl;
cout<< "Min:" <<cmp2.min () <<endl;
Compare <char> Cmp3 (' A ', ' a '); Define object Cmp3, for comparison of two characters
cout<< "Max:" <<cmp3.max () <<endl;
cout<< "Min:" <<cmp3.min () <<endl;
return 0;
}
Run Result:
Another problem to note is that the member functions in the class template listed above are defined within the class template.
If defined outside the class template, you cannot use a generic definition of a class member function:
T Compare::max () {...} You cannot define a member function in a class template
Instead, it should be written in the form of a class template:
Copy Code code as follows:
Template<class t>
T Compare <T>:: Max ()
{
Return (x>y) x:y;
}
The first line declares the class template, the second left side of the T is the virtual type name, followed by the compare<t> is a whole, the class with parameters. Indicates that the MAX function defined is within the scope of the class compare<t>.
When defining an object, the user will of course specify the actual type (figure int), and when compiling, the virtual type name T in the class template is replaced by the actual type. So compare<t> is equivalent to an actual class.
Summarize the problems to be noticed when using:
(1) Add a line before the class declaration, in the form
Template<class Virtual Type parameters >
Such as:
Copy Code code as follows:
Template<class t>//Note there is no semicolon at the end of the line
Class Compare
{
......
}
(2) When defining an object with a class template, use the following form:
Class template name < actual type name > object name;
Class template name < actual type name > object name (argument list);
Such as:
Copy Code code as follows:
Compare <float> CMP2 (45.89,88.76)///define object CMP2 For comparison of two floating-point numbers
(3) If a member function is defined outside the class template, it should be written as a class template:
Template <class Virtual type parameters >
function type class template name < virtual type parameter:: member function name (function parameter list column) {...}
(4) class template can have one or more type parameters, each type must precede the class, such as:
Copy Code code as follows:
Template <class T1,class t2>
Class SomeClass
{......} ;
When you define an object, you bring the actual type name, such as
Copy Code code as follows:
Someclass<int,double> obj;
(5) As with classes, use a class template to be aware of its scope and use it to define objects only within its valid scope.
If a class template is defined at the beginning of a file, a file scope is a valid scope where you can use the class template anywhere, but you cannot use the class template to define objects in B files.
(6) Templates can have layers, and a class template can be used as a base class to derive derived classes.