# Include <iostream>
Using namespace STD;
Template <typename type>
Type max (Type X, Type Y)
{
If (x> Y)
{
Return X;
}
Else
{
Return y;
}
}
Template <typename parameter1 = int, typename parameter2 = int>
Class
{
Parameter1 X;
Parameter2 y;
Public:
Void setdata (parameter1 X, parameter2 y)
{
This-> X = X;
This-> Y = y;
}
Void display () const
{
Cout <"x =" <x <Endl;
Cout <"Y =" <Y <Endl;
}
};
Int main ()
{
A <int, float> A;/* can be like this a <> A; this indicates that the default parameter type parameter1 = int, parameter2 = int can be called, but not a; in this way, an error is reported during compilation,
The order must be consistent with that defined by the template. Otherwise, an error occurs */
A. setdata (11, 12.363 );
A. Display ();
Int max = max <int> (); // You can also write int max = max (). In this way, typename is defined according to the parameter type in the brackets, however, the two parameter types must be consistent.
Int N;
Cin> N;
Return 0;
}
Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.