收藏 分销(赏)

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

上传人:a199****6536 文档编号:10506206 上传时间:2025-05-31 格式:DOC 页数:7 大小:81.50KB
下载 相关 举报
C++程序设计综合实验.doc_第1页
第1页 / 共7页
C++程序设计综合实验.doc_第2页
第2页 / 共7页
点击查看更多>>
资源描述
深 圳 大 学 实 验 报 告 课程名称: 面向对象程序设计 实验序号: 试 验 七 实验名称: C++程序设计综合实验 班 级: 姓 名: 隔壁老王 学 号:2010100001 实验日期:2011 年12月 11日 教师签字: 一、实验目的: (1)掌握类和对象的实现; (2)掌握类的静态成员函数和友元函数的运用; (3)掌握类的继承与派生的编程特点; (4)掌握运算符承载的程序设计。 二、实验环境: 硬件环境:办公楼二楼软件实验室 软件环境:Visual C++ 6.0 集成环境 三、实验要求: 1. 定义一个课程类CCourse,其中包含课程号(long 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_openby)和根据学生学号判断能否选课的成员函数bool select(const char *p_xh)(只有学号前4位为2010的学生可选面向对象程序设计课程)。写出COOP类的完整定义(包括构造、拷贝构造、析构和select()成员函数的实现)。 6. 编写测试程序进行测试。 7. 为了能够采用动态联编的方式调用派生类COOP的bool select(const char *p_xh)成员函数,应该在Ccourse类及其派生类COOP中作何改动? 四、实验内容与结果:(源程序及运行截图) 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:"<<no<<endl; cout<<"Course name:"<<p_name<<endl; cout<<"Course credit:"<<credit<<endl; } 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:"<<no<<endl; cout<<"Course name:"<<p_name<<endl; cout<<"Course credit:"<<credit<<endl; } CCourse::~CCourse() { delete p_name; total_course--; } long getCourseNo(const CCourse &course) { return course.no; } 3、class CCourse { public: bool operator <(const CCourse &course); }; int CCourse::total_course = 0; bool CCourse::operator <(const CCourse &course) { if (credit < course.credit) return true; else return false; } 4、源程序: #include <iostream> using namespace std; #include <string> 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); bool operator <(const CCourse &course); bool CCourse::operator <(const CCourse &course) { if (credit < course.credit) return true; else return false; } 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:"<<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(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); cout<<"Total course:"<<c<<endl<<"course1's NO:"<<sc<<endl; if(course1<course2) cout<<"course2's credit larger than course1's."<<endl; else cout<<"course2's credit do not larger than course1's."<<endl; } 程序截图: 5-6、 源程序: class COOP : public CCourse { private: 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 char[strlen(p_open)+1]; strcpy(p_openby, p_open); } COOP(const COOP &coop) { p_openby=new char[strlen(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_openby<<endl; } }; void main() { char stno[20]; COOP coop1(2011103,"面向对象程序设计",4.0,"计算机与软件设计学院"); coop1.print(); cout<<"请输入学号:"<<endl; cin>>stno; if(coop1.select(stno)) cout<<"可以选课"<<endl; else cout<<"不能选课"<<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; } 五、实验总结: (实验中遇到的困难和问题,如何解决的,在完成整个实验过程中的体会与感想。) 碰到最多的就是函数参数格式用错使编译不通过,百度、查阅资料能很好地解决。 有时候不是语法问题,而是格式不正确导致编译频频出错,需要我们在编写每一条语句时细心,检查错误时仔细查看,适当利用注析符分段检查。 六、诚信声明: 以上实验过程和结果均为本人独立完成,特签字承诺: 指导教师批阅意见: 成绩评定: 指导教师签字: 年 月 日 备注:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服