C + + Language Note Series 20--Template

Source: Internet
Author: User

1. Randomly enter two numbers x and y, output Max Max.


int max (int x, int y)
{return x>y?

X:y;}
2. Function templates
(1) The function template is represented by one or more common types.
(2) function template because there is no detailed data type. Therefore, the function template is not executable.
(3) Role: template is a tool to implement code reuse mechanism, it can achieve type parameters, that is, the type is defined as a parameter, so as to achieve real code reusability. The template can be divided into two categories, one is the function template. The other one is the class template.
Note: The function template must be replaced with a detailed type when called to convert it to a detailed function.
3. Definition of function Template:
Template <class t>
A. Template definition Keywords: template
<class t> is used to illustrate a generic type T.
B.class T is used only to represent a type, but also to represent multiple common types:
<class T1, Class T2, Class T3, ..., class tn>
4. Invocation of the function template
(1) function template as a function call.
(2) The system will take its own initiative to replace the generic type in the function template with the type of the invocation expression, so that it can be converted into a detailed function and run.


Example 1

#include <iostream.h>

Template <class t>
T Max (t X, t y)
{return x>y?

X:y;}
int main ()
{
int x = 3, y = 4;
Double D1 = 2.3, d2 = 3.4;
Long L1 = 32L, L2 = 35L;
Cout<<max (x, y) <<endl;
Cout<<max (d1, D2) <<endl;
Cout<<max (L1, L2) <<endl;
}
Program output:
4
3.4
35
Analysis: Because the actual participation X, Y is an int type, the system itself actively replaces the type T in the template with int.


function conversions: int max (int x, int y) {return x>y?

: X:y;}

Example 2

#include <iostream.h>

Template <class t>
void Exchange (t &x, T &y, T &z)
{
T T;
if (x>y) {t = x; x = y; y = z;}
if (x>z) {t = x; x = z; z = t;}
if (y>z) {t = y; y = z; z = t;}
}
5. Overloading of Function templates
(1) The function template agrees with the implicit type conversion.
(2) The function template overload does not agree that overloading is a template-the function template overload can only be a detailed dominant function.


(3) The function template overload can only be called when the function template is not callable, the system will self-actively through the implicit type conversion after the call function template overload.


Example 3

#include <iostream.h>

Template <class t>
T Max (t X, t y)
{
cout<< "This is T max ()." <<endl;
Return x>y?x:y;
}
Double max (int x, double y)
{
cout<< "This is Double Max ()." <<endl;
Return x>y?

x:y;
}
int main ()
{
int x1 = 2, x2 = 3;
Double D1 = 2.3, d2 = 3.4;
Long L1 = 3L, L2 = 5L;
Cout<<max (x1, x2) <<endl; Modulation version
Cout<<max (L1, L2) <<endl; Modulation version
Cout<<max (x1, L2) <<endl; Adjustable Reload
Cout<<max (x1, D2) <<endl; Adjustable Reload
Cout<<max (d1, D2) <<endl; Modulation version
}
Program output:
This is T max ().
3
This is T max ().
5
This is double max ().
5
This is double max ().
3.4
This is double max ().
3.4
The reload of the template can only have a detailed function.
6. Class template
Template <class t>
Class Tany
{
T x, y;
Public
Tany (t xx, T yy): X (xx), Y (yy) {}
T Getx () {return x;}
T gety () {return y;}
};
The detailed implementation of the class template:
(1) The template type is substituted by the detail type when creating an object from the class templates.
(2) Definition of class template object
Class name < detail Type > object name (Initialization list)
If more than one common type must represent more than one detail type
For example: Tany <int> objint (3, 4);
Tany <double> objdouble (3.4, 5.6);

C + + Language Note Series 20--Template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.