Chapter 11 Operator Overloading: String and Array Objects 0 2018, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Operator Overloading; String and Array Objects Chapter 11
OBJECTIVES o What operator overloading is and how it makes programs more readable and programming more convenient. To redefine(overload) operators to work with objects of user-defined classes o the differences between overloading unary and binary operators. o To convert objects from one class to another class o When to, and when not to, overload operators 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES What operator overloading is and how it makes programs more readable and programming more convenient. To redefine (overload) operators to work with objects of user-defined classes. The differences between overloading unary and binary operators. To convert objects from one class to another class. When to, and when not to, overload operators
Topics o 11.1 Introduction o 11.2 Fundamentals restrictions o 11.3 Operator Functions as class members vs. Global Functions 11.4 Overloading Stream Insertion and Stream Extraction Operators 11.5 Overloading Unary Operators o 11.6 Overloading Binary Operators 11.7 Case Study: Array Class o 11.8 Converting between types o 11.9 Case Study: String Class 11.10 Standard Library Class string(self study) 11.11 Overloading + and-(self study) o 11.12 Case Study: A Date Class(self studi 0 2018 SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 Topics 11.1 Introduction 11.2 Fundamentals & Restrictions 11.3 Operator Functions as Class Members vs. Global Functions 11.4 Overloading Stream Insertion and Stream Extraction Operators 11.5 Overloading Unary Operators 11.6 Overloading Binary Operators 11.7 Case Study: Array Class 11.8 Converting between types 11.9 Case Study: String Class 11.10 Standard Library Class string(self study) 11.11 Overloading ++ and ––(self study) 11.12 Case Study: A Date Class(self study)
11.1 Introduction 复习:函数重载 要求 °函数名相同,参数列表不同 °仅返回值不同不作为重载 注意:有缺省参数的函数 func(int i, intj= o)func(int i) 特例: const成员函数 void Employee: display o0 void Employee: display( const t 0 2018, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 11.1 Introduction 复习:函数重载 要求 函数名相同,参数列表不同 仅返回值不同不作为重载 注意:有缺省参数的函数 func(int i, int j = 0) func(int i) 特例:const成员函数 void Employee::display(){} void Employee::display() const {}
11.1 Introduction o cout<< int variable;/整型变量 cout<< priNt:∥/整型指针 cout<< ptrChar;//字符指针 “<<”流插入运算符&按位左移运算符 temp 14 <<2 temp =56 o int num = 10: num num+1: o double num =1.0 num s num + 1.0 int num[10] *pNum num; pNum pNum o operator overloading运算符重载 0 2018, SEU. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 11.1 Introduction cout << int_variable;// 整型变量 cout << ptrInt;// 整型指针 cout << ptrChar;// 字符指针 “<<”流插入运算符 & 按位左移运算符 temp = 14 << 2 temp = int num = 10; num = num + 1; double num = 1.0; num = num + 1.0; int num[10], *pNum = num; pNum = pNum + 1; operator overloading 运算符重载 56