重载运算符函数
1.重载[ ]
注意:返回值类型是T,而并非类的引用
template <typename T> T & Point<T>::operator[](int index) { if(index==0) return _x; else return _y; }
2.重载<<
template <typename TYPE> ostream & operator<<(ostream &out, const Point<TYPE> &p) { out << "(" << p._x << ", " << p._y << ")"; return out; }
3.重载>>
friend istream & operator>>(istream ⑦ &in , Complex &c) { char str[200]; in.getline(str, 200, '('); in >> c.re; in.getline(str, 200, ','); in >> c.im; in.getline(str, 200, ')'); return in; }
4.重载*(友元函数)
friend Complex operator*(const Complex &c1, const Complex &c2) { Complex temp; temp.re = c1.re*c2.re - c1.im*c2.im; temp.im = c1.re*c2.im + c1.im*c2.re; return temp; }
5.重载 *=
Complex & operator*=(const Complex &c) { return *this = *this * c; }
6.重载前++,后++
注意:前++返回引用,后++返回值
RMB & operator++() { return *this = *this + 1; }
RMB operator++(int) { RMB temp(*this); ++(*this); //利用前++ return temp; }
7.类型转换函数
operator unsigned int() const { // 类型转换函数,可将RMB类型的对象转换成unsigned int类型数据 return 100*yuan+10*jiao+fen; }