Bài giảng Lập trình hướng đối tượng - Chương 2: Quá tải toán tử

Giới thiệu

Các toán tử được phép và không được phép quá tải (C++)

Hạn chế của quá tải toán tử

Cú pháp quá tải toán tử

Cài đặt quá tải toán tử

Các ví dụ

 

 

ppt41 trang | Chuyên mục: Lập Trình Hướng Đối Tượng | Chia sẻ: dkS00TYs | Lượt xem: 2466 | Lượt tải: 0download
Tóm tắt nội dung Bài giảng Lập trình hướng đối tượng - Chương 2: Quá tải toán tử, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Chương 2 QUÁ TẢI TOÁN TỬ * Tài liệu đọc Eckel Bruce, Thinking in C++ 2rd edition 12. Operator Overloading Dietel, C++ How to Program 4th edition Chapter 8 - Operator Overloading * Những nội dung chính Giới thiệu Các toán tử được phép và không được phép quá tải (C++) Hạn chế của quá tải toán tử Cú pháp quá tải toán tử Cài đặt quá tải toán tử Các ví dụ * Giới thiệu Các toán tử cho phép ta sử dụng cú pháp toán học đối với các kiểu dữ liệu của C++ thay vì gọi hàm (nhưng bản chất vẫn là gọi hàm) Ví dụ thay a=set(b.add(c)) bằng a=b+c Tự nhiên hơn Đơn giản mã hóa chương trình Quá tải toán tử: một toán tử có thể dùng cho nhiều kiểu dữ liệu * Giải thích thêm The interface for your class would likely be: class MyString { private: char string[ MAX_STRING ]; public: MyString(); MyString( const char* ); const char* getString(); MyString& setString( const char* ); MyString& appendString( const char* ); bool isEqualTo( const char* ); }; * Giải thích thêm (tt) You could then write code like: void main() { MyString string1( "Hello" ); MyString string2( "Good bye" ); MyString string3; string3.setString( string1.getString() ); string3.appendString(" and ").appendString(string2.getString() ); if ( string1.isEqualTo( string2.getString() ) ) { cout += -= *= /= %= ^= &= |= > >>= = && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that can not be overloaded . .* :: ?: * * * Cú pháp quá tải toán tử Syntax is: return_type operator@(argument-list) --- operator is a function --- @ is one of C++ operator symbols (+, -, =, etc..) Examples: operator+ operator- operator* operator/ * Cài đặt các toán tử được quá tải Có 3 cách cài đặt toán tử được quá tải Hàm thành viên (phương thức) Hàm không thành viên toàn cục Hàm bạn Lựa chọn cách cài đặt phụ thuộc vào Số toán hạng tham gia vào toán tử Tính đóng gói * Cài đặt các toán tử được quá tải (tt) Expression obj1@obj2 translates into a function call obj1.operator@(obj2), if this function is defined within class obj1 operator@(obj1,obj2), if this function is defined outside the class obj1 * Cài đặt bằng hàm thành viên class Complex { ... public: ... Complex operator +(const Complex &op) { double real = _real + op._real, imag = _imag + op._imag; return(Complex(real, imag)); } ... }; c = a+b; * Cài đặt bằng hàm không thành viên class Complex { ... public: ... double real() { return _real; } //need access functions double imag() { return _imag; } ... }; Complex operator +(Complex &op1, Complex &op2) { double real = op1.real() + op2.real(), imag = op1.imag() + op2.imag(); return(Complex(real, imag)); } c = a+b; * Cài đặt bằng hàm bạn class Complex { ... public: ... friend Complex operator +( const Complex &, const Complex & ); ... }; Complex operator +(Complex &op1, Complex &op2) { double real = op1._real + op2._real, imag = op1._imag + op2._imag; return(Complex(real, imag)); } c = a+b; * Khi nào dùng hàm thành viên ? When overloading ( ), [ ], ->, or =, the operator overloading function must be declared as a class member. When an operator function is implemented as a member function, the left most (or only in the case of unary operators) operand must be a class object (or a reference to a class object) of operator's class If member function, then this is implicitly available for one of the arguments * Khi nào dùng hàm không thành viên toàn cục ? If the operator needs to be commutative (a + b = b + a), then making it a non-member function is necessary. If the left operand must be an object of a different class or a built-in type, this operator must be implemented as a non-class member. eg. > operators * Khi nào dùng hàm bạn ? An operator function implemented as a non-member must be a friend if it needs to access non-public data members of that class. The overloaded > operator which has a left operand of type istream. * * * * Ví dụ: Toán tử một ngôi (hàm thành viên) int main() { 	int i=2,j = 3,k; 	 i = j + k; 	Complex C1(1,2), C2(4,5), C3; 	C3 = - C1; 	C3.show(); 	return 0; } class Complex { 	int r, i; 	public: 	Complex operator-() { 	Complex temp; 	temp.r = -r ; 	temp.i = -i ; 	return temp; } void show(){cout= size) 	return contents[size-1]; 	return contents[i];	 } * Ví dụ: Toán tử [] (hàm thành viên) (tt) // ----- Main function ----- int main() { 	 String s1("String 1"); 	 s1[1] = 'p';// modifies an element of the contents 	 s1.print(); 	 cout (const ComplexT &, const ComplexT & ); 	 	double re,im; public: 	// Constructor 	 ComplexT(double re_in=0,double im_in=1); // Constructor 	 	 void print() const; }; // Default Constructor ComplexT::ComplexT(double re_in, double im_in):re(re_in), im(im_in) { 	//re=re_in; 	//im=im_in; 	cout(const ComplexT &c1, const ComplexT &c2) { 	return c1.re*c1.re + c1.im*c1.im > c2.re*c2.re+c2.im*c2.im; } void ComplexT::print() const { 	cout z2) cout> The input operator must be overloaded as a friend function because its left operand is an input stream such as cin. The general form of the operator>>() function is istream& operator>>(istream& in, ClassName& op) { 	 //local declaration if any //Read the data into the object //isObject>>. . . //Return the istream object return isObject; } The operator>>() function returns a reference to an istream object so that input operations may be chained together. * Ví dụ: Quá tải toán tử > (hàm bạn) class ComplexT { 	// Function of operator > 	friend ostream& operator>(istream &, ComplexT &); 	double re,im; public: 	// Constructor 	 ComplexT(double re_in=0,double im_in=1); // Constructor 	 }; ComplexT::ComplexT(double re_in, double im_in):re(re_in), im(im_in) { 	cout>(istream &i, ComplexT &c) { 	i>>c.re>>c.im; 	return i; } int main() { ComplexT z1, z2; cout>z1; cout>z2; cout<<z1; cout<<endl; cout<<z2; cout<<endl; return 0; } * Hỏi và Đáp * 

File đính kèm:

  • pptBài giảng Lập trình hướng đối tượng - Chương 2 Quá tải toán tử.ppt