版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、深圳大学实 验 报 告课程名称: 面向对象程序设计实验序号:试验七实验名称: C+程序设计综合实验班 级:姓 名:隔壁老王学 号:2010100001实验日期:2011年12月日、实验目的:(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(
4、const 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_nam
5、e,"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(p_name=NULL) exit(0);strcpy(p_name,course.p_name); credit=course.credit; void CCo
6、urse:print() cout<<"Course number:"<<no<<endl; cout<<"Course name:"<<p_name<<endl; cout<<"Course credit:"<<credit<<endl; CCourse:CCourse() delete p_name;2、class CCourseprivate: long no; float credit; char *p_name;sta
7、tic 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 char20; s
8、trcpy(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 charstrlen(course.p_name)+1;if(p_name=NULL)exit(0); strcpy(p_name,course.p_name); cre
9、dit=course.credit; total_course+; void CCourse:print() cout<<"Course number:"<<no<<endl; cout<<"Course name:"<<p_name<<endl; cout<<"Course credit:"<<credit<<endl;CCourse:CCourse() delete p_name; total_course-;elselon
10、g 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 <iostream&g
11、t;using namespace std;#include <string>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 getCourseN
12、o(const CCourse &course);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,ch
13、ar *na,float c)no=n;p_name=new char20;strcpy(p_name,na);credit=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()cout<<"Course number:
14、"<<no<<endl;cout<<"Course name:"<<p_name<<endl; cout<<"Course credit:"<<credit<<endl;CCourse:CCourse()delete p_name;total_course-;int CCourse:total_course = 0;long getCourseNo(c onst CCourse &course)retur n course .no;void
15、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();CCourse course4(2011103,"面向对象程序设计",4.0);course4.pri nt();c=course4.getTo
16、talCourse(); sc=getCourseNo(course1); cout<<"Totalcourse:"<<c<<e ndl<<"course1'sNO:"<<sc<<e ndl;if(course1<course2)cout<<"course2's credit larger than course1's."<<e ndl;elsecout<<"course2's c
17、redit do notlarger tha n course1's."<<e ndl;CoUFse nurkbeF:20111O0Course name : 尊数字CoLirse crediCourse n unber:2011101Course name =大学英i吉Course credit .5CoLirse number:2011102Co Lir U nane二线性代那Course credit = 35Course nunber:2S11103Courve n盘me :血向对彖程序设计Course credit=4Tntal courseMci
18、87;ursel3 a NO:20111QcouFse2J s credit do not larger than counsel1 s. Press u keu to continue_5-6、源程序:class COOP : public 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
19、 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( 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 nby<<e ndl;void mai n()程序截图:char stn o20;COOP coop1(2011103,"面向对象程序设 计",4.0,"计算机与软件设计学院”);coopl.pri nt();cout<<"请输入学号:"<<endl; cin> >st no;if(coop1.select(st no)cout<<"可以选课"<<endl;elsecout<<
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- AI在区块链技术优化中的应用
- 2026年痰湿质人群健脾祛湿防感法
- 2026年企业文化建设与职业道德融合
- 2026年医疗数据安全培训提升员工意识
- 2026年人工智能时代民办职业教育专业调整
- 2026年结核病密切接触者筛查与管理
- 2026年实验室 5G 与物联网技术融合应用
- 上海立达学院《Android 移动平台开发》2025-2026学年第一学期期末试卷(A卷)
- 上海立信会计金融学院《安装工程计量计价》2025-2026学年第一学期期末试卷(B卷)
- 上海立信会计金融学院《安全防范系统工程》2025-2026学年第一学期期末试卷(A卷)
- 光纤激光毛化技术说明
- GB/T 4140-2003输送用平顶链和链轮
- GB/T 39844-2021可靠性增长统计试验和评估方法
- GB/T 20641-2014低压成套开关设备和控制设备空壳体的一般要求
- GB/T 13454.2-2013塑料粉状三聚氰胺-甲醛模塑料(MF-PMCs)第2部分:试样制备和性能测定
- 2023年绵阳市林业系统事业单位招聘笔试模拟试题及答案解析
- 部编小学音乐六年级《卡普里岛》课件-一等奖新名师优质公开课获奖比赛人教
- 计算流体力学CFD课件
- 作文与预测-范文gre讲义
- 昆虫生态及预测预报
- 天线与电波传播:第十四讲 常用面天线
评论
0/150
提交评论