您的位置:首页 > 编程语言 > C语言/C++

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

2014-07-17 11:08 274 查看
定义函数模板的一般形 式为

template < typename T> 或 template <class T>

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

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

#include <iostream>
using namespace std;

template<typename T>
T max(T a, T b, T c)
{
if(b > a) a = b;
if(c > a) a = c;
return a;
}

int main()
{
int x, y, z, m;
cout << "please enter three integer numbers:" << endl;
cin >> x >> y >> z;
m = max(x, y ,z);
cout << "integer of max is " << m << endl;

double x1, y1, z1, m1;
cout << "please enter three double numbers:" << endl;
cin >> x1 >> y1 >> z1;
m1 = max(x1, y1 ,z1);
cout << "double of max is " << m1 << endl;

long x2, y2, z2, m2;
cout << "please enter three long numbers:" << endl;
cin >> x2 >> y2 >> z2;
m2 = max(x2, y2 ,z2);
cout << "long of max is " << m2 << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: