[Part10 C++] Operator Overloading - toán tử với các đối tượng C++

80 513 0
[Part10 C++] Operator Overloading -  toán tử với các đối tượng C++

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

[Part10 C++] Operator Overloading - toán tử với các đối tượng C++

1 Chapter - Operator Overloading Outline 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 8.11 8.12 8.13 Introduction Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs as friend Functions Overloading Stream-Insertion and Stream-Extraction Operators Overloading Unary Operators Overloading Binary Operators Case Study: Array Class Converting between Types Case Study: A String Class Overloading ++ and -Case Study: A Date Class Standard Library Classes string and vector © 2003 Prentice Hall, Inc All rights reserved 8.1 Introduction • Sử dụng toán tử với đối tượng (operator overloading) – số lớp, sử dụng toán tử sáng sử dụng lời gọi hàm object2 = object1.add(object2); object2 = object2 + object1; – toán tử cảm ngữ cảnh (sensitive to context) Ví dụ • += -= *= /= %= ^= &= |= > >>= [] () new delete new[] delete[] Operators that cannot be overloaded * © 2003 Prentice Hall, Inc All rights reserved :: ?: sizeof 8.4 Operator Functions As Class Members Vs As Friend Functions • aa@bb • @aa • aa@ aa.operator@(bb) operator@(aa,bb) aa.operator@( ) operator@(aa) aa.operator@(int) operator@(aa,int) • Operator functions – Member functions • Sử dụng từ khóa this để ngầm lấy tham số – toán hạng bên trái tốn tử hai ngơi (ví dụ +) – tốn hạng tốn tử ngơi • Toán tử toán hạng bên trái phải thuộc lớp – Non member functions • Cần tham số cho hai tốn hạng • Có thể lấy đối tượng khơng thuộc lớp tốn tử • Phải friend để truy nhập liệu private protected © 2003 Prentice Hall, Inc All rights reserved 8.4 Operator Functions As Class Members Vs As Friend Functions • Các phép tốn có tính giao hốn – phép + cần có tính giao hốn • “a + b” “b + a” phải chạy – giả sử ta có hai lớp khác – Overloaded operator member function lớp bên trái • HugeIntClass + long int – trường hợp kia, cần non-member overload function • long int + HugeIntClass © 2003 Prentice Hall, Inc All rights reserved 8.5 Overloading Stream-Insertion and Stream-Extraction Operators • > – overloaded để xử lý kiểu built-in – overload để xử lý kiểu liệu người dùng • Overloaded cần toán tử trái kiểu istream & – Vậy, hai phải non-member function • Chương trình ví dụ – Class PhoneNumber • Lưu trữ số điện thoại – In số điện thoại ó c nh dng t ng ã (123) 456-7890 â 2003 Prentice Hall, Inc All rights reserved function prototype cho hàm overload toán 10 // Fig 8.3: fig08_03.cpp Outline tử >> object 17 class PhoneNumber { 18 friend ostream &operator( istream&, PhoneNumber & ); 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 37 38 private: char areaCode[ ]; char exchange[ ]; char line[ ]; Biểu thức: cout

Ngày đăng: 13/10/2013, 21:59

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan