1 / 21

高等程式語言實習課

高等程式語言實習課. http://helldeathscythe.myweb.hinet.net/. Operator overloading. 在 C++ 語言中, << 運算符號可以配合 cout 作為字串資料流輸出符號,同樣也可以作為左移位元運算符號 + - 可以作為數字的正負,也可以作為加法與減法的運算符號 因為它們都被超載 (overloading) 在 C++ 的類別資料庫中. Operator overloading. 使用者自訂的類別. 原始版 理想版. void main() { frac f1(1,2),f2(1,3),f3;

Download Presentation

高等程式語言實習課

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. 高等程式語言實習課

  2. http://helldeathscythe.myweb.hinet.net/

  3. Operator overloading • 在C++語言中,<<運算符號可以配合cout作為字串資料流輸出符號,同樣也可以作為左移位元運算符號 • + - 可以作為數字的正負,也可以作為加法與減法的運算符號 • 因為它們都被超載(overloading)在C++的類別資料庫中

  4. Operator overloading • 使用者自訂的類別 原始版 理想版 void main() { frac f1(1,2),f2(1,3),f3; if(f1.bigger(f2)) f3.copy(f1.sub(f2)); else f3.copy(f1.add(f2)); f3.print(); } void main() { frac f1(1,2),f2(1,3),f3; if(f1>f2) f3=f1-f2; else f3=f1+f2; f3.print(); } Of course Why not?

  5. Operator overloading • 二元運算子 • 算數符號: = + - * / += -= • 關係符號: > < >= <= == != • 邏輯符號: && || &(and) |(or) ^(xor) • IO: << >> • 一元運算子 • ++ -- + - • 其他 • new delete -> [] () ,

  6. Operator overloading class ClassName { ... //超載運算子X,可能是+-/*... return_type operatorX(...) { ... } };

  7. Operator overloading 原始版 運算子超載 class point{ int x,y;public: ...//建構子等其他函數point add(const point &p2){ return point(x+p2.x,y+p2.y); //匿名物件} void set(const point &p2){ this->x = p2.x; this->y = p2.u; } }; class point{ int x,y; public: ...//建構子等其他函數 point operator+(const point &p2){ point p(x+p2.x,y+p2.y); retrun p; } void operator=(const point &p2){ x = p2.x; y = p2.u; } };

  8. Operator overloading p1+p2; p1=p2; p3=p1+p2; if (p1>p2) p1=p2; else p2=p1; p1.operato+(p2); p1.operator=(p2); p3.operator=(p1.operator+(p2)); if(p1.operator>(p2)) p1.operator=(p2); else p2.operator=(p1);

  9. Operator overloading • 這是什麼? p3.operator+(p1.operator-(p2)); p1.operator+(p2.operator*(p3)); p3.operator+(p2.operator*(p1)); p1.operator=(p2.operator=(p3)); f1.operator=(p2.operator[](1)); p1.operator++(); p1.operator+(2); p3+p1-p2; p3+p2*p1; p2*p1+p3; p1=p2=p3; f1=p2[1]; p1++; p1+2; ?

  10. *Operator overloading • 細談operator=() • 當類別含有指標成員時一定要 • 建構子 Copy Constructor • 超載 Operator =(); • 解構子Destructor

  11. 0 1 2 3 4 5 *Operator overloading List L1 = List L2 Int* A Int* A 0X001125 0X001125

  12. 0 0 1 1 2 2 3 3 4 4 5 5 *Operator overloading List L1 = List L2 Int* A Int* A 0X001125 0X001256

  13. *Operator overloading • operator=()的程式步驟 • x1 == x1 ? • x1陣列長度 = x2陣列長度 ? • delete x1; • x1 = new type[x2.size()]; • 複製x2 to x1 • return *this;

  14. Operator overloading • ++前置與後置 class point{ int x,y;public: ... point& operator++(){ //前置prefix ++i ++x; ++y; retrun *this; } point operator++(int){ //後置postfix i++; point p(*this); x++; y++; return p; }};

  15. Operator overloading • -(負號)的超載 class point{ int x,y;public: ... point& operator-(){ return point(-x,-y); }}; 如果-(負號)與-(減法)同時被超載,執行時會不會搞混?

  16. 更多的Operator overloading? • Friend Operator functions • Type Operator functions

  17. Friend Operator functions • 這是什麼? ? p1+2; 2+p1; p1.operator+(2); 2.operator+(p1); // ???

  18. Friend Operator functions • 提供一個兩個參數的operator+ class point{ int x,y; ...};point operator+(const point& p1,const point& p2){ ...//寫什麼?}int main(){ point p1(3,5),p2(4,7),p3; p3=p1+p2; p3=5+p1;} 注意: 由於point沒有實作operator+ p1+p2才會被編譯成operator+(p1,p2) 5+p1會被編譯成operator+(point(5)+p2);

  19. Friend Operator functions • 提供一個兩個參數的operator+ class point{ int x,y; friend point operator+(const point& p1,const point& p2);};point operator+(const point& p1,const point& p2){ retrun point(p1.x+p2.x,p1.y+p2.y);}int main(){ point p1(3,5),p2(4,7),p3; p3=p1+p2; p3=5+p1;} 你是我的朋友,所以你的東西就是我的東西!?

  20. Type Operator functions • 資料型態也可以超載 class clock{ int h,m,s; ...};void mian(){ clock c(17,32,43); int sec = int( c ); //17*3600+32*60+43 Cout << sec;}

  21. Type Operator functions • 資料型態也可以超載 class clock{ int h,m,s; operator int() { return h*3600+m*60+s; }};void mian(){ clock c(17,32,43); int sec = c; //17*3600+32*60+43 Cout << sec;}

More Related