C++程序设计综合实验
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
深圳大学
实验报告
课程名称: ____________ 面向对象程序设计 _____________ 实验序号:_________________ 试验七___________________ 实验名称: _________ C++程序设计综合实验 _____________ 班级: __________________ 姓名:隔壁老王
学号:2010100001实验日期:2011年12月日
、实验目的:
(1) 掌握类和对象的实现;
(2) 掌握类的静态成员函数和友元函数的运用;
(3) 掌握类的继承与派生的编程特点;
(4) 掌握运算符承载的程序设计。
、实验环境:硬件环境:办公楼二楼软件实验室软件环境:Visual C++ 6.0 集成环境
三、实验要求:
1. 定义一个课程类CCourse,其中包含课程号(Iong no)、课程学分(float credit) 两个数据
成员,以及相应的构造函数、拷贝构造函数、析构函数和打印数据成员的成员函数print()。
2. 为CCourse类增加一个数据成员课程总数(int total_course),并增加一个成
员函数getTotalCourse()获取total_course的值,编写一个友元函数getCourseNo(获取课程号no。做如上修改后,重新实现CCourse类(与第1 问相同的不用再重复,注意说明数据成员和成员函数的存储类型,以便能够用类名来调用getTotalCourse()。
3. 为CCourse类定义小于运算符(‘<')运算符重载函数。CCourse类对象大小的比较是
根据其课程学分(credit)的值的大小来实现的(与第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( const char *p_xh)成
员函数,应该在Ccourse类及其派生类C00!中作何改动?
四、实验内容与结果: (源程序及运行截图)
1、class CCourse
{private: 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 char[20]; strcpy(p_name,"course name"); credit=0.0; }
CCourse::CCourse(long n,char *na,float c) { no=n;
p_name=new char[20]; strcpy(p_name,na);
credit=c; }
CCourse::CCourse(const CCourse &course)
{ p_name=new char[strlen(course.p_name)+1];
if(p_name==NULL) exit(0);
strcpy(p_name,course.p_name);
credit=course.credit; }
void CCourse::print()
{ cout<<"Course number:"< cout<<"Course name:"< cout<<"Course credit:"< CCourse::~CCourse(){ delete p_name;} 2、class CCourse {private: 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);}; int CCourse::total_course = 0; CCourse::CCourse() { no=0; p_name=new char[20]; strcpy(p_name,"course name"); credit=0.0; } CCourse::CCourse(long n,char *na,float c) { no=n; p_name=new char[20]; strcpy(p_name,na); credit=c; total_course++; } CCourse::CCourse(const CCourse &course) { p_name=new char[strlen(course.p_name)+1]; if(p_name==NULL) exit(0); strcpy(p_name,course.p_name); credit=course.credit; total_course++; } void CCourse::print() { cout<<"Course number:"< cout<<"Course name:"< cout<<"Course credit:"< } CCourse::~CCourse() { delete p_name; total_course--; }