[C++程序设计]函数模板

定义函数模板的一般形 式为

template < typename T> 或 template <class T>

函数模板: 函数参数个数,函数体相同.参数类型不同

函数重载: 函数参数个数,类型不同.与函数类型(返回值)无关

 1 #include <iostream>
 2 using namespace std;
 3 
 4 template<typename T>
 5 T max(T a, T b, T c)
 6 {
 7     if(b > a) a = b;
 8     if(c > a) a = c;
 9     return a;
10 }
11 
12 int main()
13 {
14     int x, y, z, m;
15     cout << "please enter three integer numbers:" << endl;
16     cin >> x >> y >> z;
17     m = max(x, y ,z);
18     cout << "integer of max is " << m << endl;
19 
20     double x1, y1, z1, m1;
21     cout << "please enter three double numbers:" << endl;
22     cin >> x1 >> y1 >> z1;
23     m1 = max(x1, y1 ,z1);
24     cout << "double of max is " << m1 << endl;
25 
26     long x2, y2, z2, m2;
27     cout << "please enter three long numbers:" << endl;
28     cin >> x2 >> y2 >> z2;
29     m2 = max(x2, y2 ,z2);
30     cout << "long of max is " << m2 << endl;
31     return 0;
32 }

[C++程序设计]函数模板,布布扣,bubuko.com

[C++程序设计]函数模板

上一篇:javascript 原生态实现ajaxform 包括客户端验证


下一篇:JavaScript Patterns 6.3 Klass