(2)函数模板
#include <iostream> using namespace std; template<class T> //函数模板的定义 T add(T x,T y) { T sum ; sum=x+y; return sum; } template<class T> //函数模板的定义 T acc(T m,T n) { T acc ; acc=m*n; return acc; } int main() { int x,y; cout<<"请输入两个整数"<<endl; cin>>x>>y; cout<<"这两个数的和为:"<<add(x,y)<<endl; cout<<"这两个数的积为:"<<acc(x,y)<<endl; float m,n; cout<<"请输入两个小数"<<endl; cin>>m>>n; cout<<"这两个数的和为:"<<add(m,n)<<endl; cout<<"这两个数的积为:"<<acc(m,n)<<endl; return 0; }