C++程序设计综合实验

上传人:xins****2008 文档编号:111146499 上传时间:2019-11-01 格式:DOC 页数:7 大小:88KB
返回 下载 相关 举报
C++程序设计综合实验_第1页
第1页 / 共7页
C++程序设计综合实验_第2页
第2页 / 共7页
C++程序设计综合实验_第3页
第3页 / 共7页
C++程序设计综合实验_第4页
第4页 / 共7页
C++程序设计综合实验_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《C++程序设计综合实验》由会员分享,可在线阅读,更多相关《C++程序设计综合实验(7页珍藏版)》请在金锄头文库上搜索。

1、深 圳 大 学实 验 报 告课程名称: 面向对象程序设计 实验序号: 试 验 七 实验名称: C+程序设计综合实验 班 级: 姓 名: 丁驰明 学 号:2010300003 实验日期:2011 年12月 11日 教师签字: 一、实验目的:(1)掌握类和对象的实现;(2)掌握类的静态成员函数和友元函数的运用;(3)掌握类的继承与派生的编程特点;(4)掌握运算符承载的程序设计。二、实验环境:硬件环境:办公楼二楼软件实验室软件环境:Visual C+ 6.0 集成环境三、实验要求:1. 定义一个课程类CCourse,其中包含课程号(long 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_openby)和根据学生学号判断能否选课的成员函数bool select(const char *p_xh)(只有学号前4位为2010的学生可选面向对象程序设计课程)。写出COOP类的完整定义(包括构造、拷贝构造、析构和select()成员函数的实现)。6. 编写测试程序进行测试。7. 为了能够采用动态联编的方式调用派生类COOP的bool select(const char *

4、p_xh)成员函数,应该在Ccourse类及其派生类COOP中作何改动? 四、实验内容与结果:(源程序及运行截图)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 name);credit=0.0;CCou

5、rse: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 number:noendl;coutCourse name:p_n

6、ameendl;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();static getTotalCourse() return total_course; fr

7、iend 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_course+;CCourse:CCourse(const CCourse &course)p_name

8、=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()delete p_name;total_course-;long getCourseNo(const CCourse &co

9、urse)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 using namespace std;#include class CCourseprivate:long no;float credit;char

10、 *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);bool operator (const CCourse &course);bool CCourse:operator (const CCou

11、rse &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,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 cha

12、rstrlen(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()delete p_name;total_course-;int CCourse:total_course = 0;long getCours

13、eNo(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.print();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)cout

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号