1 / 19

Operator overloading

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)

olaf
Download Presentation

Operator overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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&);

  2. 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;

  3. 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

  4. 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; }

  5. 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; }

  6. 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.

  7. 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; };

  8. 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); } …

  9. 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

  10. 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

  11. 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; }

  12. operator overloading ostream& operator<<(ostream& in, Complex& c){ out << c.real << ‘+’ << c.imag << ‘i’ ; return out; }

  13. 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 [ ]

  14. 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 ]; }

  15. 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

  16. 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)

  17. 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)

  18. 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

  19. 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

More Related