复数类模板
template<typename T>
class complex {
public:
complex(T r = 0, T i =0) : rp(r), ip(i){}
~complex(){}
complex& operator +=(const complex& c);
bool operator < (const complex& c) const;
T getReal() const { return rp };
T getImag() const { return ip };
private:
T rp;
T ip;
}
bool complex::operator < (const complex& c) {
long a = sqrt(pow(this->rp, 2.0) + pow(this->ip, 2.0));
long b = sqrt(pow(c.rp, 2.0) + pow(c.ip, 2.0));
return a < b ? true : false;
}
complex<double> c1(2.0, 3.0);
complex<int> c2(1, 2);
函数模板:
template<typename T>
T& min(const T& a, const T& b) const {
return a > b ? b : a;
}
complex<double> c1(3.0, 4.0);
complex<double> c2(2.0, 5.0);
complex<double> c3 = min(c1, c2);//函数模板->complex类模板->类的<重载函数