C++程序设计综合实验.doc

上传人:豆**** 文档编号:23971554 上传时间:2022-07-03 格式:DOC 页数:13 大小:459.50KB
返回 下载 相关 举报
C++程序设计综合实验.doc_第1页
第1页 / 共13页
C++程序设计综合实验.doc_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《C++程序设计综合实验.doc》由会员分享,可在线阅读,更多相关《C++程序设计综合实验.doc(13页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateC+程序设计综合实验深 圳 大 学深 圳 大 学实 验 报 告课程名称: 面向对象程序设计 实验序号: 试 验 七 实验名称: C+程序设计综合实验 班 级: 姓 名: 隔壁老王 学 号:2010100001 实验日期:2011 年12月 11日 教师签字: 一、实验目的:(1)掌握类和对象的实现;(2)掌握类的静态成员函数和友元函数的运用;(3)掌握类的继承与派生的

2、编程特点;(4)掌握运算符承载的程序设计。二、实验环境:硬件环境:办公楼二楼软件实验室软件环境:Visual C+ 6.0 集成环境三、实验要求:1. 定义一个课程类CCourse,其中包含课程号(long no)、课程学分(float credit)两个数据成员,以及相应的构造函数、拷贝构造函数、析构函数和打印数据成员的成员函数print()。2. 为CCourse类增加一个数据成员课程总数(int total_course),并增加一个成员函数getTotalCourse()获取total_course的值,编写一个友元函数getCourseNo()获取课程号no。做如上修改后,重新实现C

3、Course类(与第1问相同的不用再重复,注意说明数据成员和成员函数的存储类型,以便能够用类名来调用getTotalCourse()。3. 为CCourse类定义小于运算符()运算符重载函数。CCourse类对象大小的比较是根据其课程学分(credit)的值的大小来实现的 (与第2问相同的不用再重复) 。4. 编写测试程序对Ccourse类进行测试。5. 以CCourse类为基类,派生出面向对象程序设计课程类COOP,并在该类中增加一个表示开课单位的指针数据成员(char *p_openby)和根据学生学号判断能否选课的成员函数bool select(const char *p_xh)(只有学

4、号前4位为2010的学生可选面向对象程序设计课程)。写出COOP类的完整定义(包括构造、拷贝构造、析构和select()成员函数的实现)。6. 编写测试程序进行测试。7. 为了能够采用动态联编的方式调用派生类COOP的bool select(const char *p_xh)成员函数,应该在Ccourse类及其派生类COOP中作何改动? 四、实验内容与结果:(源程序及运行截图)-1、class CCourseprivate:long no;float credit;char *p_name;public:CCourse();CCourse(long n,char *na,float c);CC

5、ourse(const CCourse &course);void print();CCourse();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;CCourse:CCourse(const CCourse &course)p_name=new charstrlen(course.p_name)+1;if(

6、p_name=NULL)exit(0);strcpy(p_name,course.p_name);credit=course.credit;void CCourse:print()coutCourse 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();

7、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);int CCourse:total_course = 0;CCourse:CCourse()no=0;p_name=new char20;strcpy(p_name,course name);credit=0.0;CCourse:CCours

8、e(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(0);strcpy(p_name,course.p_name);credit=course.credit;total_course+;void CCourse:print()coutCourse number:noendl;c

9、outCourse name:p_nameendl;coutCourse credit:creditendl;CCourse:CCourse()delete p_name;total_course-;long 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

10、 course.credit)return true;elsereturn false;4、源程序:#include using 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() retur

11、n total_course; friend long getCourseNo(const CCourse &course);bool operator (const CCourse &course);bool CCourse:operator (const CCourse &course)if (credit course.credit)return true;elsereturn false;CCourse:CCourse()no=0;p_name=new char20;strcpy(p_name,course name);credit=0.0;CCourse:CCourse(long n

12、,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(0);strcpy(p_name,course.p_name);credit=course.credit;total_course+;void CCourse:print()coutCourse number:noendl;coutCours

13、e name:p_nameendl;coutCourse credit:creditendl;CCourse:CCourse()delete p_name;total_course-;int CCourse:total_course = 0;long getCourseNo(const CCourse &course)return course.no;void main()int c=0;long sc;CCourse course1(2011100,高等数学,5.0);course1.print();CCourse course2(2011101,大学英语,2.5);course2.prin

14、t();CCourse course3(2011102,线性代数,3.5);course3.print();CCourse course4(2011103,面向对象程序设计,4.0);course4.print();c=course4.getTotalCourse();sc=getCourseNo(course1);coutTotal course:cendlcourse1s NO:scendl;if(course1course2)coutcourse2s credit larger than course1s.endl;elsecoutcourse2s credit do not large

15、r than course1s.endl;程序截图:56、源程序:class COOP : public CCourseprivate:char *p_openby;public:bool select(const char *p_xh)if(strncmp(p_xh,2010,4)=0)return true;else return false;COOP(long n, char *na, float c, char *p_open) : CCourse(n,na,c) p_openby = new charstrlen(p_open)+1;strcpy(p_openby, p_open);

16、COOP(const COOP &coop)p_openby=new charstrlen(coop.p_openby)+1;if(p_openby=NULL)exit(0);strcpy(p_openby,coop.p_openby);COOP() delete p_openby; void print()CCourse:print();cout开课单位:p_openbyendl;void main()char stno20;COOP coop1(2011103,面向对象程序设计,4.0,计算机与软件设计学院);coop1.print();cout请输入学号:stno;if(coop1.se

17、lect(stno)cout可以选课endl;elsecout不能选课endl;程序截图:7、要实现动态联编成员函数必须声明为virtual,如果基类中声明了为虚函数,则派生类中不必再声明。在class CCourse定义中增加:public:virtual bool select(const char *p_xh)if(strncmp(p_xh,2010,4)=0)return true;else return false;五、实验总结: (实验中遇到的困难和问题,如何解决的,在完成整个实验过程中的体会与感想。)碰到最多的就是函数参数格式用错使编译不通过,百度、查阅资料能很好地解决。有时候不是语法问题,而是格式不正确导致编译频频出错,需要我们在编写每一条语句时细心,检查错误时仔细查看,适当利用注析符分段检查。六、诚信声明:以上实验过程和结果均为本人独立完成,特签字承诺: 指导教师批阅意见:成绩评定: 指导教师签字: 年 月 日备注:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com