Bài giảng Lập trình C++ - Chương 8: Nạp chồng toán tử

Outline

8.1 Giới thiệu

8.2 Kiến thức cơ bản về nạp chồng toán tử

8.3 Các hạn chế của nạp chồng toán tử

8.4 Các hàm toán tử là các phương thức hoặc hàm bạn

8.5 Nạp chồng toán tử dòng dữ liệu vào ra

8.6 Nạp chồng toán tử một ngôi

8.7 Nạp chồng toán tử hai ngôi

8.8 Case Study: Lớp mảng

8.9 Chuyển đổi giữa các kiểu

8.10 Case Study: Lớp xâu

8.11 Nạp chồng toán tử ++ và --

8.12 Case Study: Lớp Date

8.13 Các lớp thư viện chuẩn: string và vector

 

ppt86 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 5320 | Lượt tải: 5download
Tóm tắt nội dung Bài giảng Lập trình C++ - Chương 8: Nạp chồng toán tử, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
; num.areaCode; // input area code 47 input.ignore( 2 ); // skip ) and space 48 input >> setw( 4 ) >> num.exchange; // input exchange 49 input.ignore(); // skip dash (-) 50 input >> setw( 5 ) >> num.line; // input line 51 52 return input; // enables cin >> a >> b >> c; * fig08_03.cpp(3 of 3)fig08_03.cppoutput (1 of 1) 53 54 } // end function operator>> 55 56 int main() 57 { 58 PhoneNumber phone; // create object phone 59 60 cout > phone invokes operator>> by implicitly issuing 63 // the non-member function call operator>>( cin, phone ) 64 cin >> phone; 65 66 cout > So sánh mảng sử dụng phép toán == và != * 8.8 Case Study: Lớp mảng Hàm khởi tạo sao chép Được sử dụng khi cần sao chép đối tượng Trả về giá trị hoặc tham số Khởi tạo một đối tượng bằng cách sao chép một đối tượng khác Array newArray( oldArray ); newArray sao chép của oldArray Khai báo nguyên mẫu cho lớp Array Array( const Array & ); Phải đặt tham chiếu Nếu không, trả về giá trị Tạo nên một bản sao chép bởi lời gọi hàm khởi tạo sao chép Sao chép vô hạn lần * array1.h (1 of 2) 1 // Fig. 8.4: array1.h 2 // Array class for storing arrays of integers. 3 #ifndef ARRAY1_H 4 #define ARRAY1_H 5 6 #include 7 8 using std::ostream; 9 using std::istream; 10 11 class Array { 12 friend ostream &operator>( istream &, Array & ); 14 15 public: 16 Array( int = 10 ); // default constructor 17 Array( const Array & ); // copy constructor 18 ~Array(); // destructor 19 int getSize() const; // return size 20 21 // assignment operator 22 const Array &operator=( const Array & ); 23 24 // equality operator 25 bool operator==( const Array & ) const; 26 * array1.h (2 of 2) 27 // inequality operator; returns opposite of == operator 28 bool operator!=( const Array &right ) const 29 { 30 return ! ( *this == right ); // invokes Array::operator== 31 32 } // end function operator!= 33 34 // subscript operator for non-const objects returns lvalue 35 int &operator[]( int ); 36 37 // subscript operator for const objects returns rvalue 38 const int &operator[]( int ) const; 39 40 private: 41 int size; // array size 42 int *ptr; // pointer to first element of array 43 44 }; // end class Array 45 46 #endif * array1.cpp (1 of 7) 1 // Fig 8.5: array1.cpp 2 // Member function definitions for class Array 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include 10 11 using std::setw; 12 13 #include // C++ standard "new" operator 14 15 #include // exit function prototype 16 17 #include "array1.h" // Array class definition 18 19 // default constructor for class Array (default size 10) 20 Array::Array( int arraySize ) 21 { 22 // validate arraySize 23 size = ( arraySize > 0 ? arraySize : 10 ); 24 25 ptr = new int[ size ]; // create space for array 26 * array1.cpp (2 of 7) 27 for ( int i = 0; i = size ) { 104 cout = size ) { 121 cout >( istream &input, Array &a ) 135 { 136 for ( int i = 0; i > a.ptr[ i ]; 138 139 return input; // enables cin >> x >> y; 140 141 } // end function * array1.cpp (7 of 7) 142 143 // overloaded output operator for class Array 144 ostream &operator 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include "array1.h" 10 11 int main() 12 { 13 Array integers1( 7 ); // seven-element Array 14 Array integers2; // 10-element Array by default 15 16 // print integers1 size and contents 17 cout > integers1 >> integers2; 29 30 cout 7 8 using std::ostream; 9 using std::istream; 10 11 class String { 12 friend ostream &operator>( istream &, String & ); 14 15 public: 16 String( const char * = "" ); // conversion/default ctor 17 String( const String & ); // copy constructor 18 ~String(); // destructor 19 20 const String &operator=( const String & ); // assignment 21 const String &operator+=( const String & ); // concatenation 22 23 bool operator!() const; // is String empty? 24 bool operator==( const String & ) const; // test s1 == s2 25 bool operator s2 35 bool operator>( const String &right ) const 36 { 37 return right 40 41 // test s1 = s2 49 bool operator>=( const String &right ) const 50 { 51 return !( *this = * string1.h (3 of 3) 54 55 char &operator[]( int ); // subscript operator 56 const char &operator[]( int ) const; // subscript operator 57 58 String operator()( int, int ); // return a substring 59 60 int getLength() const; // return string length 61 62 private: 63 int length; // string length 64 char *sPtr; // pointer to start of string 65 66 void setString( const char * ); // utility function 67 68 }; // end class String 69 70 #endif * string1.cpp (1 of 8) 1 // Fig. 8.8: string1.cpp 2 // Member function definitions for class String. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 #include // C++ standard "new" operator 13 14 #include // strcpy and strcat prototypes 15 #include // exit prototype 16 17 #include "string1.h" // String class definition 18 19 // conversion constructor converts char * to String 20 String::String( const char *s ) 21 : length( strlen( s ) ) 22 { 23 cout = length ) { 107 cout = length ) { 122 cout = length || subLength length ) ) 145 len = length - index; 146 else 147 len = subLength; 148 149 // allocate temporary array for substring and 150 // terminating null character 151 char *tempPtr = new char[ len + 1 ]; 152 153 // copy substring into char array and terminate string 154 strncpy( tempPtr, &sPtr[ index ], len ); 155 tempPtr[ len ] = '\0'; * string1.cpp (7 of 8) 156 157 // create temporary String object containing the substring 158 String tempString( tempPtr ); 159 delete [] tempPtr; // delete temporary array 160 161 return tempString; // return copy of the temporary String 162 163 } // end function operator() 164 165 // return string length 166 int String::getLength() const 167 { 168 return length; 169 170 } // end function getLenth 171 172 // utility function called by constructors and operator= 173 void String::setString( const char *string2 ) 174 { 175 sPtr = new char[ length + 1 ]; // allocate memory 176 strcpy( sPtr, string2 ); // copy literal to object 177 178 } // end function setString * string1.cpp (8 of 8) 179 180 // overloaded output operator 181 ostream &operator>( istream &input, String &s ) 191 { 192 char temp[ 100 ]; // buffer to store input 193 194 input >> setw( 100 ) >> temp; 195 s = temp; // use String class assignment operator 196 197 return input; // enables cascading 198 199 } // end function operator>> * fig08_09.cpp(1 of 4) 1 // Fig. 8.9: fig08_09.cpp 2 // String class test program. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include "string1.h" 9 10 int main() 11 { 12 String s1( "happy" ); 13 String s2( " birthday" ); 14 String s3; 15 16 // test overloaded equality and relational operators 17 cout s1 yields " 25 s1 ? "true" : "false" ) * fig08_09.cpp(2 of 4) 26 = s1 yields " 29 = s1 ? "true" : "false" ) 30 s1 yields false s2 = s1 yields false s2 6 7 using std::ostream; 8 9 class Date { 10 friend ostream &operator 4 #include "date1.h" 5 6 // initialize static member at file scope; 7 // one class-wide copy 8 const int Date::days[] = 9 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 10 11 // Date constructor 12 Date::Date( int m, int d, int y ) 13 { 14 setDate( m, d, y ); 15 16 } // end Date constructor 17 18 // set month, day and year 19 void Date::setDate( int mm, int dd, int yy ) 20 { 21 month = ( mm >= 1 && mm = 1900 && yy = 1 && dd = 1 && dd 4 5 using std::cout; 6 using std::endl; 7 8 #include "date1.h" // Date class definition 9 10 int main() 11 { 12 Date d1; // defaults to January 1, 1900 13 Date d2( 12, 27, 1992 ); 14 Date d3( 0, 99, 8045 ); // invalid date 15 16 cout , namespace std Có thể khởi tạo string s1(“hi”); Nạp chồng toán tử = > 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::string; 11 12 int main() 13 { 14 string s1( "happy" ); 15 string s2( " birthday" ); 16 string s3; 17 18 // test overloaded equality and relational operators 19 cout s1 yields " 27 s1 ? "true" : "false" ) 28 = s1 yields " 31 = s1 ? "true" : "false" ) 32 s1 yields false s2 = s1 yields false s2 , namespace std Lưu kiểu bất kỳ vector myArray(10) Hàm size ( myArray.size() ) Toán tử được nạp chồng [] Lấy phần tử xác định, myArray[3] Toán tử được nạp chồng !=, ==, and = Không bằng, bằng, gán bằng * fig08_14.cpp(1 of 5) 1 // Fig. 8.14: fig08_14.cpp 2 // Demonstrating standard library class vector. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include 10 11 using std::setw; 12 13 #include 14 15 using std::vector; 16 17 void outputVector( const vector & ); 18 void inputVector( vector & ); 19 20 int main() 21 { 22 vector integers1( 7 ); // 7-element vector 23 vector integers2( 10 ); // 10-element vector 24 * fig08_14.cpp(2 of 5) 25 // print integers1 size and contents 26 cout integers3( integers1 ); // copy constructor 57 58 cout &array ) 99 { 100 for ( int i = 0; i &array ) 115 { 116 for ( int i = 0; i > array[ i ]; 118 119 } // end function inputVector * fig08_14.cppoutput (1 of 2) Size of vector integers1 is 7 vector after initialization: 0 0 0 0 0 0 0   Size of vector integers2 is 10 vector after initialization: 0 0 0 0 0 0 0 0 0 0   Input 17 integers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 After input, the vectors contain: integers1: 1 2 3 4 5 6 7 integers2: 8 9 10 11 12 13 14 15 16 17   Evaluating: integers1 != integers2 integers1 and integers2 are not equal * fig08_14.cppoutput (2 of 2) Size of vector integers3 is 7 vector after initialization: 1 2 3 4 5 6 7   Assigning integers2 to integers1: integers1: 8 9 10 11 12 13 14 15 16 17 integers2: 8 9 10 11 12 13 14 15 16 17   Evaluating: integers1 == integers2 integers1 and integers2 are equal   integers1[5] is 13   Assigning 1000 to integers1[5] integers1: 8 9 10 11 12 1000 14 15 16 17   Attempt to assign 1000 to integers1.at( 15 )   abnormal program termination 

File đính kèm:

  • pptBài giảng Lập trình C++ - Chương 8 Nạp chồng toán tử.ppt