《C程序设计》电子教案第7章运算符重载.ppt

上传人:max****ui 文档编号:8309620 上传时间:2020-03-28 格式:PPT 页数:57 大小:224.50KB
返回 下载 相关 举报
《C程序设计》电子教案第7章运算符重载.ppt_第1页
第1页 / 共57页
《C程序设计》电子教案第7章运算符重载.ppt_第2页
第2页 / 共57页
《C程序设计》电子教案第7章运算符重载.ppt_第3页
第3页 / 共57页
点击查看更多>>
资源描述
第7章运算符重载 7 1运算符重载概述7 2运算符重载的实现7 3一元运算符重载7 4二元运算符重载7 5特殊运算符重载 7 1运算符重载概述 运算符重载是对已有的运算符赋予多重含义 同一个运算符作用于不同类型的数据导致不同类型的行为 运算符重载的实质就是函数重载 在实现过程中 首先把指定的运算表达式转化为对运算符函数的调用 运算对象转化为运算符函数的实参 然后根据实参的类型来确定需要调用的函数 这个过程是在编译过程中完成的 返回首页 表7 1C 可以重载的运算符 表7 2C 不能被重载的运算符 运算符重载的规则如下 1 C 中的运算符除了少数几个以外 全部可以重载 而且只能重载已有的这些运算符 2 重载之后运算符的优先级和结合性都不会改变 3 运算符重载是针对新类型数据的实际需要 对原有运算符进行适当的改造 返回本节 7 2运算符重载的实现 运算符的重载形式有两种 重载为类的成员函数和重载为类的友元函数 运算符重载为类的成员函数的语法形式如下 operator 运算符重载为类的友元函数的语法形式如下 friendoperator 返回首页 例7 1 以成员函数重载运算符重载两字符串加法 include includeclassString charname 256 public String char str strcpy name str String String Stringoperator constString staticchar str StringString operator constString Stringdemo3 demo1 demo2 demo3 display Stringdemo4 demo3 Programming demo4 display deletestr 此程序的运行结果为 Thestringis VisualC Thestringis 6 0Thestringis VisualC 6 0Thestringis VisualC Programming 例7 2 下面程序定义一个Time类用来保存时间 时 分 秒 通过重载操作符 实现两个时间的相加 includeclassTime public Time hours 0 minutes 0 seconds 0 无参构造函数Time inth intm ints 重载构造函数 hours h minutes m seconds s Timeoperator Time TimeTime operator Time 输出结果为 13 7 10 例7 4 下面程序修改了例7 2 将操作符重载为友元函数实现 includeclassTime public Time hours 0 minutes 0 seconds 0 无参构造函数Time inth intm ints 重载构造函数 hours h minutes m seconds s friendTimeoperator Time Timeoperator Time voidTime gettime cout hours minutes seconds endl voidmain Timet1 8 51 40 t2 4 15 30 t3 t3 t1 t2 t3 gettime 输出结果为 13 7 10 返回本节 7 3一元运算符重载 类的单目运算符可重载为一个没有参数的非静态成员函数或者带有一个参数的非成员函数 参数必须是用户自定义类型的对象或者是对该对象的引用 在C 中 单目运算符有 和 它们是变量自动增1和自动减1的运算符 在类中可以对这两个单目运算符进行重载 返回首页 如同 运算符有前缀和后缀两种使用形式一样 和 重载运算符也有前缀和后缀两种运算符重载形式 以 重载运算符为例 其语法格式如下 operator 前缀运算operator int 后缀运算使用前缀运算符的语法格式如下 使用后缀运算符的语法格式如下 例7 5 成员函数重载示例 includeclassIncrease public Increase intx value x Increase 再返回原对象 IncreaseIncrease operator int Increasetemp this 临时对象存放原有对象值value 原有对象增量修改returntemp 返回原有对象值 voidmain Increasen 20 n display n display 显示临时对象值n display 显示原有对象 n n display n n display n 第二次增量操作对临时对象进行n display 此程序的运行结果为 thevalueis20thevalueis20thevalueis21thevalueis22thevalueis24thevalueis25 例7 6 友元函数重载示例 includeclassIncrease public Increase intx value x friendIncrease Increase n display 显示临时对象值n display 显示原有对象 n n display n n display n 第二次增量操作对临时对象进行n display 此程序的运行结果为 thevalueis20thevalueis20thevalueis21thevalueis22thevalueis24thevalueis25 返回本节 7 4二元运算符重载 对于双目运算符 一个运算数是对象本身的数据 由this指针给出 另一个运算数则需要通过运算符重载函数的参数表来传递 下面分别介绍这两种情况 对于双目运算符B 如果要重载B为类的成员函数 使之能够实现表达式 oprd1Boprd2 其中oprd1为A类的对象 则应当把B重载为A类的成员函数 该函数只有一个形参 形参的类型是oprd2所属的类型 经过重载之后 表达式oprd1Boprd2就相当于函数调用 oprd1 operatorB oprd2 返回首页 例7 7 设计一个点类Point 实现点对象之间的各种运算 includeclassPoint intx y public Point x y 0 Point inti intj x i y j Point Point 运算符重载 判断两个对象是否相同 booloperator Point 运算符重载 判断两个对象是否不相同voidoperator Point 运算符重载 将两个点对象相加voidoperator Point 运算符重载 将两个点对象相减Pointoperator Point 运算符重载 相加并将结果放在左操作数中Pointoperator Point 运算符重载 相减并将结果放在左操作数中intgetx returnx intgety returny voiddisp cout x y endl Point Point Point voidPoint offset inti intj x i y j voidPoint offset Pointp x p getx y p gety boolPoint operator Pointp if x p getx boolPoint operator Pointp if x p getx y p gety return1 elsereturn0 voidPoint operator Pointp x p getx y p gety voidPoint operator Pointp x p getx y p gety PointPoint operator Pointp this x p x this y p y return this PointPoint operator Pointp this x p x this y p y return this voidmain Pointp1 2 3 p2 3 4 p3 p2 cout 1 p3 disp p3 offset 10 10 cout 2 p3 disp cout 3 p2 p3 endl cout 4 p2 p3 endl p3 p1 cout 5 p3 disp p3 p2 cout 6 p3 disp p3 p1 p3 先将p1 p3的结果放在p1中 然后赋给p3cout 7 p3 disp p3 p1 p2 cout 8 p3 disp 返回本节 7 5特殊运算符重载 7 5 1赋值运算符重载7 5 2下标运算符重载7 5 3比较运算符重载7 5 4new与delete运算符重载7 5 5逗号运算符重载7 5 6类型转换运算符重载7 5 7 运算符重载7 5 8函数调用运算符重载7 5 9I O运算符重载 返回首页 7 5 1赋值运算符重载 1 运算符 和 的重载对于标准数据类型 和 的作用是将一个数据与另一个数据进行加法或减法运算后再将结果回送给赋值号左边的变量中 对它们重载后 使其实现其他相关的功能 2 运算符 的重载赋值运算符 的原有含义是将赋值号右边表达式的结果拷贝给赋值号左边的变量 通过运算符 的重载将赋值号右边对象的私有数据依次拷贝到赋值号左边对象的私有数据中 1 运算符 和 的重载 例7 9 和 运算符重载 includeclassVector intx y public Vector Vector intx1 inty1 x x1 y y1 friendVectoroperator Vectorv1 Vectorv2 v1 x v2 x v1 y v2 y returnv1 Vectoroperator Vectorv Vectortmp tmp x x v x tmp y y v y returntmp voiddisplay cout x y endl voidmain Vectorv1 6 8 v2 3 6 v3 v4 cout v1 v1 display cout v2 v2 display v3 v1 v2 cout v3 v3 display v4 v1 v2 cout v4 v4 display 此程序的运行结果为 v1 6 8 v2 3 6 v3 9 14 v4 3 2 2 运算符 的重载 图7 1对象内存分配 例7 10 重载运算符 includeclassSample intn public Sample Sample inti n i Sample Sample 此程序的运行结果为 n 10 返回本节 7 5 2下标运算符重载 下标运算符 通常用于在数组中标识数组元素的位置 下标运算符重载可以实现数组数据的赋值和取值 下标运算符重载函数只能作为类的成员函数 不能作为类的友元函数 下标运算符 函数重载的一般形式为 typeclass name operator arg 例7 11 下标运算符的重载 includeclassDemo intVector 5 public Demo int 此程序的运行结果为 12345 返回本节 7 5 3比较运算符重载 比较运算符 c voidmain Linea 3 b 4 c 5 if a b c 返回本节 7 5 4new与delete运算符重载 new和delete只能被重载为类的成员函数 不能重载为友元 而且 无论是否使用关键字static进行修饰 重载了的new和delete均为类的静态成员函数 运算符new重载的一般形式为 void class name operatornew size t new重载应返回一个无值型的指针 且至少有一个类型为size t的参数 若该重载带有多于一个的参数 则其第一个参数的类型必须为size t 运算符delete重载的一般形式为void class name operatordelete void 例7 13 重载new和delete运算符 include includeclassmemmanager public void operatornew size tsize 分配一块大小为size的内存void operatornew size tsize chartag 分配一块大小为size的内存 并且用字符tag赋值voidoperatordelete void p 释放指针p所指向的一块内存空间 void memmanager operatornew size tsize cout new1operator endl char s newchar size 分配大小为size的内存空间 s a 用字符 a 赋值returns 返回指针 void memmanager operatornew size tsize chartag cout new2operator endl char s newchar size s tag 用字符tag赋值returns 返回指针 voidmemmanager operatordelete void p cout deleteoperator endl char s char p 强制类型转换delete s 释放内存空间 voidmain memmanager m newmemmanager deletem memmanager m new B memmanager deletem 返回本节 7 5 5逗号运算符重载 逗号运算符是双目运算符 和其他运算符一样 也可以通过重载逗号运算符来完成期望完成的工作 逗号运算符构成的表达式为 左运算数 右运算数 该表达式返回右运算数的值 如果用类的成员函数来重载逗号运算符 则只带一个右运算数 而左运算数由指针this提供 例7 14 给出以下程序的执行结果 include includeclassPoint intx y public Point Point intl intw x l y w voiddisp cout 面积 x y endl Pointoperator Pointr Pointtemp temp x r x temp y r y returntemp Pointoperator Pointr Pointtemp temp x r x x temp y r y y returntemp voidmain Pointr1 1 2 r2 3 4 r3 5 6 r1 disp r2 disp r3 disp r1 r1 r2 r3 r3 r1 disp 此程序的运行结果为 面积 2面积 12面积 30面积 30 返回本节 7 5 6类型转换运算符重载 类型转换运算符重载函数的格式如下 operator 与以前的重载运算符函数不同的是 类型转换运算符重载函数没有返回类型 因为就代表了它的返回类型 而且也没有任何参数 在调用过程中要带一个对象实参 例7 15 使用转换函数 zhuanhuan hclassComplex public operatordouble returnReal zhuanhuan cpp include include zhuanhuan h voidmain Complexc 7 8 Cout c endl 此程序的运行结果为 7 返回本节 7 5 7 运算符重载 运算符是成员访问运算符 这种一元的运算符只能被重载为成员函数 所以也决定了它不能定义任何参数 一般成员访问运算符的典型用法是 对象 成员成员访问运算符 函数重载的一般形式为 typeclass name operator 例7 16 重载 运算符 includeclasspp public intn floatm pp operator returnthis voidmain ppt1 t1 m 10 coutkis mkis10 返回本节 7 5 8函数调用运算符重载 函数调用运算符 只能说明成类的非静态成员函数 该函数具有以下的一般形式 typeclass name operator 与普通函数一样 重载了的函数调用运算符可以事先带有零个或多个参数 但不得带有缺省的参数 例7 17 函数调用运算符重载示例 includeclassF public doubleoperator doublex doubley const doubleF operator doublex doubley const return x 5 y voidmain Ff cout f 1 5 2 2 endl 返回本节 7 5 9I O运算符重载 C 的I O流库的一个重要特性就是能够支持新的数据类型的输出和输入 用户可以通过对插入符 进行重载来支持新的数据类型 下面通过一个例子讲述重载插入符和提取符的方法 例7 18 分析下列程序 并给出执行结果 includeclassDate public Date inty intm intd Year y Month m Day d friendostream istream operator istream stream Date date stream date Year date Month date Day returnstream voidmain DateCdate 2004 1 1 cout Cdate cout Newdate Cdate endl 输出结果为 Currentdate 2004 1 1Enternewdate 2004115 Newdate 2004 1 15 返回本节
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!