




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、深圳大学实 验 报 告课程名称: 面向对象程序设计实验序号:试验七实验名称:C+程序设计综合实验班 级:姓 名:隔壁老王学 号:2010100001 实验日期:2011年12_月丄 日教师签字:、实验目的:(1) 掌握类和对象的实现;(2) 掌握类的静态成员函数和友元函数的运用;掌握类的继承与派生的编程特点;(4)掌握运算符承载的程序设计。、实验环境:硬件环境:办公楼二楼软件实验室软件环境:Visual C+ 6.0集成环境三、实验要求:1.定义一个课程类CCourse,其中包含课程号(Io ng no)、课程学分(float credit)两个数据成员,以及相应的构造函数、拷贝构造函数、析构
2、函数和打印数据成员的成员函数print()。2.为CCourse类增加一个数据成员课程总数(int total_course),并增加一个 成 员函数getTotalCourse()获取total_course 的值,编写一个友元 函数 getCourseNo()获取课程号no。做如上修改后,重新实现 CCourse类(与第1 问相同的不用再重复,注意说明数据成员和成员函数的存储类型,以便能够用类名来调用getTotalCourse()。3.为CCourse类定义小于运算符('运算符重载函数。CCourse类对象大小的比较是根据其课程学分(credit)的值的大小来实现的(与第2问相同
3、的不用 再重复)。4. 编写测试程序对Ccourse类进行测试。5. 以CCourse类为基类,派生出面向对象程序设计课程类 COOP,并在该类 中增加一个表示开课单位的指针数据成员 (char *p_openby)和根据学生学号 判断能否选课的成员函数 bool select(const char *p_xh)(只有学号前4位为 2010的学生可选面向对象程序设计课程)。写出COOP类的完整定义(包括构 造、拷贝构造、析构和select()成员函数的实现)。6. 编写测试程序进行测试。7. 为了能够采用动态联编的方式调用派生类COOP的bool select(const char*p_xh)
4、成员函数,应该在Ccourse类及其派生类COOP中作何改动?四、实验容与结果:(源程序及运行截图)1、class CCourseprivate:long no;float credit;char *p_n ame;public:CCourse();CCourse(l ong n ,char *n a,float c); CCourse(c onst CCourse &course); void prin t();CCourse(); ;CCourse:CCourse()no=0;p_n ame=new char20;strcpy(p_ name,"course n ame&
5、quot;);credit=0.0;CCourse:CCourse(l ong n ,char *n a,float c) no=n;p_n ame=new char20;strcpy(p_ name ,n a);credit=c; CCourse:CCourse(c onst CCourse &course) p_n ame=newcharstrle n( course.p_ name)+1;if(p_n ame=NULL)exit(0);strcpy(p_ name,course.p_ name); credit=course.credit;void CCourse:pri nt(
6、) cout<<"Course nu mber:"< <no <<e ndl; cout<<"Coursen ame:"<<p_ name<<e ndl;cout<<"Coursecredit:"<<credit<<e ndl;CCourse:CCourse() delete p_n ame;2、class CCourseprivate:long no;float credit;char *p_n ame;static int
7、total_course;public:CCourse();CCourse(l ong n ,char *n a,float c);CCourse(c onst CCourse &course);void prin t();CCourse();static getTotalCourse() retur ntotal_course; friend longgetCourseNo(c onstCCourse &course); ;int CCourse:total_course = 0;CCourse:CCourse()no=0;p_n ame=new char20; strcpy
8、(p_ name,"course n ame"); credit=0.0;CCourse:CCourse(lo ng n, char *n a,float c) no=n;p_n ame=new char20; strcpy(p_ name ,n a); credit=c;total_course+;CCourse:CCourse(c onst CCourse &course) p_n ame=newcharstrle n( course.p_ name)+1;if(p_n ame=NULL)exit(0);strcpy(p_ name,course.p_ name
9、); credit=course.credit;total_course+;void CCourse:pri nt() cout<<"Course nu mber:"< <no <<e ndl; cout<<"Coursen ame:"<<p_ name<<e ndl;cout<<"Coursecredit:"<<credit<<e ndl;CCourse:CCourse() delete p_n ame;total_cours
10、e-;long getCourseNo(c onst CCourse &course) retur n course .no;3、class CCoursepublic:booloperator<(c onst CCourse&course);int CCourse:total_course = 0;bool CCourse:operator <(c onst CCourse&course) if (credit < course.credit)return true;elsereturn false;4、源程序:#in clude <iostr
11、eam>using n amespace std;#in clude <stri ng>class CCourseprivate:long no;float credit;char *p_n ame; static int total_course;public:CCourse();CCourse(l ong n ,char *n a,float c);CCourse(c onst CCourse &course);void prin t();CCourse();static getTotalCourse() retur ntotal_course; friend l
12、ong getCourseNo(c onst CCourse &course);bool operator<(c onstCCourse&course);bool CCourse:operator<(c onst CCourse&course)if (credit < course.credit) return true;elsereturn false;CCourse:CCourse()no=0;p_n ame=new char20;strcpy(p_ name,"course n ame"); credit=0.0;CCours
13、e:CCourse(lo ng n, char *n a,float c)no=n;p_n ame=new char20; strcpy(p_ name ,n a);credit=c;total_course+;CCourse:CCourse(c onst CCourse &course)p_n ame=newcharstrle n( course.p_ name)+1;if(p_n ame=NULL) exit(0);strcpy(p_ name,course.p_ name);credit=course.credit; total_course+;void CCourse:pri
14、nt()cout<<"Course nu mber:"< <no <<e ndl;cout<<"Coursen ame:"<<p_ name<<e ndl;cout<<"Coursecredit:"<<credit<<e ndl;CCourse:CCourse()delete p_n ame; total_course-;int CCourse:total_course = 0;long getCourseNo(c onst C
15、Course &course)retur n course .no;void mai n()int c=0;long sc;CCourse course1(2011100,"高 等数学",5.0);course1.pri nt();CCourse course2(2011101,"大学英语 ",2.5);course2.pri nt();CCourse course3(2011102,"线性代数 ",3.5);course3.pri nt();CCourse course4(2011103,"面向对象 程序设计&qu
16、ot;,4.0);course4.pri nt(); c=course4.getTotalCourse(); sc=getCourseNo(course1); cout<<"Totalcourse:"<<c<<e ndl<<"course1'sNO:"<<sc<<e ndl;if(course1<course2) cout<<"course2's credit larger than course1's."<<
17、e ndl;elsecout<<"course2's credit do notlarger tha n course1's."<<e ndl;Couvse nunbei*:20111O0Course name : 尊数字Course credit:5Course n urnber: 20111 BlCo urs e name :大学英语Course credit:2-5Course number:2011102Cotirse wee =线性代数Gqpfwe creidit = 3 «5Course niunber; 201
18、1103Course n ame: g向对象程序设计Course credit:4Total courseMcnursel* s NO:20111QOcourse2J s credit do not larger than counsel1 s. Press «nu key to continu鶴.程序截图:5 6、源程序:class COOP : public CCourseprivate:char *p_openby;public:bool select(c onst char *p_xh)if(strncmp(p_xh,"2010",4)=0)return
19、true;elsereturn false;COOP(long n, char *na, float c, char*p_ope n) : CCourse (n,n a,c)p_ope nby=newcharstrle n( p_ope n)+1;strcpy(p_ope nby, p_ope n);COOP(co nst COOP &coop)p_openby=newcharstrle n( coop.p_ope nby)+1;if(p_ope nby=NULL)exit(0);strcpy(p_ope nby,coop.p_ope nby);COOP() delete p_ope
20、nby; void prin t()CCourse:pri nt();cout<<" 开 课 单 位 "<<p_ope nby<<e ndl;void mai n()char stno 20;COOP coop1(2011103,"面向对象程序设计",4.0,"计算机与软件设计学院");coop1.pri nt();cout<<"请输入学号:"<<endl; cin> >st no;if(coop1.select(st no) cout<<"可以选课"<<endl;else cout<<"不能选课"<<e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 消费贷款购车合同(3篇)
- 2025年棉花加工成套设备项目合作计划书
- 理财顾问实习报告范文
- 2025年饲料营养型添加剂项目发展计划
- 2025年特种丝制品项目合作计划书
- 教育技术终身学习的助推器
- 2025年浙江省杭州市杭州二中物理高二下期末质量检测试题含解析
- 智慧城市管理与服务的数字化转型之路
- 国际合作在提升教育国际化水平中的贡献
- 专题04 读后续写精彩结尾及主题升华仿写(测试)原卷版-2025年高考英语二轮复习
- 2025年 济南综保控股集团有限公司招聘考试试卷附答案
- TOEFL阅读100篇附答案
- 教师调动登记表(模板)
- 2022年医院收费员考试试题及答案
- 福建省林业行政执法人员法律考试
- 《组织机构代码证》word版
- 钢筋下料单(参考模板)
- 欧亨利短篇小说集(课堂PPT)
- OPGW光缆计算
- 5710装备生产过程质量监督要求
- 建筑分户验收监理质量评估报告
评论
0/150
提交评论