C程序设计电子教案第7章运算符重载.ppt
《C程序设计电子教案第7章运算符重载.ppt》由会员分享,可在线阅读,更多相关《C程序设计电子教案第7章运算符重载.ppt(57页珍藏版)》请在三一办公上搜索。
1、第7章 运算符重载,7.1 运算符重载概述7.2 运算符重载的实现7.3 一元运算符重载7.4 二元运算符重载7.5 特殊运算符重载,7.1 运算符重载概述,运算符重载是对已有的运算符赋予多重含义,同一个运算符作用于不同类型的数据导致不同类型的行为。运算符重载的实质就是函数重载。在实现过程中,首先把指定的运算表达式转化为对运算符函数的调用,运算对象转化为运算符函数的实参,然后根据实参的类型来确定需要调用的函数,这个过程是在编译过程中完成的。,返回首页,表7-1 C+可以重载的运算符,表7-2 C+不能被重载的运算符,运算符重载的规则如下:,(1)C+中的运算符除了少数几个以外,全部可以重载,而
2、且只能重载已有的这些运算符。(2)重载之后运算符的优先级和结合性都不会改变。(3)运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造。,返回本节,7.2 运算符重载的实现,运算符的重载形式有两种:重载为类的成员函数和重载为类的友元函数。运算符重载为类的成员函数的语法形式如下:operator();运算符重载为类的友元函数的语法形式如下:friend operator();,返回首页,例7-1:以成员函数重载运算符重载两字符串加法。#include#include class String char name256;public:String(char*str)strcpy(nam
3、e,str);String()String()String operator+(const String,static char*str;String String:operator+(const String,String demo3=demo1+demo2;demo3.display();String demo4=demo3+Programming.;demo4.display();delete str;此程序的运行结果为:The string is:Visual C+The string is:6.0The string is:Visual C+6.0The string is:Visu
4、al C+Programming.,例7-2:下面程序定义一个Time类用来保存时间(时、分、秒),通过重载操作符“+”实现两个时间的相加。#include class Timepublic:Time()hours=0;minutes=0;seconds=0;/无参构造函数 Time(int h,int m,int s)/重载构造函数 hours=h;minutes=m;seconds=s;Time operator+(Time,Time Time:operator+(Time输出结果为:13:7:10,例7-4:下面程序修改了例7-2,将操作符重载为友元函数实现。#include class
5、 Timepublic:Time()hours=0;minutes=0;seconds=0;/无参构造函数 Time(int h,int m,int s)/重载构造函数 hours=h;minutes=m;seconds=s;friend Time operator+(Time,Time operator+(Time,void Time:gettime()couthours:minutes:secondsendl;void main()Time t1(8,51,40),t2(4,15,30),t3;t3=t1+t2;t3.gettime();输出结果为:13:7:10,返回本节,7.3 一元运
6、算符重载,类的单目运算符可重载为一个没有参数的非静态成员函数或者带有一个参数的非成员函数,参数必须是用户自定义类型的对象或者是对该对象的引用。在C+中,单目运算符有+和-,它们是变量自动增1和自动减1的运算符。在类中可以对这两个单目运算符进行重载。,返回首页,如同“+”运算符有前缀和后缀两种使用形式一样,“+”和“-”重载运算符也有前缀和后缀两种运算符重载形式,以“+”重载运算符为例,其语法格式如下:operator+();/前缀运算 operator+(int);/后缀运算使用前缀运算符的语法格式如下:+;使用后缀运算符的语法格式如下:+;,例7-5:成员函数重载示例。#include cl
7、ass Increasepublic:Increase(int x):value(x)Increase/再返回原对象,Increase Increase:operator+(int)Increase temp(*this);/临时对象存放原有对象值 value+;/原有对象增量修改 return temp;/返回原有对象值void main()Increase n(20);n.display();(n+).display();/显示临时对象值 n.display();/显示原有对象+n;n.display();+(+n);n.display();,(n+)+;/第二次增量操作对临时对象进行 n
8、.display();此程序的运行结果为:the value is 20the value is 20the value is 21the value is 22the value is 24the value is 25,例7-6:友元函数重载示例。#include class Increasepublic:Increase(int x):value(x)friend Increase,Increase,(n+).display();/显示临时对象值 n.display();/显示原有对象+n;n.display();+(+n);n.display();(n+)+;/第二次增量操作对临时对象
9、进行 n.display();此程序的运行结果为:the value is 20the value is 20the value is 21the value is 22the value is 24the value is 25,返回本节,7.4 二元运算符重载,对于双目运算符,一个运算数是对象本身的数据,由this指针给出,另一个运算数则需要通过运算符重载函数的参数表来传递。下面分别介绍这两种情况。对于双目运算符B,如果要重载B为类的成员函数,使之能够实现表达式“oprd1 B oprd2”,其中oprd1为A类的对象,则应当把B重载为A类的成员函数,该函数只有一个形参,形参的类型是opr
10、d2所属的类型。经过重载之后,表达式oprd1 B oprd2就相当于函数调用“oprd1.operator B(oprd2)”。,返回首页,例7-7:设计一个点类Point,实现点对象之间的各种运算。#include class Point int x,y;public:Point()x=y=0;Point(int i,int j)x=i;y=j;Point(Point/运算符重载,判断两个对象是否相同,bool operator!=(Point);/运算符重载,判断两个对象是否不相同void operator+=(Point);/运算符重载,将两个点对象相加void operator-=(
11、Point);/运算符重载,将两个点对象相减Point operator+(Point);/运算符重载,相加并将结果放在左操作数中Point operator-(Point);/运算符重载,相减并将结果放在左操作数中int getx()return x;int gety()return y;void disp()cout(x,y)endl;Point:Point(Point,void Point:offset(int i,int j)x+=i;y+=j;void Point:offset(Point p)x+=p.getx();y+=p.gety();bool Point:operator=(
12、Point p)if(x=p.getx(),bool Point:operator!=(Point p)if(x!=p.getx()|y!=p.gety()return 1;else return 0;void Point:operator+=(Point p)x+=p.getx();y+=p.gety();void Point:operator-=(Point p)x-=p.getx();y-=p.gety();Point Point:operator+(Point p)this-x+=p.x;this-y+=p.y;return*this;,Point Point:operator-(Po
13、int p)this-x-=p.x;this-y-=p.y;return*this;void main()Point p1(2,3),p2(3,4),p3(p2);cout1:;p3.disp();p3.offset(10,10);cout2:;p3.disp();cout3:(p2=p3)endl;cout4:(p2!=p3)endl;p3+=p1;cout5:;p3.disp();p3-=p2;,cout6:;p3.disp();p3=p1+p3;/先将p1+p3的结果放在p1中,然后赋给p3cout7:;p3.disp();p3=p1-p2;cout8:;p3.disp();,返回本节,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计 电子 教案 运算 重载
链接地址:https://www.31ppt.com/p-5373706.html