C++中函数模板template的使用

时间:2014-12-18 13:30:38   收藏:0   阅读:165
 

下面以一个简单程序演示一下函数模板的使用:

 1 #include<iostream>
 2 using namespace std;
 3 template<class Type>
 4 Type Abc(Type a,Type b,Type c)
 5 {
 6     return a+b+c;
 7 }
 8 int main()
 9 {
10     int a=1,b=2,c=3;
11     cout<<"a= "<<a<<",b= "<<b<<",c= "<<c<<endl;
12     cout<<"使用函数模板的结果为:\n";
13     cout<<Abc(a,b,c)<<endl;
14     float f=4,d=0.6,e=2.3;
15     cout<<"f= "<<f<<",d= "<<d<<",e= "<<e<<endl;
16     cout<<"使用函数模板的结果为:\n";
17     cout<<Abc(f,d,e)<<endl;
18     return 0;
19 }

 

调试运行结果:

bubuko.com,布布扣

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!