面向对象程序设计(C)实验指导书_第1页
面向对象程序设计(C)实验指导书_第2页
面向对象程序设计(C)实验指导书_第3页
面向对象程序设计(C)实验指导书_第4页
面向对象程序设计(C)实验指导书_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

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

文档简介

1、1/22面向对象程序设计(C+)课程实验指导书安阳工学院计算机科学与信息工程学院软件工程教研室2018.9编号:课程总学时:64实验学时:32课程总学分:3.5实验学分:先修课程:C语言程序设计适用专业:计算机科学与技术,网络工程,软件工程一、本课程实验的主要目的与任务面向对象程序设计(C+)是计算机专业学生的一门专业基础课。C+是一种高效而又实用的程序设计语言,它既可以进行过程化程序设计,也可以进行面向对象程序设计,因此成为了编程人员最广泛使用的工具。主要任务是介绍C+语言中的数据类型,运算,语句结构及其程序设计的基本方法。使学生掌握一门高级程序设计语言,了解面向对象程序设计的基本概念与方法

2、,进而学会利用C+语言学会解决一般应用问题,从而掌握面向voidmain()2/22对象程序设计的基本知识和基本技能。并为后续的专业课程奠定程序设计基础。实验1C+基础一、实验目的1.加强学生掌握C+的基本知识点;2.加强学生掌握I/O流;3加强学生进一步理解函数的用法;4理解引用的概念及应用。三、实验内容1.用函数返回值实现统计A类学生和B类学生个数, 平均分大于等于80的为A类,其余为B类。四. 实验指导1.参考程序:#includeintmain()coutMynameisJone。coutcout2。coutendl。2.参考程序:#include#include/要用到格式控制符do

3、ubleamount=22.0/7。coutamountendl。coutsetprecision(0)amountendlsetprecision(1)amountendlsetprecision(2)amountendlsetprecision(3)amountendlsetprecision(4)amountendl。coutsetiosflags(ios:fixed)。coutsetprecision(8)amountendl。coutsetiosflags(ios:scientific)amountendl。coutsetprecision(6)。重新设置成原默认设置3.参考程序:#

4、include#includeintmain()(intnumber=1001。coutDecimal:decnumberendl3/22Hexadecimal:hexnumberendlOctal:octnumberendl。return04.参考程序:#include#includeintmain()(coutsetfill(*)setw21endlsetw(3)21endlsetw(4)21endlcoutsetfill()。/恢复默认设置return05.参考程序:#include#includevoidmain()(coutsetiosflags(ios:right)setw(5)1

5、setw(5)2setw(5)3endl。coutsetiosflags(ios:left)setw(5)1setw(5)2setw(5)3endl。6.参考程序:#includevoidmain()(constfloatPI=3.1415926f。floatr。floatz,s。coutr。z=2*PI*r。s=PI*r*r。cout圆的周长为:zendl。cout圆的面积为:sendl。7.参考程序:#includeusingnamespacestd。intarray64=60,80,90,75,(75,85,65,77,80,88,90,98,(89,100,78,81,(62,68,6

6、9,75,(85,85,77,91。int&level(intgrade,intsize,int&tA,int&tB)。intmain()(inttypeA=0,typeB=0。intstudent=6。intgradesize=4。for(inti=0。istudent。i+)/处理所有的学生level(arrayi,gradesize,typeA,typeB)+。/函数调用作为左值coutnumberoftypeAistypeAendl。coutnumberoftypeBistypeBendl。/system(PAUSE)。return0。int&level

7、(intgrade,intsize,int&tA,int&tB)intsum=0。for(inti=0。i=80)returntA。/typeAstudentelsereturntB。/typeBstudent5/22运行结果:NE八教学面向对急程序该汁枷 I)程序代玛第9章待nunberoFtypeAis3nunheroftvpeBis3请按任意键继续-实验2类和对象1、实验目的:掌握类的定义,根据具体需求设计类;深入理解C+中类的封装性;会根据类创建各种对象;掌握对象的各种成员的使用方法。2、实验内容定义一个满足如下要求的Date类。(1)用下面的格式输出日期:日/月/年;

8、(2)可运行在日前上加一天操作;(3)设置日期。参考代码:#includeclassDatepublic:voidDisplay()。voidAddOneDay()。voidSetDay(inty,intm,intd)。protected:boolLegal(inty,intm,intd)。boolIsLeapYear(inty)。intyearintmonthintday6/22。voidDate:Display()(coutday/month/year9999|y1|d1|m12)returnfalseintdayLimit=31。switch(m)case4:case6:case9:ca

9、se11:dayLimit-。if(m=2)dayLimit=IsLeapYear(y)?29:28。return(ddayLimit)?false:trueboolDate:IsLeapYear(inty)(return!(y%4)&(y%100)|!(y%400)。intmain()(Dated。d.SetDay(2018,2,28)。d.Display()。d.AddOneDay()。7/22d.Display()。system(PAUSE)。运行结果:实验3继承与派生1、实验目的:理解继承的概念,学习如何使用基类成员,了解基类成员在派生类中的访问控制;理解类的派生对代码复用的作

10、用。2、实验内容:设计一个人员类person和一个日期类date,由人员类派生出学生类Student和教师类professor,学生类和教师类的数据成员birthday为日期类。参考代码:#include#includeusingnamespacestd。classdate(public:date()(coutyearmonthday。voiddisplay()8/22(coutyear-month-day。private:intyear。intmonth。intday。classperson(protected:char*name。public:person。person:person()(

11、charnamestr50。coutnamestrname=newcharstrlen(namestr)+1。strcpy(name,namestr)。classstudent:publicperson(private:intID。intscoredatebirthdaypublic:student()(coutIDcoutscorevoiddisplay()(coutThebasicinformation:endlcoutIDtnametscoretbirthday.display()coutendl。classprofessor:publicperson9/22(public:profes

12、sor()(coutNo。coutmajorvoiddisplay()(coutThebasicinformation:endlcouttNotnametmajortbirthday.display()。coutendlprivate:intNocharmajor10。datebirthdayintmain()(studentstustu.display()professorprofprof.display()system(PAUSE)。return0运行结果:10/22实验4多态1、实验目的:掌握函数的概念及应用方法;理解多态性的运用和作用。2、实验内容:新建一个基类shape类是一个表示形

13、状的抽象类,area()为求图形面积的函数。请从shape类派出矩形类(rectangle)、三角形类(triangle)、圆类(circles)、并给出具体的求面积函数。参考代码:#include#includeusingnamespacestd。classshapepublic:virtualdoublearea()=0。virtualvoiddisplay()=0。shape()。classrectangle:publicshapepublic:rectangle(doublea=1,doubleb=1)x=a。y=b。doublearea()returnx*y。voiddisplay(

14、)(coutarea()endl。private:doublex。doubley。classtriangle:publicshape(11/22public:triangle(doublea=1,doubleb=1,doublec=1)(x=a。y=b。z=c。doublearea()(doublel=(x+y+z)/2。return(sqrt(l-x)*(l-y)*(l-z)*l)。voiddisplay()(coutarea()endlprivate:doublexdoubleydoublezclasscircles:publicshape(public:circles(doubleR=1

15、)(r=R。doublearea()(return3.14*r*r。voiddisplay()(coutarea()display()。p=newtriangle(3,4,5)。p-display()。p=newcircles(2)。p-display()。system(PAUSE)。return0。运行结果:实验5运算符重载1、实验目的:掌握运算符重载的概念及使用方法,掌握特殊运算符的重载要求和方法。2、实验内容:定义整数集合类intSet,实现如下功能:定义类的构造函数,并根据需要可以定义多个构造函数。(1)Clear函数:清空整数集合(2)IsEmpty():整数集合是否空集;(3)Is

16、MemberOf():判断某个整数是否在整数集合内(4)Operator+:增加一个整数到整数集合中(5)Operator-:从整数集合中删除一个整数元素;(6)Operator=:判断两个整数集合是否相等;(7)Operator*:求两个整数结合的交集;(8)Operator+:求两个整数集合的交集;(9)Operator=:整数集合的对象之间的赋值;(10)Operator:输出整数集合中的元素。提示:类intSet可以用数组的方式定义整数集合。例如:intelement200:保存整数集合数据;13/22intElementNum:指示整数集合的最后一个元素位置注意:一个整数集合中不允许

17、有相同元素存在,二是集合中的元素没有顺序。参考代码:#includeusingnamespacestd。classintSetpublic:intSet()。intSet(inta,intsize)。voidClear()。boolIsEmpty()。boolIsMemberOf(inta)。booloperator+(inta)。booloperator-(inta)。booloperator=(intSet&set)。intSetoperator+(intSet&set)。intSetoperator*(intSet&set)。voidoperator=(intSe

18、t&set)。friendostream&operator(ostream&,intSet&)。protected:intelement100。intElementNum。初始化整数集合元素intSet:intSet()for(inti=0。i=100)ElementNum=99。elseElementNum=size-1。for(inti=0。i=ElementNum。i+)elementi=ai。for(inti=size。i100。i+)elementi=0。14/22/清空整数集合元素voidintSet:Clear()(for(inti=0。i100。

19、i+)(elementi=0。ElementNum=-1。判断整数集合是否为空boolintSet:IsEmpty()(if(ElementNum=-1)returnfalse。elsereturntrue。/判断元素是否在整数集合中boolintSet:IsMemberOf(inta)(for(inti=0。iElementNum。i+)if(elementi=a)returntrue。returnfalse/往集合中增加一个元素boolintSet:operator+(inta)(if(IsMemberOf(a)returntrueelse(if(ElementNum=99)returnf

20、alseelse(ElementNum+。elementElementNum=a。returntrue/从集合中删除一个元素boolintSet:operator-(inta)15/22(intpos。if(!IsMemberOf(a)returnfalse。for(inti=0。i=ElementNum。i+)(if(elementi=a)(pos=i。break。for(inti=pos。iElementNum。i+)elementi=elementi+1。ElementNum-returntrue/判断两个整数集合是否相等boolintSet:operator=(intSet&s

21、et)(boolEqual。if(ElementNum!=set.ElementNum)returnfalse。for(inti=0。i=ElementNum。i+)(Equal=false。for(intj=0。jset.ElementNum。j+)(if(elementi=set.elementi)(Equal=true。break。if(!Equal)returnfalse。returntrue。16/22/求两个集合的交集intSetintSet:operator+(intSet&set)(inta100,size=0。for(inti=0。i=ElementNum。i+)fo

22、r(intj=0。j=set.ElementNum。j+)(if(elementi=set.elementj)(asize=elementi。size+。breakoreturnintSet(a,size)。求两个集合的并集intSetintSet:operator*(intSet&set)(inta100,size。for(inti=0。i=ElementNum。i+)ai=elementi。size=ElementNum+1。for(inti=0。i=set.ElementNum。i+)if(IsMemberOf(set.elementi)continueelse(if(size=

23、99)breakasize=set.elementi。size+returnintSet(a,size)整数集合对象间赋值voidintSet:operator=(intSet&set)(for(inti=0。i=set.ElementNum。i+)elementi=set.elementi。ElementNum=set.ElementNum。/输出整数集合中的元素ostream&operator(ostream&os,intSet&s)(for(inti=0。i=s.ElementNum。i+)oselementis.elementiendl。returnos

24、。intmain()(inta7=1,2,3,4,5,6,7。intb6=5,6,7,8,9,0。intSetS1(a,7),S2(b,6),S3。S3=S1+S2。coutS3。system(PAUSE)。return0。17/22运行结果:实验6输入输出流1、实验目的:掌握流的概念;掌握文件的打开与关闭;掌握文件的读写操作;2、实验内容:定义一个学生类,包含学生的学号、姓名和成绩等基本信息,将学生信息写入二进制文件student.dat中,实现对学生信息的显示、查询和删除等基本功能。参考代码:#include#includeusingnamespacestd。classStudent(pr

25、ivate:longNo。char*Name。intScore。18/22public:Student(longstu_no=0,char*stu_name=NULL,intstu_score=0)。longGetNo()。char*GetName()。intGetScore()。voidShowStudent()。Student:Student(longstu_no,char*stu_name,intstu_score)(No=stu_no。Name=stu_name。Score=stu_score。voidStudent:ShowStudent()(coutNotNametScoreend

26、l。longStudent:GetNo()(returnNochar*Student:GetName()(returnNameintStudent:GetScore()(returnScoreintmain()(Studentstu3=Student(200907001,Liming”,70),Student(200907002,Hujun”,80),Student(200907003,Wangtian,90)。inti,k,pos。fstreaminfile,outfile。outfile.open(Students.dat”,ios:out|ios:binary|ios:trunc)。if

27、(!outfile)cerrFileopenerror!endl。exit(1)。for(i=0。i3。i+)19/22outfile.write(char*)&stui,sizeof(stui)outfile.close()coutStudents.dat:endl。infile.open(Students.dat,ios:in|ios:binary)。for(i=0。i3。i+)infile.read(char*)&stui,sizeof(Student)coutstui.GetNo()tstui.GetName()tstui.GetScore()endlinfile.cl

28、ose()infile.open(Students.dat,ios:in|ios:binary)。coutkpos=(k-1)*sizeof(Student)。infile.seekg(pos)。infile.read(char*)&stui,sizeof(Student)。coutstui.GetNo()tstui.GetName()tstui.GetScore()endlinfile.close()。coutDeletetheSecondrecord!endl。infile.open(Students.dat,ios:in|ios:binary|ios:trunc)。for(i=0。i3。i+)if(i!=1)outfile.wr

温馨提示

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

评论

0/150

提交评论