class template(模板)首次试用
#include <iostream> using namespace std; template<typename T> class complex{ public:complex(T r=0,T i=0) :re(r),im(i) {} T real() const{return re;}//函数中const表示不改变函数内各值的内容,只是简单的将值取出 T imag() const{return im;} private: T re,im; }; int main() { complex<int> c1(2,1); complex<double> c2(2.5,1.2); cout<<c1.real()<<endl; cout<<c2.real()<<endl; return 0; }