Ngôn ngữ lập trình C++ - Chương 8: Operator Overloading

Outline

8.1 Introduction

8.2 Fundamentals of Operator Overloading

8.3 Restrictions on Operator Overloading

8.4 Operator Functions as Class Members vs. as friend Functions

8.5 Overloading Stream-Insertion and Stream-Extraction Operators

8.6 Overloading Unary Operators

8.7 Overloading Binary Operators

8.8 Case Study: Array Class

8.9 Converting between Types

8.10 Case Study: A StringClass

8.11 Overloading ++and --8.12 Case Study: A DateClass

8.13 Standard Library Classes stringand vector

pdf80 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1608 | Lượt tải: 2download
Tóm tắt nội dung Ngôn ngữ lập trình C++ - Chương 8: Operator Overloading, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
 >= > <= <
– Phép gán – Assignment operator =
– Phép nối – Concatenation (overloaded +=)
© 2003 Prentice Hall, Inc. All rights reserved.
66
8.13 Standard Library Classes string and 
vector
• Class string
– hàm xâu con substr
• s1.substr(0, 14);
– bắt đầu từ vị trí 0, lấy 14 ký tự
• s1.substr(15)
– xâu con bắt đầu từ vị trí 15
– Overloaded []
• truy nhập 1 ký tự
• Không kiểm tra tính hợp lệ của chỉ số (No range checking)
– at function
• s1.at(10)
• Ký tự tại chỉ số 10
• có kiểm tra biên (bounds checking)
– Sẽ dừng chương trình nếu chỉ số không hợp lệ (chi tiết tại 
chương 13)
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
67
fig08_13.cpp
(1 of 4)
1 // Fig. 8.13: fig08_13.cpp
2 // Standard library string class test program.
3 #include 
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 is \"" << s1 << "\"; s2 is \"" << s2
20 << "\"; s3 is \"" << s3 << '\"' 
21 << "\n\nThe results of comparing s2 and s1:"
22 << "\ns2 == s1 yields " 
23 << ( s2 == s1 ? "true" : "false" )
24 << "\ns2 != s1 yields " 
25 << ( s2 != s1 ? "true" : "false" )
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
68
fig08_13.cpp
(2 of 4)
26 s1 yields " 
27 s1 ? "true" : "false" ) 
28 << "\ns2 < s1 yields " 
29 << ( s2 < s1 ? "true" : "false" ) 
30 = s1 yields "
31 = s1 ? "true" : "false" )
32 << "\ns2 <= s1 yields " 
33 << ( s2 <= s1 ? "true" : "false" );
34 
35 // test string member function empty 
36 cout << "\n\nTesting s3.empty():\n";
37 
38 if ( s3.empty() ) {
39 cout << "s3 is empty; assigning s1 to s3;\n";
40 s3 = s1; // assign s1 to s3
41 cout << "s3 is \"" << s3 << "\"";
42 }
43 
44 // test overloaded string concatenation operator
45 cout << "\n\ns1 += s2 yields s1 = ";
46 s1 += s2; // test overloaded concatenation
47 cout << s1;
48 
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
69
fig08_13.cpp
(3 of 4)
49 // test overloaded string concatenation operator 
50 // with C-style string
51 cout << "\n\ns1 += \" to you\" yields\n";
52 s1 += " to you"; 
53 cout << "s1 = " << s1 << "\n\n";
54 
55 // test string member function substr
56 cout << "The substring of s1 starting at location 0 for\n"
57 << "14 characters, s1.substr(0, 14), is:\n"
58 << s1.substr( 0, 14 ) << "\n\n";
59 
60 // test substr "to-end-of-string" option
61 cout << "The substring of s1 starting at\n"
62 << "location 15, s1.substr(15), is:\n"
63 << s1.substr( 15 ) << '\n'; 
64 
65 // test copy constructor
66 string *s4Ptr = new string( s1 ); 
67 cout << "\n*s4Ptr = " << *s4Ptr << "\n\n";
68 
69 // test assignment (=) operator with self-assignment
70 cout << "assigning *s4Ptr to *s4Ptr\n";
71 *s4Ptr = *s4Ptr; 
72 cout << "*s4Ptr = " << *s4Ptr << '\n';
73 
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
70
fig08_13.cpp
(4 of 4)
74 // test destructor
75 delete s4Ptr; 
76 
77 // test using subscript operator to create lvalue
78 s1[ 0 ] = 'H'; 
79 s1[ 6 ] = 'B';
80 cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "
81 << s1 << "\n\n";
82 
83 // test subscript out of range with string member function "at"
84 cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl;
85 s1.at( 30 ) = 'd'; // ERROR: subscript out of range
86 
87 return 0;
88 
89 } // end main
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
71
fig08_13.cpp
output (1 of 2)
s1 is "happy"; s2 is " birthday"; s3 is ""
The results of comparing s2 and s1:
s2 == s1 yields false
s2 != s1 yields true
s2 > s1 yields false
s2 < s1 yields true
s2 >= s1 yields false
s2 <= s1 yields true
Testing s3.empty():
s3 is empty; assigning s1 to s3;
s3 is "happy"
s1 += s2 yields s1 = happy birthday
s1 += " to you" yields
s1 = happy birthday to you
The substring of s1 starting at location 0 for
14 characters, s1.substr(0, 14), is:
happy birthday
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
72
fig08_13.cpp
output (2 of 2)
The substring of s1 starting at
location 15, s1.substr(15), is:
to you
*s4Ptr = happy birthday to you
assigning *s4Ptr to *s4Ptr
*s4Ptr = happy birthday to you 
s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you
Attempt to assign 'd' to s1.at( 30 ) yields:
abnormal program termination
© 2003 Prentice Hall, Inc. All rights reserved.
73
8.13 Standard Library Classes string and 
vector
• Class vector
– Header , namespace std
– Lưu trữ kiểu dữ liệu bất kỳ
• vector myArray(10)
– Function size ( myArray.size() )
– Overloaded []
• lấy phần tử theo chỉ số, myArray[3]
– Overloaded !=, ==, and =
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
74
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 
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
75
fig08_14.cpp
(2 of 5)
25 // print integers1 size and contents
26 cout << "Size of vector integers1 is "
27 << integers1.size()
28 << "\nvector after initialization:\n";
29 outputVector( integers1 );
30 
31 // print integers2 size and contents
32 cout << "\nSize of vector integers2 is " 
33 << integers2.size()
34 << "\nvector after initialization:\n";
35 outputVector( integers2 );
36 
37 // input and print integers1 and integers2
38 cout << "\nInput 17 integers:\n";
39 inputVector( integers1 );
40 inputVector( integers2 );
41 
42 cout << "\nAfter input, the vectors contain:\n"
43 << "integers1:\n";
44 outputVector( integers1 );
45 cout << "integers2:\n";
46 outputVector( integers2 );
47 
48 // use overloaded inequality (!=) operator
49 cout << "\nEvaluating: integers1 != integers2\n";
50 
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
76
fig08_14.cpp
(3 of 5)
51 if ( integers1 != integers2 )
52 cout << "integers1 and integers2 are not equal\n";
53 
54 // create vector integers3 using integers1 as an 
55 // initializer; print size and contents 
56 vector integers3( integers1 ); // copy constructor
57 
58 cout << "\nSize of vector integers3 is "
59 << integers3.size()
60 << "\nvector after initialization:\n";
61 outputVector( integers3 );
62 
63 
64 // use overloaded assignment (=) operator 
65 cout << "\nAssigning integers2 to integers1:\n";
66 integers1 = integers2; 
67 
68 cout << "integers1:\n";
69 outputVector( integers1 );
70 cout << "integers2:\n";
71 outputVector( integers1 );
72 
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
77
fig08_14.cpp
(4 of 5)
73 // use overloaded equality (==) operator
74 cout << "\nEvaluating: integers1 == integers2\n";
75 
76 if ( integers1 == integers2 )
77 cout << "integers1 and integers2 are equal\n";
78 
79 // use overloaded subscript operator to create rvalue
80 cout << "\nintegers1[5] is " << integers1[ 5 ];
81 
82 // use overloaded subscript operator to create lvalue
83 cout << "\n\nAssigning 1000 to integers1[5]\n";
84 integers1[ 5 ] = 1000;
85 cout << "integers1:\n";
86 outputVector( integers1 );
87 
88 // attempt to use out of range subscript
89 cout << "\nAttempt to assign 1000 to integers1.at( 15 )"
90 << endl;
91 integers1.at( 15 ) = 1000; // ERROR: out of range
92 
93 return 0;
94 
95 } // end main
96 
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
78
fig08_14.cpp
(5 of 5)
97 // output vector contents
98 void outputVector( const vector &array )
99 {
100 for ( int i = 0; i < array.size(); i++ ) {
101 cout << setw( 12 ) << array[ i ];
102 
103 if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
104 cout << endl;
105 
106 } // end for
107 
108 if ( i % 4 != 0 )
109 cout << endl;
110 
111 } // end function outputVector
112 
113 // input vector contents
114 void inputVector( vector &array )
115 {
116 for ( int i = 0; i < array.size(); i++ )
117 cin >> array[ i ];
118 
119 } // end function inputVector
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
79
fig08_14.cpp
output (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
© 2003 Prentice Hall, Inc.
All rights reserved.
Outline
80
fig08_14.cpp
output (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:

  • pdfNgôn ngữ lập trình C++ - Chương 8 Operator Overloading.pdf
Tài liệu liên quan