




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验4 类中数据的共享与保护一、实验目的与实验要求(1)掌握友元的定义和应用。(2)掌握对象数组的定义、初始化方法及应用。(3)掌握指针和引用作为函数参数的应用。(4)掌握在类内定义静态数据成员以实现共享的基本方法,并根据需要定义相应的静态成员函数专门操作静态数据成员。(5)掌握类中常数据成员的定义及初始化方法,正确使用常数据成员。(6)理解常成员函数的意义以及常对象的意义,在程序中正确定义常对象,并正确调用相应的成员函数。二、实验内容1 编写一个程序,定义一个Circle类,按下述内容要求定义相关的数据成员及成员函数,最后在主函数中输出各圆的半径及对应面积,并一次性输出平均面积。Circle类中定义4个数据成员:常数据成员PI代表圆周率,静态数据成员count用于统计圆对象的个数,普通的double型数据成员r代表圆的半径,普通的double型数据成员area代表圆的面积,所有数据成员均定义为私有属性。再定义相关的成员函数,用于求单个圆的面积、输出圆的半径及面积、获取静态数据成员的值。 主函数中以一维对象数组定义若干个圆类的对象,调用相应的函数,求面积,并输出每个圆的半径及对应的面积,并且输出一次圆的个数。 在Circle类中增加一个友元函数的声明,用来求所有圆面积的平均值,并实现该函数的代码,主函数中增加调用此函数的语句,输出所有圆面积的平均值。#include using namespace std;class Circleconst double PI;static int count; double Radius;public:Circle(double r):PI(3.14) /Radius=new sizeof (r);Radius=r;count+;double Area();double Circumference();int Getmount(); Circle():PI(3.14) ;double Circle:Area()return PI*Radius*Radius;double Circle:Circumference()return Radius;int Circle:Getmount()return count;int Circle:count=0;int main()/Circle c1(3),c2(4);/coutarea of c1=c1.Area(),circumference of c1=c1.Circumference()c1.Getmount()endl;/coutarea of c2=c2.Area(),circumference of c2=c2.Circumference()c2.Getmount()endl;double r;Circle c2=Circle(3),Circle(4); /int count=0; for(int i=0;ir;Circle(r);/ci.Circle:Area();/CircleArea();/CiecleCircumference();/Circle c1;coutarea of c=ci.Area()endl,circumference of c=ci.Circumference()endlci.Getmount()endl; return 0;2. 程序改错,请修改下列程序,尽量减少增行或减行,使程序的运行结果如下:The number of all students: 0The number of all students: 1The number of all students: 0The number of all students: 2The number of all students: 2 要求:类中的数据成员连同访问属性均不可以修改。/错误程序源代码如下:#include using namespace std;class Studentprivate:char name20;static int total; /用来统计学生总人数public: Student( ) total+; Student( ) Student(char *p=Wang);static int GetTotal( );static int Student:total=0; Student:Student(char *p=Wang) strcpy(name,p);total+;static int Student:GetTotal( ) return total;int main() coutThe number of all students: Student:totalendl; Student *p=new Student(Li); coutThe number of all students: GetTotal( )endl;delete p; coutThe number of all students: Student:totalendl; Student s2; coutThe number of all students: s0.totalendl; coutThe number of all students: s1.totalendl;return 0;#include using namespace std;class Studentprivate:char name20;static int total;public:Student() total+;Student()total-;Student(char*p=Wang);static intGetTotal(); int Student:total=0;Student:Student(char*p)strcpy(name,p);total+; int Student:GetTotal()return total;int main()coutThe number of all students:Student:GetTotal()endl;Student *p=new Student(Li);coutThe number of all students:GetTotal( )endl;delete p;coutThe number of all students:GetTotal()/*Student:total*/endl;Student s2;coutThe number of all students:s0.GetTotal()endl;coutThe number of all students:s1.GetTotal()endl;return 0;3. 设计一个Score类,该结构有两个数据成员,分别为整型的home_team(主队)和opponent(客队)。用Score类定义一个含有5个元素的game数组,用于记录一个球队全部5场比赛的每场比分。 定义相关的构造函数,初始比分均为0:0,Set()函数用于修改每场比分,GetHometeam()函数用于提取主队的比分,GetOpponent()函数用于提取客队的比分。Display()用于显示比分情况,输出形如55:66。 定义对象数组game,并初始化对象数组,以98:67,105:103,88:96的比分给game数组的前三元素赋初值。 调用Set()函数为对象数组的其他元素赋值。 设计一个查询功能,让用户输入场次,查询该场次比分情况,以输入0为结束。 定义普通函数result(Score *p,Score &e),Score类指针用来传递对象数组的首地址,Score类对象e为一引用,用来存储比赛的总比分成绩。在result()函数中,通过统计每场比赛的比分情况,得出最后总成绩,在主函数中根据end对象中比赛结果,输出最后的胜利者。完成上述程序并回答下列问题:(1)如果将函数result(Score *p,Score &e)改为result(Score *p,Score e),修改主函数的调用语句,观察程序运行结果,说明原因。(2)如果将函数result(Score *p,Score &e)改为result(Score *p,Score *e),修改主函数的调用语句,观察程序运行结果,说明原因。#include using namespace std;class Scoreint hometeam;int opponent;public:Score()hometeam=0;opponent=0;Score(int a,int b)hometeam=a;opponent=b;voidSet(int a,int b)hometeam=a;opponent=b;intGetHometeam()return hometeam;intGetOpponent()return opponent;intDisplay() return hometeam; return opponent; int result(Score *p,Score &e)for(int i=0;ipi.GetOpponent()e.hometeam+; else e.opponent-;int main()int i;Score game5=Score(98,67),Score(105,103),Score(88,96),Score(),Score();Score F1=Score(0,0);coutplease input the numberi; while(i0) coutgamei-1.GetHometeam()endlgamei-1.GetOpponent()endl; return 0; cout最终冠军是F0.GetOpponent() ) cout冠军是主队endl;else cout冠军是客队endl;4. 阅读课本P89 3.6 “程序实例-学生信息管理系统” 程序,按照课本的运行步骤运行一遍。然后修改该程序,使得在主菜单中选择输入2时,显示如下的的二级菜单: *1.按照姓名查询* *2.按照年龄查询* *3.按照专业查询* *4.按照学号查询* *0.返回主菜单*编写对应的二级菜单程序,实现按照不同的查询方式查询学生信息,最后设计运行步骤验证修改后程序是否达到查询学生信息的目的。#include#include example3_26_student.husing namespace std;const int N=10;void menu();void menu2();void OutputStu( Student *array );void InputStu(Student *array);int SearchStu( Student *array, char *na);int SearchStu2(Student *array, char*na2);int SearchStu3(Student *array,char *number1);int count=0;int main()Student arrayN;int choice;domenu();coutchoice;if( choice=0 & choice = 3 ) switch(choice)case 1:InputStu(array);break;case 2:domenu2(); /coutInput the name searchedendl; coutchoice;if(choice=0&choicena; int i; i=SearchStu(array,na); if(i=N) coutt; arrayt.Display(); case 3: coutInput the specialityna2;int Q; Q=SearchStu2(array,na2); if (Q=N) cout查无此专业!n; else arrayi.Display(); break; case 4: coutInput thenumbernumber120; int Q; Q=SearchStu3(array,number1); if (Q=N) coutna; /int i; / i=SearchStu(array, na); /if (i=N) / cout查无此人!n; /else / arrayi.Display(); /break;icase 1case 3:OutputStu(array); break;default:break;while(choice);return 0; void menu()cout*1.录入信息*endl;cout*2.查询信息*endl;cout*3.浏览信息*endl;cout*0.退 出*endl;void menu2() cout*1.按照姓名查询*endl; cout*2.按照年龄查询*endl; cout*3.按照专业查询*endl; cout*4.按照学号查询*endl; c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 车险核保考试题及答案
- 发展新质生产力的
- 福建新质生产力发展计划
- 新质生产力赋能出版业
- 民族英雄戚继光课件
- 民族舞蹈基本功训练课件
- 植树节活动方案(模板)
- 数字科技赋能新质生产力
- 2025年妇产科超声常见疾病诊断模拟考试答案及解析
- 科学家视角:新质生产力的创新密码
- 2025三门县国企招聘考试题目及答案
- 2025-2030红色旅游行业市场发展现状及发展前景与投资机会研究报告
- 植筋施工方案 全
- 2025四川省前期物业服务合同示范文本
- 法院舆情风险防控课件
- 动态系统仿真技术-全面剖析
- 护理人员绩效考核制度
- 人教版六年级语文上册教学计划(含进度表)
- 苏教版科学五年级上册全册教案(含反思)
- 餐饮服务与数字化运营 习题及答案 项目六
- 天津地铁设备管理制度范文
评论
0/150
提交评论