一、类模板概念
所谓类模板,我理解的就是将类中成员数据的类型参数化(成员数据包括数据成员和函数成员,静态非静态都可以)。使得程序设计更具可变性。
二、示例代码
//复数类模板
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;
}