


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、深圳大学实 验 报 告课程名称: 面向对象程序设计实验序号:试验七实验名称: C+程序设计综合实验班 级:姓 名:隔壁老王学 号:2010100001实验日期:2011年12月_!1日、实验目的:(1) 掌握类和对象的实现;(2) 掌握类的静态成员函数和友元函数的运用;(3) 掌握类的继承与派生的编程特点;(4) 掌握运算符承载的程序设计。、实验环境: 硬件环境:办公楼二楼软件实验室 软件环境: Visual C+ 6.0 集成环境三、实验要求:1. 定义一个课程类CCourse,其中包含课程号(Iong no)、课程学分(float credit) 两个数据成员,以及相应的构造函数、拷贝构造
2、函数、析构函数和打印数据 成员的成员函数 print()。2. 为CCourse类增加一个数据成员课程总数(int total_course),并增加一个成 员函数 getTotalCourse()获取total_course的值,编写一个友元函数 getCourseNo(获取课程号no。做如上修改后,重新实现 CCourse类(与第1 问相同的不用再重复,注意说明数据成员和成员函数的存储类型,以便能够 用类名来调用 getTotalCourse()。3. 为CCourse类定义小于运算符()运算符重载函数。CCourse类对象大 小的比较是根据其课程学分(credit)的值的大小来实现的(与
3、第2问相同的不 用再重复 ) 。4. 编写测试程序对Ccourse类进行测试。5. 以CCourse类为基类,派生出面向对象程序设计课程类COOP,并在该类中增加一个表示开课单位的指针数据成员(char *p_ope nby)和根据学生学号判 断能否选课的成员函数 bool select(const char *p_xh)(只有学号前4位为2010 的学生可选面向对象程序设计课程 )。写出 COOP 类的完整定义 (包括构造、 拷贝构造、析构和select()成员函数的实现)。6. 编写测试程序进行测试。7. 为了能够采用动态联编的方式调用派生类 COOP 的 bool select( con
4、st char *p_xh)成员函数,应该在 Ccourse类及其派生类C00!中作何改动?四、实验内容与结果: (源程序及运行截图)1、class CCourseprivate: long no; float credit; char *p_name;public: CCourse(); CCourse(long n,char *na,float c); CCourse(const CCourse &course); void print(); CCourse(); ;CCourse:CCourse() no=0;p_name=new char20; strcpy(p_name,course
5、 name); credit=0.0; CCourse:CCourse(long n,char *na,float c) no=n;p_name=new char20; strcpy(p_name,na); credit=c; CCourse:CCourse(const CCourse &course) p_name=new charstrlen(course.p_name)+1;if(p_name=NULL) exit(0);strcpy(p_name,course.p_name); credit=course.credit; void CCourse:print() coutCourse
6、number:noendl; coutCourse name:p_nameendl; coutCourse credit:creditendl; CCourse:CCourse() delete p_name;2、class CCourseprivate: long no; float credit; char *p_name;static int total_course;public:CCourse();CCourse(long n,char *na,float c); CCourse(const CCourse &course); void print();CCourse();stati
7、c getTotalCourse() return total_course; friend long getCourseNo(const CCourse &course);int CCourse:total_course = 0;CCourse:CCourse() no=0; p_name=new char20; strcpy(p_name,course name); credit=0.0; CCourse:CCourse(long n,char *na,float c) no=n; p_name=new char20; strcpy(p_name,na); credit=c; total_
8、course+; CCourse:CCourse(const CCourse &course) p_name=new charstrlen(course.p_name)+1;if(p_name=NULL)exit(0); strcpy(p_name,course.p_name); credit=course.credit; total_course+; void CCourse:print() coutCourse number:noendl; coutCourse name:p_nameendl; coutCourse credit:creditendl;CCourse:CCourse()
9、delete p_name; total_course-;elselong getCourseNo(const CCourse &course) return course.no; 3、class CCoursepublic:bool operator (const CCourse &course); ;int CCourse:total_course = 0;bool CCourse:operator (const CCourse &course) if (credit course.credit)return true;elsereturn false;4、源程序:#include usi
10、ng namespace std;#include class CCourseprivate:long no;float credit;char *p_name;static int total_course;public:CCourse();CCourse(long n,char *na,float c);CCourse(const CCourse &course); void print();CCourse();static getTotalCourse() return total_course; friend long getCourseNo(const CCourse &course
11、);bool operator (const CCourse &course);bool CCourse:operator (const CCourse &course)if (credit course.credit)return true;return false;CCourse:CCourse()no=0;p_name=new char20;strcpy(p_name,course name);credit=0.0;CCourse:CCourse(long n,char *na,float c)no=n;p_name=new char20;strcpy(p_name,na);credit
12、=c; total_course+;CCourse:CCourse(const CCourse &course)p_name=newcharstrlen(course.p_name)+1;if(p_name=NULL)exit(0);strcpy(p_name,course.p_name);credit=course.credit;total_course+;void CCourse:print()coutCourse number:noendl;coutCourse name:p_nameendl; coutCourse credit:creditendl;CCourse:CCourse()
13、delete p_name;total_course-;int CCourse:total_course = 0;long getCourseNo(c onst CCourse &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
14、();CCourse course4(2011103,面向对象程序设计,4.0);course4.pri nt();c=course4.getTotalCourse(); sc=getCourseNo(course1); coutTotalcourse:ce ndlcourse1sNO:sce ndl;if(course1course2) coutcourse2s credit larger than course1s.e ndl;elsecoutcourse2s credit do notlarger tha n course1s.se naim号二咼等数学Jou.i*se credit:
15、5course nunber:2011101n為me誥大学英语credit :2.5Jo UPS e n mnbe r = 2011102Ui*s e n ame * 线性代数course credit 3ourse number : 2011103bourse name:面向对漿裡序设计Toijii*se credit : 4Fotal coui*se-4s NO = 2011100;our,se2f s credit do not lartfer than cour,sel, s.3ress any key to comt inueH5-6、源程序:class COOP : public
16、CCourseprivate:char *p_openby;public:bool select(c onst char *p_xh)if(strncmp(p_xh,2010,4)=0) return true;elsereturn false;COOP(long n, char *na, float c, char *p_ope n) : CCourse (n,n a,c)p_openby=newcharstrle n( p_ope n)+1;strcpy(p_ope nby, p_ope n);COOP(co nst COOP & coop)p_openby=newcharstrle n(
17、 coop.p_ope nby)+1;if(p_ope nby=NULL)exit(0);strcpy(p_ope nby,coop.p_ope nby);COOP() delete p_ope nby; void prin t()CCourse:pri nt();cout 开 课 单 位p_ope nbye ndl;void mai n()程序截图:char stn o20;COOP coop1(2011103,面向对象程序设 计,4.0,计算机与软件设计学院”);coopl.pri nt();cout请输入学号: st no;if(coop1.select(st no)cout可以选课en
18、dl;elsecout不能选课endl;程序截图:7、要实现动态联编成员函数必须声明为 则派生类中不必再声明。在class CCourse定义中增加:public:virtual bool select(c onst char *p_xh)if(strncmp(p_xh,2010,4)=0)return true;elsereturn false; ourse n umbei* : -2078 976689 oupse n罰e:面向对象程序设计 oursa credit:4迷单位:计算机与软件设计学院 号:!0105 00123可以选课Course Course Jour scnumber:-2078976689 name :面向对象程序设计 credit:4
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 四川省成都市金牛区2023-2024学年五年级下学期语文期末试卷(含答案)
- 2025汽车销售合同协议书样本简单
- 2025合同驱动的能源管理系统
- 2025客房销售合同范本下载
- 2025网络广告制作合同
- 2025混泥土浇筑合同
- 2025年婚礼摄影服务合同
- 2025建筑工程合同范本
- 2025合同之各类型详细划分
- 2025跨国技术合作合同(中英文对照)
- 2025地质勘察合同范本
- 2025年时政政治试题库及答案
- 抗帕金森病试题及答案
- 2025-2030中国钢结构行业现状供需分析及市场深度研究发展前景及规划可行性分析研究报告
- 阅读提取信息课件
- 2025年河南省中考数学二轮复习压轴题:动态几何问题专练
- 《知识产权保护》课件
- 北京市东城区2024-2025学年度第二学期高三综合练习(一)(东城高三一模)【历史试卷+答案】
- 江苏省2024年中职职教高考文化统考烹饪专业综合理论真题试卷
- 2025年电力人工智能多模态大模型创新技术及应用报告-西安交通大学
- T-CBIA 009-2022 饮料浓浆标准
评论
0/150
提交评论