C程序设计电子运算符重载_第1页
C程序设计电子运算符重载_第2页
C程序设计电子运算符重载_第3页
C程序设计电子运算符重载_第4页
C程序设计电子运算符重载_第5页
已阅读5页,还剩51页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

会计学1C程序设计电子运算符重载表7-1C++可以重载的运算符第1页/共56页表7-2C++不能被重载的运算符第2页/共56页运算符重载的规则如下:

(1)C++中的运算符除了少数几个以外,全部可以重载,而且只能重载已有的这些运算符。(2)重载之后运算符的优先级和结合性都不会改变。(3)运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造。

返回本节第3页/共56页7.2运算符重载的实现运算符的重载形式有两种:重载为类的成员函数和重载为类的友元函数。运算符重载为类的成员函数的语法形式如下:<函数类型>operator<运算符>(<形参表>){ <函数体>;}运算符重载为类的友元函数的语法形式如下:friend<函数类型>operator<运算符>(<形参表>){ <函数体>;}返回首页第4页/共56页例7-1:以成员函数重载运算符重载两字符串加法。#include<iostream.h>#include<string.h>classString{ charname[256];public: String(char*str) { strcpy(name,str);} String(){} ~String(){} Stringoperator+(constString&); voiddisplay() { cout<<"Thestringis:"<<name<<endl;}};第5页/共56页staticchar*str;StringString::operator+(constString&a){ strcpy(str,name); strcat(str,); returnString(str);}voidmain(){ str=newchar[256]; Stringdemo1("Visualc++"); Stringdemo2("6.0"); demo1.display(); demo2.display();第6页/共56页Stringdemo3=demo1+demo2; demo3.display(); Stringdemo4=demo3+"Programming."; demo4.display(); deletestr;}此程序的运行结果为:Thestringis:VisualC++Thestringis:6.0Thestringis:VisualC++6.0Thestringis:VisualC++Programming.第7页/共56页例7-2:下面程序定义一个Time类用来保存时间(时、分、秒),通过重载操作符“+”实现两个时间的相加。#include<iostream.h>classTime{ public: Time(){hours=0;minutes=0;seconds=0;}//无参构造函数

Time(inth,intm,ints)//重载构造函数 {

hours=h;minutes=m;seconds=s; } Timeoperator+(Time&);//操作符重载为成员函数,返回结果为Time类

voidgettime(); private: inthours,minutes,seconds;};第8页/共56页TimeTime::operator+(Time&time){ inth,m,s; s=time.seconds+seconds; m=time.minutes+minutes+s/60; h=time.hours+hours+m/60; Timeresult(h,m%60,s%60); returnresult;}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第9页/共56页例7-4:下面程序修改了例7-2,将操作符重载为友元函数实现。

#include<iostream.h>classTime{ public: Time(){hours=0;minutes=0;seconds=0;}//无参构造函数

Time(inth,intm,ints)//重载构造函数 {

hours=h;minutes=m;seconds=s; } friendTimeoperator+(Time&,Time&);//重载运算符为友元函数形式

voidgettime(); private: inthours,minutes,seconds;};第10页/共56页Timeoperator+(Time&time1,Time&time2){ inth,m,s; s=time1.seconds+time2.seconds; //计算秒数

m=time1.minutes+time2.minutes+s/60; //计算分数

h=time1.hours+time2.hours+m/60; //计算小时数

Timeresult(h,m%60,s%60); returnresult;}第11页/共56页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返回本节第12页/共56页7.3一元运算符重载类的单目运算符可重载为一个没有参数的非静态成员函数或者带有一个参数的非成员函数,参数必须是用户自定义类型的对象或者是对该对象的引用。在C++中,单目运算符有++和--,它们是变量自动增1和自动减1的运算符。在类中可以对这两个单目运算符进行重载。

返回首页第13页/共56页如同“++”运算符有前缀和后缀两种使用形式一样,“++”和“--”重载运算符也有前缀和后缀两种运算符重载形式,以“++”重载运算符为例,其语法格式如下:<函数类型>operator++(); //前缀运算<函数类型>operator++(int); //后缀运算使用前缀运算符的语法格式如下:++<对象>;使用后缀运算符的语法格式如下:<对象>++;第14页/共56页例7-5:成员函数重载示例。#include<iostream.h>classIncrease{public: Increase(intx):value(x){} Increase&operator++(); //前增量

Increaseoperator++(int); //后增量

voiddisplay(){cout<<"thevalueis"<<value<<endl;}private: intvalue;};Increase&Increase::operator++(){ value++; //先增量

return*this; //再返回原对象第15页/共56页}IncreaseIncrease::operator++(int){ Increasetemp(*this); //临时对象存放原有对象值

value++; //原有对象增量修改

returntemp; //返回原有对象值}voidmain(){ Increasen(20); n.display(); (n++).display(); //显示临时对象值

n.display(); //显示原有对象 ++n; n.display(); ++(++n); n.display();第16页/共56页(n++)++; //第二次增量操作对临时对象进行

n.display();}此程序的运行结果为:thevalueis20thevalueis20thevalueis21thevalueis22thevalueis24thevalueis25第17页/共56页例7-6:友元函数重载示例。#include<iostream.h>classIncrease{public: Increase(intx):value(x){} friendIncrease&operator++(Increase&); //前增量

friendIncreaseoperator++(Increase&,int); //后增量

voiddisplay(){cout<<"thevalueis"<<value<<endl;}private: intvalue;};第18页/共56页Increase&operator++(Increase&a){ a.value++; //前增量

returna; //再返回原对象}Increaseoperator++(Increase&a,int){ Increasetemp(a); //通过拷贝构造函数保存原有对象值

a.value++; //原有对象增量修改

returntemp; //返回原有对象值}voidmain(){ Increasen(20); n.display();第19页/共56页(n++).display(); //显示临时对象值

n.display(); //显示原有对象 ++n; n.display(); ++(++n); n.display(); (n++)++; //第二次增量操作对临时对象进行

n.display();}此程序的运行结果为:thevalueis20thevalueis20thevalueis21thevalueis22thevalueis24thevalueis25返回本节第20页/共56页7.4二元运算符重载对于双目运算符,一个运算数是对象本身的数据,由this指针给出,另一个运算数则需要通过运算符重载函数的参数表来传递。下面分别介绍这两种情况。对于双目运算符B,如果要重载B为类的成员函数,使之能够实现表达式“oprd1Boprd2”,其中oprd1为A类的对象,则应当把B重载为A类的成员函数,该函数只有一个形参,形参的类型是oprd2所属的类型。经过重载之后,表达式oprd1Boprd2就相当于函数调用“oprd1.operatorB(oprd2)”。返回首页第21页/共56页例7-7:设计一个点类Point,实现点对象之间的各种运算。#include<iostream.h>classPoint{ intx,y;public: Point(){x=y=0; Point(inti,intj){x=i;y=j;} Point(Point&); ~Point(){} voidoffset(int,int); //提供对点的偏移

voidoffset(Point); //重载,偏移量用Point类对象表示

booloperator==(Point); //运算符重载,判断两个对象是否相同第22页/共56页booloperator!=(Point); //运算符重载,判断两个对象是否不相同

voidoperator+=(Point); //运算符重载,将两个点对象相加

voidoperator-=(Point); //运算符重载,将两个点对象相减

Pointoperator+(Point); //运算符重载,相加并将结果放在左操作数中

Pointoperator-(Point); //运算符重载,相减并将结果放在左操作数中

intgetx(){returnx;} intgety(){returny;} voiddisp() { cout<<"("<<x<<","<<y<<")"<<endl; }};Point::Point(Point&p){ x=p.x;y=p.y;第23页/共56页}voidPoint::offset(inti,intj){ x+=i;y+=j;}voidPoint::offset(Pointp){ x+=p.getx();y+=p.gety();}boolPoint::operator==(Pointp){ if(x==p.getx()&&y==p.gety()) return1; else return0;}第24页/共56页boolPoint::operator!=(Pointp){ if(x!=p.getx()||y!=p.gety() return1; else return0;}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;}第25页/共56页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;第26页/共56页cout<<"6:"; p3.disp(); p3=p1+p3; //先将p1+p3的结果放在p1中,然后赋给p3 cout<<"7:"; p3.disp(); p3=p1-p2; cout<<"8:"; p3.disp();}返回本节第27页/共56页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运算符重载

返回首页第28页/共56页7.5.1赋值运算符重载1.运算符“+=”和“-=”的重载对于标准数据类型,“+=”和“-=”的作用是将一个数据与另一个数据进行加法或减法运算后再将结果回送给赋值号左边的变量中。对它们重载后,使其实现其他相关的功能。2.运算符“=”的重载赋值运算符“=”的原有含义是将赋值号右边表达式的结果拷贝给赋值号左边的变量,通过运算符“=”的重载将赋值号右边对象的私有数据依次拷贝到赋值号左边对象的私有数据中。

第29页/共56页1.运算符“+=”和“-=”的重载例7-9:“+=”和“-=”运算符重载。

#include<iostream.h>classVector{ 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) {第30页/共56页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=”;第31页/共56页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)第32页/共56页2.运算符“=”的重载图7-1对象内存分配第33页/共56页例7-10:重载运算符“=”。#include<iostream.h>classSample{ intn;public: Sample(){} Sample(inti){n=i;} Sample&operator=(Sample); Voiddisp(){cout<<”n=”<<n<<endl;}};第34页/共56页Sample&Sample::operator=(Samples){ Sample::n=s.n; return*this;}voidmain(){ Samples1(10),s2; s2=s1; s2.disp();}此程序的运行结果为:n=10返回本节第35页/共56页7.5.2下标运算符重载下标运算符“[]”通常用于在数组中标识数组元素的位置,下标运算符重载可以实现数组数据的赋值和取值。下标运算符重载函数只能作为类的成员函数,不能作为类的友元函数。下标运算符“[]”函数重载的一般形式为:typeclass_name::operator[](arg_);第36页/共56页例7-11:下标运算符的重载。#include<iostream.h>classDemo{ intVector[5];public: Demo(){}; int&operator[](inti)(returnVector[i];)};voidmain(){ Demov; for(inti=0;i<5;i++) v[i]=i+1; for(i=0;i<5;i++) cout<<v[i]<<""; cout<<endl;}此程序的运行结果为:12345返回本节第37页/共56页7.5.3比较运算符重载比较运算符(>、<、==等)重载必须返回真(非0)或假(0)。例7-12:编写一个程序测试输入的长度能否构成一个三角形。};voidmain(){ Linea(3),b(4),c(5); if(a+b>c&&a+c>b&&b+c>a) cout<<"能够构成三角形!"<<endl; else cout<<"不能构成三角形!"<<endl;}第38页/共56页};voidmain(){ Linea(3),b(4),c(5); if(a+b>c&&a+c>b&&b+c>a) cout<<"能够构成三角形!"<<endl; else cout<<"不能构成三角形!"<<endl;}返回本节第39页/共56页7.5.4new与delete运算符重载new和delete只能被重载为类的成员函数,不能重载为友元。而且,无论是否使用关键字static进行修饰,重载了的new和delete均为类的静态成员函数。运算符new重载的一般形式为:void*class_name::operatornew(size_t,<arg_list>);new重载应返回一个无值型的指针,且至少有一个类型为size_t的参数。若该重载带有多于一个的参数,则其第一个参数的类型必须为size_t。运算符delete重载的一般形式为void*class_name::operatordelete(void*,<size_t>);第40页/共56页例7-13:重载new和delete运算符。

#include<iostream.h>#include<stddef.h>classmemmanager{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; //返回指针}第41页/共56页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;返回本节第42页/共56页7.5.5逗号运算符重载逗号运算符是双目运算符,和其他运算符一样,也可以通过重载逗号运算符来完成期望完成的工作。逗号运算符构成的表达式为“左运算数,右运算数”,该表达式返回右运算数的值。如果用类的成员函数来重载逗号运算符,则只带一个右运算数,而左运算数由指针this提供。

第43页/共56页例7-14:给出以下程序的执行结果。#include<iostream.h>#include<malloc.h>classPoint{ intx,y;public: Point(){}; Point(intl,intw) { x=l;y=w; } voiddisp() { cout<<"面积:"<<x*y<<endl; }第44页/共56页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; }};第45页/共56页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返回本节第46页/共56页7.5.6类型转换运算符重载类型转换运算符重载函数的格式如下:operator<类型名>(){ <函数体>;}与以前的重载运算符函数不同的是,类型转换运算符重载函数没有返回类型,因为<类型名>就代表了它的返回类型,而且也没有任何参数。在调用过程中要带一个对象实参。

第47页/共56页例7-15:使用转换函数。//zhuanhuan.hclassComplex{public: operatordouble(){returnReal;}};//zhuanhuan.cpp#include<iostream.h>#include"zhuanhuan.h"voidmain(){ Complexc(7,8); Cout<<c<<endl;}此程序的运行结果为:7返回本节第48页/共56页7.5.7->运算符重载“->”运算符是成员访问运算符,这种一元的运算符只能被重载为成员函数,所以也决定了它不能定义任何参数。一般成员访问运算符的典型用法是:对象->成员成员访问运算符“->”函数重载的一般形式为:typeclass_name::operator->();第49页/共56页例7-16:重载->运算符。#include<iostream.h>classpp{public: intn; floatm; pp*operator->() { returnthis; }};voidmain(){ ppt1; t1->m=10; cout<<"t1.kis:"<<t1.m<<endl; cout<<"t1-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论