class template(模板)首次试用

Posted zmachine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于class template(模板)首次试用的主要内容,如果未能解决你的问题,请参考以下文章

template相关

C++中 模板Template的使用

模板 <class T>:错误:“T”没有命名类型

类模板成员函数默认值问题:an out-of-line definition of a member of a class template cannot have default arguments

Template_5模板拾遗1

template用法问题