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

 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 }

 

调试运行结果:

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

 

相关文章:

  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2021-10-29
  • 2022-03-07
  • 2022-03-04
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案