class template,类模板

class template,类模板


一、类模板概念

所谓类模板,我理解的就是将类中成员数据的类型参数化(成员数据包括数据成员和函数成员,静态非静态都可以)。使得程序设计更具可变性。

二、示例代码

	//复数类模板
	template<typename T>
    class complex
    {
    public:
        complex (T r = 0, T i = 0): re(r), im(i){ }
        complex& operator += (const complex&);
        complex& operator -= (const complex&);
        complex& operator *= (const complex&);
        T real () const { return re; }
        T imag () const { return im; }
        static T a;  // 静态成员变量
    private:
        T re, im;
    };
	//静态成员变量初始化
    template<typename T>
    T complex<T>::a = T();

	//成员函数的定义
    template<typename T>
    inline complex<T>&
    complex<T>::operator+=(const complex<T> &r)
    {
        this->re += r.re;
        this->im += r.im;
        return *this;
    }

    template<typename T>
    inline complex<T>&
    complex<T>::operator-=(const complex<T> &r)
    {
        this->re -= r.re;
        this->im -= r.im;
        return *this;
    }

    template<typename T>
    inline complex<T>&
    complex<T>::operator*=(const complex<T> &r)
    {
        T f = this->re * r.re - this->im * r.im;
        this->im = this->re * r.im + this->im * r.re;
        this->re = f;
        return *ths;
    }
	
	//外部全局函数的定义
    template<typename T>
    inline T
    imag (const complex<T>& x)
    {
        return x.imag ();
    }

    template<typename T>
    inline T
    real (const complex<T>& x)
    {
        return x.real ();
    }

    template<typename T>
    inline complex<T>
    operator + (const complex<T>& x, const complex<T>& y)
    {
        return complex<T> (real (x) + real (y), imag (x) + imag (y));
    }

    template<typename T>
    inline complex<T>
    operator + (const complex<T>& x, double y)
    {
        return complex<T> (real (x) + y, imag (x));
    }

    template<typename T>
    inline complex<T>
    operator + (double x, const complex<T>& y)
    {
        return complex<T> (x + real (y), imag (y));
    }

    template<typename T>
    inline complex<T>
    operator - (const complex<T>& x, const complex<T>& y)
    {
        return complex<T> (real (x) - real (y), imag (x) - imag (y));
    }

    template<typename T>
    inline complex<T>
    operator - (const complex<T>& x, double y)
    {
        return complex<T> (real (x) - y, imag (x));
    }

    template<typename T>
    inline complex<T>
    operator - (T x, const complex<T>& y)
    {
        return complex<T> (x - real (y), - imag (y));
    }

    template<typename T>
    inline complex<T>
    operator * (const complex<T>& x, const complex<T>& y)
    {
        return complex<T> (real (x) * real (y) - imag (x) * imag (y),
                        real (x) * imag (y) + imag (x) * real (y));
    }

    template<typename T>
    inline complex<T>
    operator * (const complex<T>& x, T y)
    {
        return complex<T> (real (x) * y, imag (x) * y);
    }

    template<typename T>
    inline complex<T>
    operator * (T x, const complex<T>& y)
    {
        return complex<T> (x * real (y), x * imag (y));
    }

    template<typename T>
    complex<T>
    operator / (const complex<T>& x, double y)
    {
        return complex<T> (real (x) / y, imag (x) / y);
    }

    template<typename T>
    inline complex<T>
    operator + (const complex<T>& x)
    {
        return x;
    }

    template<typename T>
    inline complex<T>
    operator - (const complex<T>& x)
    {
        return complex<T> (-real (x), -imag (x));
    }

    template<typename T>
    inline bool
    operator == (const complex<T>& x, const complex<T>& y)
    {
        return real (x) == real (y) && imag (x) == imag (y);
    }

    template<typename T>
    inline bool
    operator == (const complex<T>& x, T y)
    {
        return real (x) == y && imag (x) == 0;
    }

    template<typename T>
    inline bool
    operator == (T x, const complex<T>& y)
    {
        return x == real (y) && imag (y) == 0;
    }

    template<typename T>
    inline bool
    operator != (const complex<T>& x, const complex<T>& y)
    {
        return real (x) != real (y) || imag (x) != imag (y);
    }

    template<typename T>
    inline bool
    operator != (const complex<T>& x, T y)
    {
        return real (x) != y || imag (x) != 0;
    }

    template<typename T>
    inline bool
    operator != (T x, const complex<T>& y)
    {
        return x != real (y) || imag (y) != 0;
    }

#include <cmath>

    template<typename T>
    inline complex<T>
    polar (T r, T t)
    {
        return complex<T> (r * cos (t), r * sin (t));
    }

    template<typename T>
    inline complex<T>
    conj (const complex<T>& x)
    {
        return complex<T> (real (x), -imag (x));
    }

    template<typename T>
    inline T
    norm (const complex<T>& x)
    {
        return real (x) * real (x) + imag (x) * imag (x);
    }

    template<typename T>
    ostream&
    operator << (ostream& os, const complex<T>& x)
    {
        return os << '(' << real (x) << ',' << imag (x) << ')';
    }
	
	//测试代码
    void test_class_template()
    {
        complex<double> c1(2, 1);
        complex<double> c2(4, 0);

        cout << c1 << endl;
        cout << c2 << endl;

        cout << c1+c2 << endl;
        cout << c1-c2 << endl;
        cout << c1*c2 << endl;
        cout << c1 / 2 << endl;

        cout << conj(c1) << endl;
        cout << norm(c1) << endl;
        cout << polar(10,4) << endl;

        cout << (c1 == c2) << endl;
        cout << (c1 != c2) << endl;
        cout << +c2 << endl;
        cout << -c2 << endl;

        cout << (c1 += c2) << endl;
        cout << (c2 - 2) << endl;
        cout << (5 + c2) << endl;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值