2014210723李玉花实验报告五类与对象

上传人:今*** 文档编号:105827691 上传时间:2019-10-13 格式:DOC 页数:15 大小:109.51KB
返回 下载 相关 举报
2014210723李玉花实验报告五类与对象_第1页
第1页 / 共15页
2014210723李玉花实验报告五类与对象_第2页
第2页 / 共15页
2014210723李玉花实验报告五类与对象_第3页
第3页 / 共15页
2014210723李玉花实验报告五类与对象_第4页
第4页 / 共15页
2014210723李玉花实验报告五类与对象_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《2014210723李玉花实验报告五类与对象》由会员分享,可在线阅读,更多相关《2014210723李玉花实验报告五类与对象(15页珍藏版)》请在金锄头文库上搜索。

1、宁夏师范学院数学与计算机科学学院面向对象程序设计(C+)实验报告实验序号:5 实验项目名称:构造函数与析构函数 学号姓名专业班级2014级计算机科学与技术实验地点文科楼229指导教师马学梅时 间2015.5.21一、实验目的及要求1、掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数;2、掌握构造函数和析构函数的执行过程及调用顺序;3、掌握对象数组、对象指针的定义及使用方法;4、正确理解对象的赋值及对象的复制,掌握对象的赋值及复制方法;5、继续学习使用VC6.0的Debug调试功能,观察程序流程,跟踪观察类的构造函数、析构函数、成员函数的执行顺序二、实验设备

2、(环境)及要求硬件:PC(PII以上,128M以上内存)、因特网接入;软件:Windows XP操作系统或更高版本、Office2003或更高版本、Visual C+6.0。实验学时:2学时其他要求:对实验例题和例题操作步骤进行阅读,并实现;验证题目和设计题目尽量完成;任选一个设计题目写实验报告三、实验内容与步骤说明:对下述实验内容给出实验结果并分析总结。【验证题目】1 验证教材第9章例题 9.2至9.10第九章的例二#includeusing namespace std;class Boxpublic:Box(int,int,int);int volume();private:int hei

3、ght;int width;int length;Box:Box(int h,int w,int len)height=h;width=w;length=len;int Box:volume()return(height*length*width);int main()Box box1(12,25,30);coutThe volume of box1 isbox1.volume()endl;Box box2(15,30,21);coutThe volume of box2 isbox2.volume()endl;return 0;运行结果:9.3#include using namespace

4、 std;class Boxpublic:Box();Box(int h,int w,int len):height(h),width(w),length(len)int volume();private:int height;int width;int length;Box:Box()height=10;width=10;length=10;int Box:volume()return(height*width*length);int main()Box box1;coutThe volume of box1 isbox1.volume()endl;Box box2; coutThe vol

5、ume of box2 isbox2.volume()endl;return 0;9.4#includeusing namespace std;class Boxpublic:Box(int h=10,int w=10,int len=10);int volume();private:int height;int width;int length;Box:Box(int h,int w,int len)height=h;width=w;length=len;int Box:volume()return(height*width*length);int main()Box box1;coutTh

6、e volume of box1 is box1.volume()endl;Box box2(15);coutThe volume of box2 is box2.volume()endl;Box box3(15,30);coutThe volume of box3 is box3.volume()endl; Box box4(15,30,20);coutThe volume of box4 is box4.volume()endl;return 0; 9.5#include#includeusing namespace std;class Studentpublic:Student(int

7、n,string nam,char s)num=n;name=nam;sex=s;coutConstructor called.endl;Student()coutDestructor called.endl;void display()coutnum:numendl;coutname:nameendl;coutsex:sexendlendl;private:int num;string name;char sex;int main()Student stud1(10010,Wang_li,f);stud1.display();Student stud2(10011,Zhang_fang,m)

8、;stud2.display();return 0;9.6#includeusing namespace std;class Boxpublic:Box(int h=10,int w=12,int len=15):height(h),width(w),length(len)int volume();private:int height;int width;int length;int Box:volume ()return(height*width*length);int main()Box a3=Box(10,12,15),Box(15,18,20),Box(16,20,26),;coutv

9、olume of a0 isa0.volume()endl;coutvolume of a1 isa1.volume()endl;coutvolume of a2 isa2.volume()endl;return 0;9.7#includeusing namespace std;class Timepublic:Time(int,int,int);int hour;int minute;int sec;void get_time();Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void Time:get_time ()couthour:m

10、inute:secendl;int main()Time t1(10,13,56);int *p1=&t1.hour;cout *p1get_time();void(Time:*p3)();p3=&Time:get_time;p3=&Time:get_time;(t1.*p3)();return 0;9.8#includeusing namespace std;class Timepublic:Time(int,int,int);int hour;int minute;int sec;Time:Time(int h,int m,int s)hour=h;minute=m;sec=s;void

11、fun(Time &t)t.hour=18;int main()Time t1(10,13,56);fun(t1);coutt1.hourendl;return 0;9.9#includeusing namespace std;class Boxpublic:Box(int=10,int=10,int=10);int volume();private:int height;int width;int length;Box:Box(int h,int w,int len)length=h;width=w;length=len;int Box:volume()return(height*width

12、*length);int main()Box box1(15,30,25),box2;coutThe volume of box1 isbox1.volume()endl;box2=box1;coutThe volume of box2 isbox2.volume()endl;return 0;在运行的过程中出现了以下的错误,导致运行结果也出现了错误。其运行结果是:no relevant changes detected/没有相关的变化检测9.10的程序:#includeusing namespace std;class Boxpublic:Box(int,int);int volume();static int height;int width;int length;Box:Box(int w,int len)width=w;length=len;int Box:volume()return(height*width*length);int Box:hei

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

最新文档


当前位置:首页 > 高等教育 > 大学课件

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