smie-121 software design ii [email protected] school of mobile information engineering, sun...

49
SMIE-121 Software Design II http://smiesd.sinaapp.com/ [email protected] School of Mobile Information Engineering, Sun Yat- sen University Lecture 07. C++ Operator Overloading

Upload: flora-noreen-reeves

Post on 11-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

SMIE-121 Software Design IIhttp://smiesd.sinaapp.com/

[email protected]

School of Mobile Information Engineering, Sun Yat-sen University

Lecture 07. C++ Operator Overloading

Page 2: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

2 / 49April 21, 2023

Design and programming are human activities;forget that and all is lost.

--Bjarne Stroustrup, 1991

Page 3: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

3 / 49April 21, 2023

Outline

Introduction Class Practice Methods vs. Friend Functions Unary vs. Binary Case Study: Array Other Operator Overloadings

Page 4: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

4 / 49April 21, 2023

( 1 )运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时,产生不同类型的行为。 C++允许同一运算符具有多种不同运算功能的机制。

( 2 )为什么需要运算符重载: C++中预定义了许多运算符 , 能够满足用户的一般应用要求 , 但是其运算的对象应是基本数据类型 , 而不适用于用户自定义数据类型 ( 如类 ),这可以使用运算符重载。

( 3 )明智地使用操作符重载可以使类类型的使用像内置类型一样直观

( 4 )操作符重载的实质是函数重载。

Introduction

Page 5: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

5 / 49April 21, 2023

Operator overloading

• Use traditional operators with user-defined objects

• Straightforward and natural way to extend C++

• Requires great care

• When overloading is misused, programs become difficult to understand

class Point

{

public:

Point(int xx=0,int yy=0){ x=xx;y=yy; }

int getX();

int getY();

//...

private:

int x;

int y ;

};

Point p1(1,1),p2(3,3) ,实现“ p1 + p2” 则需要编写程序来说明“ +”如何作用于 Point对象( p1, p2),即 p1的私有成员 x , y 与 p2的私有成员 x ,y 分别相加这一功能。

Introduction

Page 6: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

6 / 49April 21, 2023

Use operator overloading to improve readability• Avoid excessive or inconsistent usage

Format• Write function definition as normal(重载函数跟普通函数一样,具有返回类型和形参表 )

• Function name is keyword operator followed by the symbol for the operator being overloaded.

• operator+ would be used to overload the addition operator (+).

Fundamentals of Operator Overloading

Page 7: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

7 / 49April 21, 2023

Assignment operator (=) • may be used with every class without explicit overloading

• memberwise assignment

• Same is true for the address operator (&)

class MyClass { public: ... MyClass& operator=(const MyClass &rhs); ... }

MyClass a, b; ... b = a; // Same as b.operator=(a);

Fundamentals of Operator Overloading

Page 8: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

8 / 49April 21, 2023

Most of C++’s operators can be overloaded

• 有四个符号( +, -, * 和 &)既可作一元操作符又可作二元操作符,可以同时定义一元和二元操作符

• 重载操作符不能改变原操作符的优先级、结合性或操作数目• 重载操作符必须具有至少一个类类型或枚举类型的操作数,也就是说重载操作符不能重新定义用于内置类型对象的操作符的含义,比如不能重载” +”号操作符用于两个整数相加。

Restrictions on Operator Overloading

Page 9: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

9 / 49April 21, 2023

Outline

Introduction Class Practice Methods vs. Friend Functions Unary vs. Binary Case Study: Array Other Operator Overloadings

Page 10: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

10 / 49April 21, 2023

Write an overloading for the operator “*=” for this class

class Point

{

public:

Point(int xx=0,int yy=0){ x=xx;y=yy; }

int getX();

int getY();

//...

private:

int x;

int y;

};

Point p1(1,1),p2(3,3) ,实现“ p1 *= p2” ,即 p1 的私有成员 x , y

与 p2的私有成员 x , y 分别相乘,并将结果分别赋于 p1的私有成员。

Class practice

Page 11: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

11 / 49April 21, 2023

Outline

Introduction Class Practice Methods vs. Friend Functions Unary vs. Binary Case Study: Array Other Operator Overloadings

Page 12: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

12 / 49April 21, 2023

Operator functions • Can be member or non-member functions

Overloading the assignment operators • i.e:(), [], ->,=

• Operator must be a member function

Operator Functions as Class Members vs. as friend Functions

Page 13: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

13 / 49April 21, 2023

Operator functions as member functions• Leftmost operand must be an object (or reference to an object) of the class

• If left operand of a different type( 与定义的 class 不一致 ), operator function must be a non-member function

• A non-member operator function must be a friend if private or protected members of that class are accessed directly 如果需要访问类的私有或者保护对象

Operator Functions as Class Members vs. as friend Functions

Page 14: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

14 / 49April 21, 2023

Non-member overloaded operator functions• Enable the operator to be commutative( 可交换的 ) , because

the leftmost operand can be another type.

HugeInteger bigInteger1;

long int number;

bigInteger1 = number + bigInteger1;

or

bigInteger1 = bigInteger1 + number;

Operator Functions as Class Members vs. as friend Functions

Page 15: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

15 / 49April 21, 2023

Friend function Member Function

a + b operator+(a,b) a.operator+(b)

a++ operator++(a,0)

a.operator++(0)

-a operator-(a) a.operator-( )

类成员的操作符函数与其等价的友元函数:

Operator Functions as Class Members vs. as friend Functions

Page 16: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

16 / 49April 21, 2023

友元运算符函数无 this指针,因而在函数体内必须通过对象名来使用 private成员数据;一般将一元运算符函数重载为类运算符函数(“ =”也必须)。因为一元运算符时常需要用到 this 指针(为什么?)

一般将二元运算符重载为友元运算符函数,以免出现使用错误。因为时常需要操作数可交换

“=”、“()”、“ []”和“ ->”不能重载为友元。

Operator Functions as Class Members vs. as friend Functions

Page 17: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

17 / 49April 21, 2023

Overloaded << and >> operators • Must have left operand of types ostream &, istream &

respectively

• It must be a non-member (friend) function (left operand not an object of the class)

• It must be a friend function if it accesses private data members

Overloading Stream-Insertion and Stream-Extraction Operators

语法:

friend<函数类型 > operator<运算符 >(形参表 )

{

函数体;

}

Page 18: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

18 / 49April 21, 2023

Overloading Stream-Insertion and Stream-Extraction Operators

both design and programming comply with the top-down & step-wised refinement methodology

Page 19: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

fig18_03.cpp (1 of 3)

Page 20: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

fig18_03.cpp (2 of 3)

26 // Overloaded stream-insertion operator (cannot be

27 // a member function if we would like to invoke it with

28 // cout << somePhoneNumber;).

29 ostream &operator<<( ostream &output, const PhoneNumber &num )

30 {

31 output << "(" << num.areaCode << ") "

32 << num.exchange << "-" << num.line;

33 return output; // enables cout << a << b << c;

34 } // end operator<< function

35

36 istream &operator>>( istream &input, PhoneNumber &num )

37 {

38 input.ignore(); // skip (

39 input >> setw( 4 ) >> num.areaCode; // input area code

40 input.ignore( 2 ); // skip ) and space

41 input >> setw( 4 ) >> num.exchange; // input exchange

42 input.ignore(); // skip dash (-)

43 input >> setw( 5 ) >> num.line; // input line

44 return input; // enables cin >> a >> b >> c;

45 } // end operator>> function

46

47 int main()

48 {

49 PhoneNumber phone; // create object phone

50

Page 21: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline51 cout << "Enter phone number in the form (123) 456-7890:\n";

52

53 // cin >> phone invokes operator>> function by

54 // issuing the call operator>>( cin, phone ).

55 cin >> phone;

56

57 // cout << phone invokes operator<< function by

58 // issuing the call operator<<( cout, phone ).

59 cout << "The phone number entered was: " << phone << endl;

60 return 0;

61 } // end function main

fig18_03.cpp (3 of 3)

Enter phone number in the form (123) 456-7890:(800) 555-1212The phone number entered was: (800) 555-1212

Page 22: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

22 / 49April 21, 2023

Outline

Introduction Class Practice Methods vs. Friend Functions Unary vs. Binary Case Study: Array Other Operator Overloadings

Page 23: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

23 / 49April 21, 2023

Overloading unary operators• Avoid friend functions and friend classes unless

absolutely necessary.

• Use of friends violates the encapsulation of a class.

• As a member function:

class String {public: bool operator!() const; ...};

< 返回类型 > operator<运算符 >(形参表 )

{

函数体;

}

Overloading Unary Operators

Page 24: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

24 / 49April 21, 2023

Overloaded binary operators• Non-static member function, one argument

• Non-member function (friend), two arguments

class String {public: const String &operator+=( const String & ); ...}; // end class String

y += z;

equivalent to y.operator+=( z );

Overloading Binary Operators

Page 25: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

25 / 49April 21, 2023

Example

class String{friend const String &operator+=( String &, const String & );

...}; // end class String

y += z;

equivalent to operator+=( y, z );

语法:

friend< 返回类型 > operator< 运算符 >( 形参表 )

{

函数体;

}

Overloading Binary Operators

Page 26: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

26 / 49April 21, 2023

Outline

Introduction Class Practice Methods vs. Friend Functions Unary vs. Binary Case Study: Array Other Operator Overloadings

Page 27: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

27 / 49April 21, 2023

Implement an Array class with • Range checking

• Array assignment

• Arrays that know their size

• Outputting/inputting entire arrays with << and >>

• Array comparisons with == and !=

Case Study: An Array class

Page 28: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.h (1 of 2)

1 // Fig. 18.4: array1.h

2 // Simple class Array (for integers)

3 #ifndef ARRAY1_H

4 #define ARRAY1_H

5

6 #include <iostream>

7

8 using std::ostream;

9 using std::istream;

10

11 class Array {

12 friend ostream &operator<<( ostream &, const Array & );

13 friend istream &operator>>( istream &, Array & );

14 public:

15 Array( int = 10 ); // default constructor

16 Array( const Array & ); // copy constructor

17 ~Array(); // destructor

18 int getSize() const; // return size

19 const Array &operator=( const Array & ); // assign arrays

20 bool operator==( const Array & ) const; // compare equal

21

22 // Determine if two arrays are not equal and

23 // return true, otherwise return false (uses operator==).

24 bool operator!=( const Array &right ) const

25 { return ! ( *this == right ); }

26

Page 29: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.h (2 of 2)

27 int &operator[]( int ); // subscript operator

28 const int &operator[]( int ) const; // subscript operator

29 static int getArrayCount(); // Return count of

30 // arrays instantiated.

31 private:

32 int size; // size of the array

33 int *ptr; // pointer to first element of array

34 static int arrayCount; // # of Arrays instantiated

35 }; // end class Array

36

37 #endif

why there are two operator[] methods here?

Page 30: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

30 / 49April 21, 2023

38 // Fig 18.4: array1.cpp

39 // Member function definitions for class Array

40 #include <iostream>

41

42 using std::cout;

43 using std::cin;

44 using std::endl;

45

46 #include <iomanip>

47

48 using std::setw;

49

50 #include <cstdlib>

51 #include <cassert>

52 #include "array1.h"

53

array1.cpp (1 of 6)

Page 31: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.cpp (2 of 6)

54 // Initialize static data member at file scope

55 int Array::arrayCount = 0; // no objects yet

56

57 // Default constructor for class Array (default size 10)

58 Array::Array( int arraySize )

59 {

60 size = ( arraySize > 0 ? arraySize : 10 );

61 ptr = new int[ size ]; // create space for array

62 assert( ptr != 0 ); // terminate if memory not allocated

63 ++arrayCount; // count one more object

64

65 for ( int i = 0; i < size; i++ )

66 ptr[ i ] = 0; // initialize array

67 } // end Array constructor

68

69 // Copy constructor for class Array

70 // must receive a reference to prevent infinite recursion

71 Array::Array( const Array &init ) : size( init.size )

72 {

73 ptr = new int[ size ]; // create space for array

74 assert( ptr != 0 ); // terminate if memory not allocated

75 ++arrayCount; // count one more object

76

77 for ( int i = 0; i < size; i++ )

78 ptr[ i ] = init.ptr[ i ]; // copy init into object

79 } // end Array constructor

80

Page 32: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.cpp (3 of 6)

81 // Destructor for class Array

82 Array::~Array()

83 {

84 delete [] ptr; // reclaim space for array

85 --arrayCount; // one fewer object

86 } // end Array destructor

88 // Get the size of the array

89 int Array::getSize() const { return size; }

90

Page 33: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outlinearray1.cpp (3 of 6)

92 // const return avoids: ( a1 = a2 ) = a3

93 const Array &Array::operator=( const Array &right )

94 {

95 if ( &right != this ) { // check for self-assignment

96

97 // for arrays of different sizes, deallocate original

98 // left side array, then allocate new left side array.

99 if ( size != right.size ) {

100 delete [] ptr; // reclaim space

101 size = right.size; // resize this object

102 ptr = new int[ size ]; // create space for array

103 assert( ptr != 0 ); // terminate if not allocated

104 } // end if

105

106 for ( int i = 0; i < size; i++ )

107 ptr[ i ] = right.ptr[ i ]; // copy array into object

108 } // end if

110 return *this; // enables x = y = z;

111 } // end operator= function

Page 34: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.cpp (4 of 6)

112

113 // Determine if two arrays are equal and

114 // return true, otherwise return false.

115 bool Array::operator==( const Array &right ) const

116 {

117 if ( size != right.size )

118 return false; // arrays of different sizes

119

120 for ( int i = 0; i < size; i++ )

121 if ( ptr[ i ] != right.ptr[ i ] )

122 return false; // arrays are not equal

123

124 return true; // arrays are equal

125 } // end operator== function

126

127 // Overloaded subscript operator for non-const Arrays

128 // reference return creates an lvalue

129 int &Array::operator[]( int subscript )

130 {

131 // check for subscript out of range error

132 assert( 0 <= subscript && subscript < size );

133

134 return ptr[ subscript ]; // reference return

135 } // end operator[] function

136

Page 35: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.cpp (5 of 6)

137 // Overloaded subscript operator for const Arrays

138 // const reference return creates an rvalue

139 const int &Array::operator[]( int subscript ) const

140 {

141 // check for subscript out of range error

142 assert( 0 <= subscript && subscript < size );

143

144 return ptr[ subscript ]; // const reference return

145 } // end operator[] function

146

147 // Return the number of Array objects instantiated

148 // static functions cannot be const

149 int Array::getArrayCount() { return arrayCount; }

150

151 // Overloaded input operator for class Array;

152 // inputs values for entire array.

153 istream &operator>>( istream &input, Array &a )

154 {

155 for ( int i = 0; i < a.size; i++ )

156 input >> a.ptr[ i ];

157

158 return input; // enables cin >> x >> y;

159 } // end operator>> function

160

Page 36: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

array1.cpp (6 of 6)

161 // Overloaded output operator for class Array

162 ostream &operator<<( ostream &output, const Array &a )

163 {

164 int i;

165

166 for ( i = 0; i < a.size; i++ ) {

167 output << setw( 12 ) << a.ptr[ i ];

168

169 if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output

170 output << endl;

171 } // end for

172

173 if ( i % 4 != 0 )

174 output << endl;

175

176 return output; // enables cout << x << y;

177 } // end operator<< function

Page 37: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

fig18_04.cpp (1 of 4)

178 // Fig. 18.4: fig18_04.cpp

179 // Driver for simple class Array

180 #include <iostream>

181

182 using std::cout;

183 using std::cin;

184 using std::endl;

185

186 #include "array1.h"

187

Page 38: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

fig18_04.cpp (2 of 4)

188 int main()

189 {

190 // no objects yet

191 cout << "# of arrays instantiated = "

192 << Array::getArrayCount() << '\n';

193

194 // create two arrays and print Array count

195 Array integers1( 7 ), integers2;

196 cout << "# of arrays instantiated = "

197 << Array::getArrayCount() << "\n\n";

198

199 // print integers1 size and contents

200 cout << "Size of array integers1 is "

201 << integers1.getSize()

202 << "\nArray after initialization:\n"

203 << integers1 << '\n';

204

205 // print integers2 size and contents

206 cout << "Size of array integers2 is "

207 << integers2.getSize()

208 << "\nArray after initialization:\n"

209 << integers2 << '\n';

210

Page 39: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

fig18_04.cpp (3 of 4)

211 // input and print integers1 and integers2

212 cout << "Input 17 integers:\n";

213 cin >> integers1 >> integers2;

214 cout << "After input, the arrays contain:\n"

215 << "integers1:\n" << integers1

216 << "integers2:\n" << integers2 << '\n';

217

218 // use overloaded inequality (!=) operator

219 cout << "Evaluating: integers1 != integers2\n";

220 if ( integers1 != integers2 )

221 cout << "They are not equal\n";

222

223 // create array integers3 using integers1 as an

224 // initializer; print size and contents

225 Array integers3( integers1 );

226

227 cout << "\nSize of array integers3 is "

228 << integers3.getSize()

229 << "\nArray after initialization:\n"

230 << integers3 << '\n';

231

232 // use overloaded assignment (=) operator

233 cout << "Assigning integers2 to integers1:\n";

234 integers1 = integers2;

235 cout << "integers1:\n" << integers1

236 << "integers2:\n" << integers2 << '\n';

237

Page 40: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

fig18_04.cpp (4 of 4)

238 // use overloaded equality (==) operator

239 cout << "Evaluating: integers1 == integers2\n";

240 if ( integers1 == integers2 )

241 cout << "They are equal\n\n";

242

243 // use overloaded subscript operator to create rvalue

244 cout << "integers1[5] is " << integers1[ 5 ] << '\n';

245

246 // use overloaded subscript operator to create lvalue

247 cout << "Assigning 1000 to integers1[5]\n";

248 integers1[ 5 ] = 1000;

249 cout << "integers1:\n" << integers1 << '\n';

250

251 // attempt to use out of range subscript

252 cout << "Attempt to assign 1000 to integers1[15]" << endl;

253 integers1[ 15 ] = 1000; // ERROR: out of range

254

255 return 0;

256 } // end function main

Page 41: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

Array output (1 of 1)

# of arrays instantiated = 0# of arrays instantiated = 2 Size of array integers1 is 7Array after initialization: 0 0 0 0 0 0 0 Size of array integers2 is 10Array 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 17After input, the arrays contain:integers1: 1 2 3 4 5 6 7integers2: 8 9 10 11 12 13 14 15 16 17 Evaluating: integers1 != integers2They are not equal Size of array integers3 is 7Array after initialization: 1 2 3 4 5 6 7

Page 42: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Outline

Array output (2 of 2)

Assigning integers2 to integers1:integers1: 8 9 10 11 12 13 14 15 16 17integers2: 8 9 10 11 12 13 14 15 16 17

Evaluating: integers1 == integers2They are equal integers1[5] is 13Assigning 1000 to integers1[5]integers1: 8 9 10 11 12 1000 14 15 16 17 Attempt to assign 1000 to integers1[15]Assertion failed: 0 <= subscript && subscript < size, file Array1.cpp, line 95 abnormal program termination

Page 43: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

43 / 49April 21, 2023

Cast operator• Convert objects into built-in types or other objects

• Conversion operator must be a non-static member function.

• Cannot be a friend function

• 形参表必须为空• 不能指定返回类型,但是必须显式返回一个指定类型的值转换函数采用如下通用形式:

operator type();

type 表示内置类型名、类类型名或由类型别名定义的名字,对任何可作为函数返回类型的类型(除了 void 之外)都可以定义转换函数。

For user-defined class AA::operator char *() const;A::operator int() const;A::operator otherClass() const;

• When compiler sees (char *) s it calls s.operator char*()

Converting between Types

Page 44: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

44 / 49April 21, 2023

Outline

Introduction Class Practice Methods vs. Friend Functions Unary vs. Binary Case Study: Array Other Operator Overloadings

Page 45: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

45 / 49April 21, 2023

The compiler can call these functions to create temporary objects.

• If s is not of type char *

Calls A::operator char *() const; for

cout << s;

Converting between Types

Page 46: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

46 / 49April 21, 2023

Pre/post-incrementing/decrementing operators • Can be overloaded

• How does the compiler distinguish between the two?

• Prefix versions overloaded same as any other prefix unary operator would be. i.e. d1.operator++(); for ++d1;

Postfix versions• When compiler sees postincrementing expression, such as d1++;• Generates the member-function call

d1.operator++( 0 );

• Prototype:Date::operator++( int );

Overloading ++ and --

Page 47: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

47 / 49April 21, 2023

前置单目运算符和后置单目运算符重载的区别:函数的形参。

语法规定 :

•前置单目运算符重载为成员函数时没有形参•后置单目运算符重载为成员函数时,需要有一个 int 型形参。

(这个 int 型参数在函数体中并不使用 , 专门用来区别前置与

后置)

Overloading ++ and --

Page 48: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

48 / 49April 21, 2023

Class Diagram

class nameclass name

attributesattributes

methodsmethodscompositioncomposition

dependency, friend

dependency, friend functionfunction

• Align elements as possible• No cross lines, no diagonal lines

Page 49: SMIE-121 Software Design II  raoyangh@mail.sysu.edu.cn School of Mobile Information Engineering, Sun Yat-sen University Lecture

Software Design II – C++ Operator Overloading

49 / 49April 21, 2023

Thank you!