




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第八讲特殊成员与友元拷贝构造函数this指针静态成员友元运算符重载.拷贝构造函数 构造函数完成对像成员的一个一个拷贝,它可以认为是构造函数的一个重载。 在C++中,如果用户没有提供拷贝构造函数,系统会自动提供一个默认的拷贝构造函数。.classroom{private: intm;public: room(inti) { m=i; } ……};1010r1.mr2.mroomr1(10);roomr2=r1;.classroom{private: int*p;public: room() { p=newint; } ~room() { deletep; } }{ …… roomr1; roomr2=r1; ……}r1.pr2.p.为这个类添加一个拷贝构造函数classroom{private: int*p; intm;public: room(inti):m(i){p=newint;} ~room(){ deletep;} room::room(room&r) { m=r.m; p=newint; }}{ …… roomr1; roomr2=r1; ……}r1.pr2.p.this指针 this指针隐含于每一个类的成员函数中(静态成员函数除外),它指向调用该成员函数的对象。.#include<iostream.h>#include<string.h>classroom{private: intm;public: room(inti,intj); view() { cout<<"m="<<m<<endl; cout<<"m="<<this->m<<endl; }};通过this指针引导本对象的成员.静态成员静态数据成员静态函数成员.静态数据成员 将数据成员声明为静态的,将使所有的同类对象都拥有同一份静态成员,该静态成员遵守数据封装约定。静态成员对象1对象2对象n…….classroom{private: intm; public:
staticintn; room(intv1):m(v1) { n++; } voidview() { cout<<"m="<<m<<endl; cout<<"n="<<n<<endl; } ~room(){};};introom::n=0;所有对象共同拥有一份静态成员必须是全局范围的类从程序一开始运行就存在,与构造函数和析构函数无关只能在类的外部定义,定义时可以初始化.voidmain(){ roomr1(10); roomr2(20); roomr3(30); cout<<"inclassroomn=“ <<room::n<<endl;}inclassroomn=3通过任何一个对象都可以对静态成员进行改动静态成员可以通过类名和对象名引用.静态函数成员 静态成员函数也不与对象相联系,只与类联系,为所有对象所共享,可用类名和对象名引导。
它不能引用非静态成员,也不能引用this指针。.classroom{private: intm;public:
staticintn; room(inti):m(i) { n++; }
staticvoidview()//静态成员函数 { m++; //error这里不能引用非静态成员 cout<<"n="<<n<<endl; }};introom::n=0;.静态成员的应用//在学生对像链表中寻找某个学生。classstudent{private: charname[40]; intnumber;
staticintcount; staticstudent*pfirst; student*pnext;public: student(char*); ~student();
staticvoidfindname(char*); staticvoidview();};计数器链表下一个结点链表结点查找链表遍历链表头指针.intstudent::count=0;student*student::pfirst=NULL;student::student(char*pname){ strcpy(name,pname); pnext=pfirst; pfirst=this; count++; number=count;}*thisnextpfirstpfirst.student::~student(){
if(pfirst==this) { pfirst=pnext; count--; return; } for(student*ps=pfirst;ps;ps=ps->pnext) if(ps->pnext==this) { ps->pnext=pnext; count--; return; }}判断pfirst是否指向当前对象.voidstudent::findname(char*pname)//在链表中查找某个关键结点{ for(student*ps=pfirst;ps;ps=ps->pnext) if(strcmp(ps->name,pname)==0) { cout<<"findit,itisnumber“ <<ps->number<<endl; return; } cout<<"nofind"<<endl; return;}.voidstudent::view()//对链表进行遍历{ for(student*pv=pfirst;pv;pv=pv->pnext) cout<<pv->name<<endl;}voidmain(){ students1("s1"); students2("s2"); students3("s3"); student::view(); charname[40]; cout<<"pleaseputinthenameof whichyouwanttofind"<<endl; cin>>name; student::findname(name);}.友元 类的友元可以直接访问类非公有成员。它在被访问类中以关键字friend声明。友元函数友元成员函数友类.友元函数 当一个普通函数能直接访问类非公有成员时,称它为类的友元函数。.classrect;classpoint{private: intx; inty;public: point(intxx=0,intyy=0):x(xx),y(yy){};
friendintinrect(constpoint&,constrect&);};声明类声明友元函数.classrect{private: pointp1; pointp2;public: rect(intx1,inty1,intx2,inty2) :p1(x1,y1),p2(x2,y2){};
friendintinrect(constpoint&,constrect&);};声明友元函数.intinrect(constpoint&p,constrect&r){ if((p.x<=r.p1.x)&&(p.x>=r.p2.x) &&(p.y<=r.p1.y)&&(p.y>=r.p2.y)) return1; if((p.x>=r.p1.x)&&(p.x<=r.p2.x) &&(p.y>=r.p1.y)&&(p.y<=r.p2.y)) return1; return0;}友元函数内可以访问类的非公有成员.友元成员函数 当一个类的某个成员函数被宣布为另一个类的友元时,在这个成员函数内就可以访问另一个类的非公有数据。.classstudent;classteacher{public: voidteacher::setscore(student&s,inti) { s.score=i; }};classstudent{private: intscore;public:
friendvoidteacher::give(student&s,inti); ……};……students1; teachert1;t1.give(s1,90);.友类 当类A宣布为类B的友类时,类A的所有成员函数都可以访问类B的非公有成员。 classB { ……
friendclassA; …… }.运算符重载非成员函数的运算符函数作为成员函数的运算符函数增量运算符转换运算符赋值运算符.非成员函数的运算符函数classstudent{private: intn;public: student(inti=10):n(i){};
friendstudentoperator+(student&,student&); voidview();};.studentoperator+(student&s1,student&s2){ students3; s3.n=s1.n+s2.n; returns3;}……stu3=stu1+stu2; .作为成员函数的运算符函数classstudent{private: intn;public: student(inti=10):n(i){};
studentoperator+(student&); voidview();};.studentstudent::operator+(student&s2){
students3; s3.n=n+s2.n;
returns3;}
studentstu1(20);studentstu2(30);studentstu3;stu3=stu1+stu2; inthisobject,n=20观察是哪个对象调用了这个函数cout<<"inthisobject,n="<<n<<endl;.增量运算符 增量运算符有前增量与后增量之分,在定义这两个运算符时,函数的返回类型,运算符形式,参数表均相同,为了以示区别,规定,在后增量函数的参数表中再加一个int型参数。.classstudent{private: intn;public: student(inti=10):n(i){}; friendstudentoperator++(student&); friendstudentoperator++(student&,int); voidview();};前增量后增量.studentoperator++(student&s){ s.n++;
returns;}studentoperator++(student&s,int){ studenttemp; temp=s; s.n++;
returntemp;}studentstu1;stu2=stu1++;stu3=++stu1;stu2.n=10stu3.n=12.增量运算函数作成员函数classstudent{private: intn;public: student(inti=10):n(i){};
studentoperator++(); studentoperator++(int); voidview();};.studentstudent::operator++(){ n++;
return(*this);}studentstudent::operator++(int){ studenttemp; temp=(*this); n++;
returntemp;}studentstu1;studentstu2;stu2=++(++stu1);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 唐代宫廷胡乐兴衰史论
- 人教版九年级上学期语文教学计划评估与反馈
- 普通话水平测试30个工作话题及范文示例
- 离职员工资产清查协议
- 人教版英语教学计划的创新思路
- 教育培训行业招商流程框架
- 家庭和学校合作的教师培训心得体会
- 汽车售后服务质量协议
- 冷库业务转让协议
- 2025至2030年中线伸线机项目投资价值分析报告
- 水库维修养护实施方案
- 2025中国农业银行个人房屋按揭贷款合同
- 装修拆除工程施工方案
- MOOC 批判性思维-南京林业大学 中国大学慕课答案
- 【北师大版】六年级下册数学教学设计-总复习《数的认识》(1)
- 中医护理原则和方法
- 机房专用精密空调巡检维护
- 换流站控制保护软件Accel简介
- 动、静平衡原理及平衡方法
- 《家庭养花技术》PPT课件.ppt
- 水泥企业职业病防治责任制
评论
0/150
提交评论