C程序设计综合实验_第1页
C程序设计综合实验_第2页
C程序设计综合实验_第3页
C程序设计综合实验_第4页
C程序设计综合实验_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

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

文档简介

1、深圳大学实验报告课程名称:面向对象程序设计实验序号:试验七实验名称: C+程序设计综合实验班级:姓名:隔壁老王学 号:2010100001实验日期:201112月_!1日教师签字:、实验目的:(1) 掌握类和对象的实现;(2) 掌握类的静态成员函数和友元函数的运用;(3) 掌握类的继承与派生的编程特点;(4) 掌握运算符承载的程序设计。、实验环境:硬件环境:办公楼二楼软件实验室软件环境:Visual C+ 6.0集成环境三、实验要求:1. 定义一个课程类CCourse,其中包含课程号(long no)、课程学分(float credit)两个 数据成员,以及相应的构造函数、拷贝构造函数、析构函

2、数和打印数据 成员的成员函 数 print()o2. 为CCourse类增加一个数据成员课程总数(int total_course),并增加一个成 员函数 getTotalCourse()获取total_course的值,编写一个友元函数getCourseNo(获取课 程号no。做如上修改后,重新实现CCourse类(与第1问相同的不用再重复,注意说 明数据成员和成员函数的存储类型,以便能够用类名来调用getTotalCourse()o3. 为CCourse类定义小于运算符(2 J运算符重载函数。CCourse类对象大小的 比较是根据其课程学分(credit)的值的大小来实现的(与第2问相同的

3、不用再重复)。4. 编写测试程序对Ccourse类进行测试。5. 以CCourse类为基类,派生出面向对象程序设计课程类COOP,并在该类中增加一个表示开课单位的指针数据成员(char *p_ope nby)和根据学生学号判 断能 否选课的成员函数bool select(const char *p_xh)(只有学号前4位为2010的学生可 选面向对象程序设计课程)o写出COOP类的完整定义(包括构造、拷贝构造、析 构和select。成员函数的实现)。6. 编写测试程序进行测试。7. 为了能够采用动态联编的方式调用派生类COOP的bool select( const char *p_xh)成员函

4、数,应该在Ccourse类及其派生类C00!中作何改动?四、实验内容与结果:(源程序及运行截图)1、 class CCourseprivate: long no; float credit; char *p_name; public: CCourse(); CCourse(long n,char a,float c); CCourse(const CCourse &course); void print(); CCourse();CCourse:CCourse() no=0;p_name=new char20;strcpy(p_name/course name”); credit=O.O;CC

5、ourse:CCourse(long n,char a,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(O);strcpy(p _n ame,course _n ame); credit=course.credit;void CCourse:print() coutHCourse number:MCCourse() dele

6、te p_name;2v class CCourseprivate: long no; float credit; char *p_name;static int total_course;public:CCourse();CCourse(long n,char a,float c);CCourse(const CCourse &course); void print();CCourse();static getTotalCourse() return total_course;friend long getCourseNo(const CCourse& course);int CCourse

7、:total_course = 0; no=0; p_name=new char20; strcpy(p_namecourse nameH); credit=O.O;CCourse:CCourse(long n,char na,float c) no=n; p_name=new char20; strcpy(p_name,na); credit=c; total_course+;CCourse:CCourse(const CCourse &course) p_name=new charstrlen(course.p_name)+1; if(p_name=NULL)exit(O); strcpy

8、(p_ name,course _n ame); credit=course.credit; total_course+;void CCourse:print() coutHCourse number:HCCourse() delete p_name; total_course-; CCourse:CCourse()long getCourseNo(const CCourse &course)return course.no;3、class CCourse(public:bool operator v(const CCourse &course); int CCourse:total_cour

9、se = 0;bool CCourse:operator (const CCourse& course)if (credit course.credit)return true;elsereturn false; 4、源程序:#inelude using namespace std;#inelude class CCourseprivate:long no;float credit;char 卡ame;static int total_course;public:CCourse();CCourse(long n,char a,float c);CCourse(const CCourse &co

10、urse); void print();CCourse();static getTotalCourse() return total_course;friend long getCourseNo(const CCourse& course);bool operator v(const CCourse &course);bool CCourse:operator v(const CCourse& course)(if (credit course.credit)return true;elseCCourse:CCourse()no=0;p_name=new char20;strcpy(p_nam

11、e,ncourse nameH); credit=O.O;)CCourse:CCourse(long n,char Fa,float c)no=n;p_name=new char20;strcpy(p _n ame,na); credit=c; total_course+;)CCourse:CCourse(const CCourse &course)p_n ame=new charstrle n(course.p_ name)+1 ;if(p_name=NULL)exit(O);strcpy(p_name,course.p_ name); credit=course.credit;total_

12、course+;)void CCourse:print()coutHCourse number:nvnovvendl; coutHCourse name:Mp_namevvendl; coutHCourse credit:Hcreditendl;)CCourse:CCourse()delete p_name;total_course-;return false;int CCourse:total_course = 0;long getCourseNo(c onst CCourse Scourse)retur n course .no;void mai n()int c=0;long sc;CC

13、ourse coursel (2011100,高 等数学”,5.0);coursel.pri nt();CCourse course2(2011101大 学英语”25);course2.pri nt();CCourse course3(2011102/*线 性代数”,3.5);course3.pri nt();CCourse course4(2011103,*面向对象程序设计”,4.0);course4.pri nt();5-6、源程序:class COOP : public CCourseprivate:char *p_openby;public:bool select(c onst cha

14、r *p_xh)if(strncmp(p_xh;2O10,4)=0) returntrue;elsereturn false;COOP(Iong n, char Fa3 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);c=course4.getTotalCourse();sc=getCourseNo(course1); coutnTotal course:,fce ndlHcourse1fs NO:Hsce ndl;if(cour

15、se1 se naiin号二咼等数学Jou.i*se credit: 5 course nunber: 201110111為me誥大学英语credit: 25Jo UPS e 11 mnbe r = 2011102Ui*s e 11 ame *线性代数Icourse credit 3ourse number : 2011103bourse name:面向对漿裡序设计Toijii*se credit: 4IFotal coni*se-4sNO = 2011100COOP(co nst COOP & coop)p_openby=newcharstrle n( coop.p ope nby)+1;i

16、f(p_ope nby=NULL) exit(O);strcpy(p_ope nby5coop.p_ope nby); -COOP() delete p ope nby; void prin t()CCourse:pri nt();cout开课单位 vvp_ope nbye ndl;;void mai n()char stn o20;COOP coopl (2011103,面向对象程序设 计”,4.0,%十算机与软件设计学院”);coopl.pri nt();cout请输入学号:endl; cin st no;if(coop1.select(st no)cout可以选课endl;elsecou

17、t不能选课endl;程序截图:7、要实现动态联编成员函数必须声明为 则派生类中不必再声明。在class CCourse定义中增加:public:virtual bool select(c onst char *p_xh) if(strncmp(p_xh,2010,4)=0) return true;五、实验总结:Isereturn false;CourseCourseJour scourse n umbei* : -2078 976689oupse n罰e:面向对象程序设 oursa credit:4迷单位:计算机与软件设计学院号:!0105 00123可以选课number: -2078976689 name :面向对象程 序设计credit: 4香隸輻1

温馨提示

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

评论

0/150

提交评论