230 likes | 699 Views
Operator overloading. redefine the operations of operators + * / << >> ( ) [ ] …, 參考 p. 286 eg. Complex number Complex A, B, C; C = A.add(B); // use member function add C = A + B; // overload the operator + 格式: ( function prototype)
E N D
Operator overloading • redefine the operations of operators • + * / << >> ( ) [ ] …, 參考p. 286 • eg. Complex number Complex A, B, C; C = A.add(B); // use member function add C = A + B; // overload the operator + • 格式: (function prototype) • Complex operator+(Complex&);
operator overloading(example p.291) class Complex { public: Complex(); //default constructor Complex(double ); Complex(double,double ); void write()const; //operator methods Complex operator+(const Complex&)const; Complex operator-(const Complex&)const; Complex operator*(const Complex&)const; Complex operator/(const Complex&)const;
operator overloading(cont.) private: double real; double imag; }; int main ( ) { Complex c1(7.0, 5.0); Complex c2(2.0, 3.0); Complex c3; cout << “c1=“; c1.write( ); cout << “c2=“; c2.write( ); c3 = c1 + c2; cout << “c1+c2=“; c3.write( ); } c1=7+5i c2=2+3i c1+c2=9+8i
operator overloading(cont.) Complex::Complex(){ real =imag =0.0; } Complex::Complex(double re ){ real =re; imag =0.0; } Complex::Complex(double re,double im ){ real =re; imag =im; }
operator overloading(cont.) Complex Complex::operator+(const Complex&u )const { Complex v(real +u.real,imag +u.imag ); return v; } Complex Complex::operator-(const Complex&u )const { Complex v(real -u.real,imag -u.imag ); return v; } Complex Complex::operator*(const Complex&u )const { Complex v(real *u.real -imag *u.imag,imag *u.real +real *u.imag ); return v; }
friend functions • friend function • a function defined outside the class but allowed to access the private and protected members of the given class • prototype is declared within the class and leading with the keyword friend • eg.
friend functions class Complex { public: Complex( ); Complex(double); Complex(double, double); void write( ) const; friend Complex operator+(const Complex&, const Complex&); friend Complex operator-(const Complex&, const Complex&); friend Complex operator*(const Complex&, const Complex&); friend Complex operator/(const Complex&, const Complex&); private: double real; double imag; };
friend functions // Complex + as top-level function Complex operator+(const Complex& t, const Complex& u) { return Complex(t.real+u.real, t.imag+u.imag); } Complex operator-(const Complex& t, const Complex& u) { return Complex(t.real-u.real, t.imag-u.imag); } …
operator overloading( >> and <<) • overload the input and output operators • cin >> i; // interpreted as cin.operator>>(i); • cin is a system declared object of istream class • cout is a system declared object of ostream class • >> must be overload as a top-level friend function
operator overloading • overload the >> and << operators for Complex eg. Complex a, b, c; cin >> b; cin >> c; a = b+c; cout << a; • 4 • 5 • 5 + 9i
operator overloading class Complex{ … friend istream& operator>> (istream&, Complex&); friend ostream& operator<<(ostream&, Complex&); … }; istream& operator>>(istream& in, Complex& c){ in >> c.real >> c.imag; return in; }
operator overloading ostream& operator<<(ostream& in, Complex& c){ out << c.real << ‘+’ << c.imag << ‘i’ ; return out; }
class intArray{ public: intArrary(int s); int& operator[ ](int); const int& operator[ ](int) const; int get_size( ) const { return size; } private: int size; int* a; }; intArray::intArray( int s) { try { a = new int[s]; } catch (bad_alloc) { cerr << “Unable to allocate\ storage for intArrar\n”; throw; } size = a; } overload the subscript operator [ ]
overload the subscript operator [ ] int& intArray::operator[ ](int i) { if (i < 0 || i >= size) throw string(“OutOfBounds”); return a[ i ]; } const int& intArray::operator[ ](int i) const { if (i < 0 || i >= size) throw string(“OutOfBounds”); return a[ i ]; }
int main( ) { intArray b(5); int i; try { for (i=0; i < b.get_size; i++) b[ i ] = 2 * i; for (i=0; i < 6; i++) cout << b[ i ] << ‘\n’; } catch ( string s) { cerr << s << ‘\n’; cerr << “i=“ << i <<‘\n’; } return 0; } overload the subscript operator [ ] 0 2 4 6 8 OutOfBounds i=5
class Clock { public: Clock (int =12, int=0, int=0); Clock tick( ); friend ostream& operator<<( ostream&, const Clock&); Clock operator++( ); // ++a Clock operator++(int); // a++ private: int hour; int min; int ap; }; Clock::Clock(int h, int m, int ap_flag){ hour = h; min = m; ap = ap_flag; } operator overloading (example)
Clock Clock::tick( ) { ++min; if (min == 60) { hour++; min = 0; } if (hour == 13) hour = 1; if (hour == 12 && min == 0) ap = !ap; return *this; } Clock Clock::operator++( ) { return tick( ); } Clock Clock::operator++(int n) { Clock c=*this; tick( ); return c; } operator overloading (example)
operator overloading (example) ostream& operator<<(ostream& out, const Clock& c) { out << setfill(‘0’) << setw(2) << c.hour << ‘:’ << setw(2) << c.min; if (c.ap) out << “ PM”; else out << “ AM”; return out; } int main( ) { Clock c,d; c = d++; cout << “Clock: “ << c << ‘\n’; cout << “Clock: “ << d << ‘\n’; … } Clock c: 12:00 AM Clock d: 12:01 AM
operator overloading int main( ) { Clock c,d; c = ++d; cout << “Clock: “ << c << ‘\n’; cout << “Clock: “ << d << ‘\n’; … } Clock c: 12:01 AM Clock d: 12:01 AM