东南大学:《C++语言程序设计》课程教学资源(PPT课件讲稿)Chapter 11 Operator Overloading; String and Array Objects(主讲:东方)

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《计算机网络》课程实验教学大纲.pdf
- 广西医科大学:《计算机网络 Computer Networking》课程教学资源(PPT课件讲稿)Chapter 18 NETWORK DESIGN AND IMPLEMENTATION.pptx
- 《多媒体技术基础》课程教学资源(PPT课件讲稿)单元1 多媒体概述.ppt
- 中国科学技术大学:《并行算法实践》课程教学资源(PPT课件讲稿)上篇 并行程序设计导论 单元I 并行程序设计基础 第三章 并行程序设计简介.ppt
- 《计算机控制技术》课程教学资源(PPT课件讲稿)第二章 模拟量输出通道.ppt
- 哈尔滨工业大学:开放式中文实体关系抽取研究(导师:秦兵).pptx
- 兰州大学:《SOA & Web Service》教学资源(PPT课件讲稿)Lecture 5 Web Service Program(苏伟).ppt
- 中国科学技术大学:《高级操作系统 Advanced Operating System》课程教学资源(PPT课件讲稿)第四章 分布式进程和处理机管理(主讲:熊焰).ppt
- 香港浸会大学:《网络管理 Network Management》课程教学资源(PPT课件讲稿)Chapter 02 Network Management Model.ppt
- 对等网络 Peer-to-Peer Networks(P2P).ppt
- Threads, SMP, and MicroKernels.ppt
- Object-Oriented Programming(Java).ppt
- 《编译原理》课程教学资源(PPT课件讲稿)第三章 语法分析.ppt
- 《操作系统》课程教学资源(PPT课件)第六章 设备管理 Devices Management.ppt
- 《计算机组装维修及实训教程》课程教学资源(PPT课件)第2章 中央处理器.ppt
- 《轻松学习C语言》教学资源(PPT课件讲稿,繁体版,共十二章).pptx
- 西安电子科技大学:《Mobile Programming》课程PPT教学课件(Android Programming)Lecture 7 数据持久化 Data Persistence.pptx
- 《数据结构》课程教学资源(PPT课件讲稿)第四章 串.ppt
- 四川大学:《数据库技术》课程教学资源(PPT课件讲稿)第1章 数据库技术概论.ppt
- Urandaline Investments The Perils of Down Under:Chinese Investment in Australia.pptx
- 《大学计算机基础》课程教学资源(PPT课件讲稿)第四章 Excel 2007电子表格.ppt
- 进程(PPT课件讲稿)Processes.pptx
- 电子科技大学:《汇编语言程序设计》课程教学资源(PPT课件)第一章 基础知识(主讲:詹瑾瑜).ppt
- 《数据库系统概论》课程教学资源(PPT课件讲稿)第六章 数据库设计.ppt
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)MSI、MESI、分布式共享存储器体系结构、Models of Memory Consistency.pptx
- 《汇编语言》课程教学资源(PPT课件讲稿)第6章 子程序.ppt
- 中国科学技术大学:《网络信息安全 NETWORK SECURITY》课程教学资源(PPT课件讲稿)第二章 数据加密技术基础.ppt
- 北京大学:《软件需求工程》课程教学资源(PPT课件讲稿)第十章 软件需求开发与管理工具.ppt
- 《计算机组装与维修》课程教学资源(PPT课件讲稿)第十三章 局域网维护及常见故障处理.ppt
- 南京大学:《编译原理》课程教学资源(PPT课件讲稿)第四章 语法分析(戴新宇).pptx
- 北京大学:《高级编译技术 Advanced Compiler Techniques》课程教学资源(PPT课件讲稿)Introduction to Optimizations.ppt
- 香港大学:Data Analysis - Factors Potentially Affecting Development.pptx
- 西安电子科技大学:《操作系统 Operating Systems》课程教学资源(PPT课件讲稿)Chapter 06 文件系统 File Systems(主讲:高海昌).ppt
- 南京大学:《自然语言处理 Natural Language Processing(NLP)》课程教学资源(PPT课件讲稿)自然语言处理概述、基于规则(知识工程)的传统自然语言处理方法(理性方法).ppt
- 香港科技大学:片上网络(PPT讲稿)network-on-chip(NoC)NoC Building Blocks.pptx
- 《数据结构》课程教学资源(PPT课件讲稿)第五章 数组.ppt
- 语义网与本体(PPT讲稿)Semantic Web & Ontology(元数据 Metadata).ppt
- 软件开发环境与工具(PPT讲稿)Software development environment and tool.ppt
- 哈尔滨工业大学:逻辑斯蒂回归与最大熵(PPT课件讲稿).pptx
- 《机器学习》教学资源(PPT讲稿)支持向量机 support vector machines.ppt