头文件“HEAD.h”:
#ifndef HEAD_H #define HEAD_H template<typename T> class ?Cal { public: T add(T x,T y); T multiply(T x,T y); }; #endif
源文件:
#include<iostream> #include"HEAD.h" #include<stdlib.h> #include<iomanip> using namespace std; template<typename T> T Cal<T>::add(T x,T y) { return x + y; } template<typename T> T Cal<T>::multiply(T x,T y) { return x*y; } int main() { Cal<int> x; Cal<float> y; cout << "9+8=" << x.add(9,8) << endl; cout << "9.1+6.1=" << y.add(9.1,6.1) << endl; cout << "9*8=" << x.multiply(9,8) << endl; cout << "9.1*6.1=" << y.multiply(9.1,6.1) << endl; system("pause"); return 0; }
(编辑:ASP站长网)
|