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
11.1 Introduction ●C++语言为了支持基本数据类型数据运算,内置 了多种运算符,并且其中部分已针对操作数类型 的不同进行了重载; °当需要将这些运算符用于用户自定义类型时,用 户可进行运算符重载。 °重载运算符的基本概念、限制,何时选择重载? °如何实现重载?全局vs成员函数 °拷贝构造函数/转换构造函数 °自定义 String类vs标准 string类 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 11.1 Introduction C++语言为了支持基本数据类型数据运算,内置 了多种运算符,并且其中部分已针对操作数类型 的不同进行了重载; 当需要将这些运算符用于用户自定义类型时,用 户可进行运算符重载。 重载运算符的基本概念、限制,何时选择重载? 如何实现重载?全局 vs 成员函数 拷贝构造函数 / 转换构造函数 自定义String类 vs 标准string类
Topics o 11.1 Introduction o 11.2 Fundamentals Restrictions o 11.3 Operator Functions as class Members vs Global Functions o 11.4 Overloading Stream Insertion and Stream Extraction Operators 11.5 Overloading Unary Operators 11.6 Overloading Binary Operators o 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) o 11.11 Overloading + and -(self study) o 11.12 Case Study: A Date Class(self study) 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 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.2 Fundamentals Restrictions 需求 目的:提高类代码的可用、可读性 HugeintA. add(HugeIntB)vs HugeintA HugeintB ●提高C++的可扩展性 °特别适合于和数学相关的类 复数类 °大整数类 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 11.2 Fundamentals & Restrictions --- 需求 目的:提高类代码的可用、可读性 HugeintA.add(HugeIntB) vs HugeintA + HugeintB 提高C++的可扩展性 特别适合于和数学相关的类 复数类 大整数类
11.2 Fundamentals Restrictions 语法 °运算符重载只是一种“语法上的方便”,也就是 说它只是另一种函数调用的方式。区别: 定义方式 调用方式 定义重载的运算符(可视为特殊函数)就像定义函数 (全局/成员),区别是该函数的名称是 operator@ 其中 operator是关键词,@是被重载的运算符, 如: Hugelnt operator+(const Hugelnt& a); 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 11.2 Fundamentals & Restrictions --- 语法 运算符重载只是一种“语法上的方便”,也就是 说它只是另一种函数调用的方式。区别: 定义方式 调用方式 定义重载的运算符(可视为特殊函数)就像定义函数 (全局/成员),区别是该函数的名称是 operator@ 其中operator是关键词,@是被重载的运算符, 如: HugeInt operator+(const HugeInt& a);
11.2 Fundamentals Restrictions 语法 °运算符重载只是一种“语法上的方便”,也就是 说它只是另一种函数调用的方式。区别: °定义方式 调用方式 普通函数 °全局函数:函数名(参数列表) °类成员函数:对象函数名(参数列表等 重载的运算符 使用时以表达式形式出现: HugelntA+ HugelntB 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 11.2 Fundamentals & Restrictions --- 语法 运算符重载只是一种“语法上的方便”,也就是 说它只是另一种函数调用的方式。区别: 定义方式 调用方式 普通函数 全局函数: 函数名(参数列表) 类成员函数: 对象.函数名(参数列表)等 重载的运算符 使用时以表达式形式出现: HugeIntA + HugeIntB