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

上传人:本田雅阁 文档编号:2152551 上传时间:2019-02-23 格式:PPT 页数:57 大小:224.51KB
返回 下载 相关 举报
《C程序设计》电子教案第7章运算符重载.ppt_第1页
第1页 / 共57页
《C程序设计》电子教案第7章运算符重载.ppt_第2页
第2页 / 共57页
《C程序设计》电子教案第7章运算符重载.ppt_第3页
第3页 / 共57页
亲,该文档总共57页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《《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(

3、char* str) strcpy(name,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.0 The

4、string is : Visual C+ 6.0 The string is : Visual C+ Programming.,例7-2:下面程序定义一个Time类用来保存时间(时、分、秒),通过重载操作符“+”实现两个时间的相加。 #include class Time public: 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

5、 输出结果为: 13:7:10,例7-4:下面程序修改了例7-2,将操作符重载为友元函数实现。 #include class Time public: 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(

6、 ) Time t1(8,51,40),t2(4,15,30),t3; t3=t1+t2; t3.gettime( ); 输出结果为: 13:7:10,返回本节,7.3 一元运算符重载,类的单目运算符可重载为一个没有参数的非静态成员函数或者带有一个参数的非成员函数,参数必须是用户自定义类型的对象或者是对该对象的引用。 在C+中,单目运算符有+和-,它们是变量自动增1和自动减1的运算符。在类中可以对这两个单目运算符进行重载。,返回首页,如同“+”运算符有前缀和后缀两种使用形式一样,“+”和“-”重载运算符也有前缀和后缀两种运算符重载形式,以“+”重载运算符为例,其语法格式如下: operator

7、 +(); /前缀运算 operator +(int); /后缀运算 使用前缀运算符的语法格式如下: +; 使用后缀运算符的语法格式如下: +;,例7-5:成员函数重载示例。 #include class Increase public: Increase(int x):value(x) Increase /再返回原对象, Increase Increase:operator+(int) Increase temp(*this); /临时对象存放原有对象值 value+; /原有对象增量修改 return temp; /返回原有对象值 void main() Increase n(20); n

8、.display(); (n+).display(); /显示临时对象值 n.display(); /显示原有对象 +n; n.display(); +(+n); n.display();,(n+)+; /第二次增量操作对临时对象进行 n.display(); 此程序的运行结果为: the value is 20 the value is 20 the value is 21 the value is 22 the value is 24 the value is 25,例7-6:友元函数重载示例。 #include class Increase public: Increase(int x)

9、:value(x) friend Increase ,Increase ,(n+).display(); /显示临时对象值 n.display(); /显示原有对象 +n; n.display(); +(+n); n.display(); (n+)+; /第二次增量操作对临时对象进行 n.display(); 此程序的运行结果为: the value is 20 the value is 20 the value is 21 the value is 22 the value is 24 the value is 25,返回本节,7.4 二元运算符重载,对于双目运算符,一个运算数是对象本身的数

10、据,由this指针给出,另一个运算数则需要通过运算符重载函数的参数表来传递。下面分别介绍这两种情况。 对于双目运算符B,如果要重载B为类的成员函数,使之能够实现表达式“oprd1 B oprd2”,其中oprd1为A类的对象,则应当把B重载为A类的成员函数,该函数只有一个形参,形参的类型是oprd2所属的类型。经过重载之后,表达式oprd1 B oprd2就相当于函数调用“oprd1.operator B(oprd2)”。,返回首页,例7-7:设计一个点类Point,实现点对象之间的各种运算。 #include class Point int x,y; public: Point() x=y=

11、0; Point(int i,int j) x=i;y=j; Point(Point /运算符重载,判断两个对象是否相同,bool operator! =(Point); /运算符重载,判断两个对象是否不相同 void operator+ =(Point); /运算符重载,将两个点对象相加 void operator- =(Point); /运算符重载,将两个点对象相减 Point operator+(Point); /运算符重载,相加并将结果放在左操作数中 Point operator-(Point); /运算符重载,相减并将结果放在左操作数中 int getx() return x; in

12、t 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=(Point p) if(x=p.getx() ,bool Point:operator!=(Point p) if(x!=p.getx() |y!=p.gety() return 1; else return 0; voi

13、d 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-(Point p) this-x-=p.x; this-y-=p.y; return *this; void main() Point p1(2,3),p2(3,4),p3(p2); cout“1

14、:“; 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中,然后赋给p3 cout“7:“; p3.disp(); p3=p1-p2; cout“8:“; p3.disp(); ,返回本节,7.5 特殊运算符重载,7.5.1 赋值运算符重载 7.5.2 下标运算符重载 7.5.3 比较运算符

15、重载 7.5.4 new与delete运算符重载 7.5.5 逗号运算符重载 7.5.6 类型转换运算符重载 7.5.7 -运算符重载 7.5.8 函数调用运算符重载 7.5.9 I/O运算符重载,返回首页,7.5.1 赋值运算符重载,1运算符“+=”和“-=”的重载 对于标准数据类型,“+=”和“-=”的作用是将一个数据与另一个数据进行加法或减法运算后再将结果回送给赋值号左边的变量中。对它们重载后,使其实现其他相关的功能。 2运算符“=”的重载 赋值运算符“=”的原有含义是将赋值号右边表达式的结果拷贝给赋值号左边的变量,通过运算符“=”的重载将赋值号右边对象的私有数据依次拷贝到赋值号左边对象

16、的私有数据中。,1运算符“+=”和“-=”的重载,例7-9:“+=”和“-=”运算符重载。 #include class Vector int x,y; public: Vector() ; Vector(int x1,int y1) x=x1;y=y1; friend Vector operator +=(Vector v1,Vector v2) v1.x+=v2.x; v1.y+=v2.y; return v1; Vector operator-=(Vector v) ,Vector tmp; tmp.x=x-v.x; tmp.y=y-v.y; return tmp; void displ

17、ay() cout”(“x”,”y”)”endl; ; void main() Vector v1(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:重载运算符“=”。 #include class Sample

18、 int n; public: Sample() Sample(int i) n=i; Sample ,Sample 此程序的运行结果为: n=10,返回本节,7.5.2 下标运算符重载,下标运算符“ ”通常用于在数组中标识数组元素的位置,下标运算符重载可以实现数组数据的赋值和取值。下标运算符重载函数只能作为类的成员函数,不能作为类的友元函数。 下标运算符“ ”函数重载的一般形式为: type class_name:operator (arg_);,例7-11:下标运算符的重载。 #include class Demo int Vector5; public: Demo() ; int 此程序

19、的运行结果为: 1 2 3 4 5,返回本节,7.5.3 比较运算符重载,比较运算符(、c ,; void main() Line a(3),b(4),c(5); if(a+bc ,返回本节,7.5.4 new与delete运算符重载,new和delete只能被重载为类的成员函数,不能重载为友元。而且,无论是否使用关键字static进行修饰,重载了的new和delete均为类的静态成员函数。 运算符new重载的一般形式为: void *class_name:operator new(size_t,); new重载应返回一个无值型的指针,且至少有一个类型为size_t的参数。若该重载带有多于一个

20、的参数,则其第一个参数的类型必须为size_t。 运算符delete重载的一般形式为 void *class_name:operator delete(void *,);,例7-13:重载new和delete运算符。 #include #include class memmanager public: void *operator new(size_t size); /分配一块大小为size的内存 void *operator new(size_t size,char tag); /分配一块大小为size的内存,并且用字符tag赋值 void operator delete(void *p);

21、 /释放指针p所指向的一块内存空间 ; void *memmanager:operator new(size_t size) cout“new1 operator“endl; char *s=new charsize; /分配大小为size的内存空间 *s=a; /用字符a赋值 return s; /返回指针 ,void *memmanager:operator new(size_t size,char tag) cout“new2 operator“endl; char *s=new charsize; *s=tag; /用字符tag赋值 return s; /返回指针 void memma

22、nager:operator delete(void *p) cout“delete operator“endl; char *s=(char *)p; /强制类型转换 delete s; /释放内存空间 void main() memmanager *m=new memmanager(); delete m; memmanager *m=new(B) memmanager(); delete m;,返回本节,7.5.5 逗号运算符重载,逗号运算符是双目运算符,和其他运算符一样,也可以通过重载逗号运算符来完成期望完成的工作。逗号运算符构成的表达式为“左运算数,右运算数”,该表达式返回右运算数的

23、值。如果用类的成员函数来重载逗号运算符,则只带一个右运算数,而左运算数由指针this提供。,例7-14:给出以下程序的执行结果。 #include #include class Point int x,y; public: Point() ; Point(int l,int w) x=l;y=w; void disp() cout“面积:“ x*yendl; ,Point operator,(Point r) Point temp; temp.x=r.x; temp.y=r.y; return temp; Point operator+(Point r) Point temp; temp.x=

24、r.x+x; temp.y=r.y+y; return temp; ;,void main() Point r1(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 () ; 与以前的重载运算符函数不同的是,类型转换运算符重载函数没有返回类型,因为就代表了它的返回类型,而且也没有任何参数。在调用过程中要带一个对象实参。

25、,例7-15:使用转换函数。 /zhuanhuan.h class Complex public: operator double() return Real; ; /zhuanhuan.cpp #include #include “zhuanhuan.h“ void main() Complex c(7,8); Coutcendl; 此程序的运行结果为: 7,返回本节,7.5.7 -运算符重载,“-”运算符是成员访问运算符,这种一元的运算符只能被重载为成员函数,所以也决定了它不能定义任何参数。一般成员访问运算符的典型用法是: 对象-成员 成员访问运算符“-”函数重载的一般形式为: type

26、class_name:operator-();,例7-16:重载-运算符。 #include class pp public: int n; float m; pp *operator-() return this; ; void main() pp t1; t1-m=10; coutk is“mk is 10,返回本节,7.5.8 函数调用运算符重载,函数调用运算符“()”只能说明成类的非静态成员函数,该函数具有以下的一般形式: type class_name:operator()(); 与普通函数一样,重载了的函数调用运算符可以事先带有零个或多个参数,但不得带有缺省的参数。,例7-17:函

27、数调用运算符重载示例。 #include class F public: double operator ()(double x, double y) const; ; double F:operator ()(double x, double y) const return (x+5)*y; void main() F f; coutf(1.5, 2.2)endl; ,返回本节,7.5.9 I/O运算符重载,C+的I/O流库的一个重要特性就是能够支持新的数据类型的输出和输入。用户可以通过对插入符()进行重载来支持新的数据类型。 下面通过一个例子讲述重载插入符和提取符的方法。,例7-18:分析

28、下列程序,并给出执行结果。 #include class Date public: Date(int y,int m,int d) Year=y;Month=m;Day=d; friend ostream istream& operator(istream &stream,Date &date), streamdate.Yeardate.Monthdate.Day; return stream; void main() Date Cdate(2004,1,1); coutCdate; cout“New date:“Cdateendl; 输出结果为: Current date:2004/1/1 Enter new date:2004 1 15 New date:2004/1/15,返回本节,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1