面向对象实验报告_第1页
面向对象实验报告_第2页
面向对象实验报告_第3页
面向对象实验报告_第4页
面向对象实验报告_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

实验报告姓名:班级:学号:课程:C++面向对象程序设计一、实验题目:继承和派生一、实验地点:实验楼A201三、实验目的:了解重载运算符函数的定义格式。了解并掌握重载运算符函数的调用格式。了解并掌握重载单目、双目、赋值、比较等运算符的设计方法。四、实验内容:1•设计一个学生类Student,包括姓名和四门课程成绩,利用重载运算符“+”将所有学生的成绩相加放在一个对象中,再对该对象求各门课程的平均分。2•设计一个时间类Time,包括时、分、秒等私有数据成员。要求实现时间的基本运算,如一时间加上另一时间、一时间减去另一时间等。五、实验中遇到的问题及解决办法:由于是刚接触重载运算符,所以对于重载运算符的应用还是很陌生,实验起初甚至还不知道应如何构建重载运算符;实验中会把运算符的有/无副作用混淆起来,误用const,导致对成员函数的定义出现错误。幸而在及时查看课本之后得以解决;对于重载运算符中this的应用还不熟,经常出错,但在冋学的帮助下得以解决;实验的第一题对我来说,是本次实验的难点;因为,时间的计算需要考虑到hour、minute和second的进位和借位,所以在计算时间的相加减的时候相对而言是非常棘手的。好在及时请教同学,在同学的帮助下顺利的解决了问题,完成了本次实验。六、实验结论和感想:(附实验程序和结果图)1.#includeviostream>usingnamespacestd;classStudent{public:Student(char*pName="",floatscorel=0,floatscore2=0,floatscore3=0,floatscore4=0){strcpy(n_name,pName);n_score1=score1;n_score2=score2;n_score3=score3;n_score4=score4;n_count=1;}Student&operator+(constStudent&stu1){this->n_score1+=stu1.n_score1;this->n_score2+=stu1.n_score2;this->n_score3+=stu1.n_score3;this->n_score4+=stu1.n_score4;n_count++;return*this;}floatGetAverage1(){returnn_score1/n_count;}floatGetAverage2(){returnn_score2/n_count;}floatGetAverage3(){returnnscore3/ncount;

}floatGetAverage4(){returnn_score4/n_count;}private:charn_name[20];floatn_scorel;floatn_score2;floatn_score3;floatn_score4;intn_count; //学生人数计数};intmain(){Students1("chenzhi",100,34.5,60,80.5);Students2("liwen",89,90.5,78,93);Students3("zhangyu",97,94.5,77,88);Students4("zhaobin",95,93.5,80,85);Students=sl+s2+s3+s4;cout<<"课程1的平均分="vvs.GetAverage1()<<endl;cout<<"课程2的平均分="vvs.GetAverage2()<<endl;cout<<"课程3的平均分="vvs.GetAverage3()<<endl;cout<<"课程4的平均分="vvs.GetAverage4()<<endl;return0;}实验结果:'H\e]向炳叢\宾验五&udent\DebugV<s.exe'均均均均平平平评i^E^S,ki-1234均均均均平平平评i^E^S,ki-1234b匸n吾王呈E-耒Jr.srK.-I1:1^7.arK.-Iy

na££eF.PJ95.2578.2573.75S6.625tocontinuewg.exe-Serrar(s),uarning(s)2.#includeviostream>usingnamespacestd;classTime{inthour;intminute;intsecond;public:Time(inth=0,intm=0,ints=0):hour(h),minute(m),second(s){}voidsetHour(inth){hour=h;}voidsetMinute(intm){minute=m;}voidsetSecond(ints){second=s;}Timeoperator+(constTime&t)const;Timeoperator-(constTime&t)const;Timeoperator=(constTime&t);Timeoperator+=(constTime&t);Timeoperator-=(constTime&t);friendostream&operatorvv(ostream&out,constTime&t){outvvt.hourvv":"vvt.minutevv":"vvt.secondvvendl;returnout;}};TimeTime::operator+(constTime&t)const{intans=0;Timetemp;temp.second=this->second+t.second;if(temp.second>=60){temp.second=temp.minute-60;ans=1;}temp.minute=this->minute+t.minute+ans;ans=0;if(temp.minute>=60){temp.minute=temp.minute+ans;ans=1;}temp.hour=this->hour+t.hour+ans;if(temp.hour>=24)temp.hour=temp.hour-24;returntemp;}TimeTime::operator-(constTime&t)const{intans=0;Timetemp;temp.second=this->second-t.second;if(temp.secondvO){temp.second=temp.minute+60;ans=1;}temp.minute=this->minute-t.minute-ans;ans=0;if(temp.minutevO){temp.minute=temp.minute-ans;ans=1;}temp.hour=this->hour-t.hour-ans;if(temp.hourvO)temp.hour=temp.hour+24;returntemp;}TimeTime::operator+=(constTime&t){intans=0;this->second+=t.second;if(this->second>=60){this->second=this->second-60;ans=1;}this->minute+=t.minute+ans;ans=0;if(this->minute>=60){this->minute=this->minute+ans;ans=l;}this->hour+=t.hour+ans;if(this->hour>=24)this->hour=this->hour-24;return*this;}TimeTime::operator-=(constTime&t){intans=0;this->second-=t.second;if(this->second<0){this->second=this->second+60;ans=1;}this->minute-=t.minute+ans;ans=0;if(this->minute<0){this->minute=this->minute+60;ans=1;}this->hour-=t.hour+ans;if(this->hour<0)this->hour=this->hour+24;return*this;}TimeTime::operator=(constTime&t){this->hour=t.hour;this->minute=t.minute;this->second=t.second;return*this;}intmain(){Timenow(13,26,30),start(3,45,20),T;coutvv"最初时间:"vvstartvvendl;cout

温馨提示

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

评论

0/150

提交评论