南昌航空大学实验报告3析构函数_第1页
南昌航空大学实验报告3析构函数_第2页
南昌航空大学实验报告3析构函数_第3页
南昌航空大学实验报告3析构函数_第4页
南昌航空大学实验报告3析构函数_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.南昌航空大学实验报告2011年11月16日课程名称: 面向对象程序设计B 实验名称:构造函数班级:102022 姓名:林书晨指导老师评定: 签名:实验二 析构函数1. 实验目的 通过实验理解析构函数的概念与其特殊应用2. 实验内容 应用VC+6.0的编辑环境构造一个类student,该类主要实现学生的基本操作。该学生类包括学生姓名 学生学号 学生成绩(课程数目不定,起存储空间应动态申请),实现对该学生信息的初始化,求该学生的总成绩,平均成绩,最高分与最低分以及最后输出,具体说明如下: class Studentpublic:Student(char *n,char *s,int num,do

2、uble *s);Student();double GetSum();double GetAver();double GetMax();double GetMin();void Show();private:char *name;/student namechar *stuno;/student noint score_num;/score number Double *score; /student score;3. 概要设计a. 析构函数 class Studentpublic:Student(char *n,char *s,int num,double *s);Student();dou

3、ble GetSum();double GetAver();double GetMax();double GetMin();void Show();private:char *name;/student namechar *stuno;/student noint score_num;/score number Double *score; /student score;b.实现功能1. double GetSum() 求总成绩 double s=0; for(int i=0; iscore_num; i+) s = s+*(score+i); return s; 2.double GetAv

4、er() 求平均成绩 return GetSum()/score_num ; 3.double GetMax() 求最高分数 double max=*score; for(int i=0; i=max) max=*(score+i); return max; 4.double GetMin() 求最低分数 double m=*score; for(int i=0; iscore_num; i+) if(*(score+i)=m) m=*(score+i); return m; 4. 详细设计 #include #includeclass Studentprivate: char *name;

5、/student name char *stuno; /student no int score_num; /课程数量 double *score; /存储学生课程成绩的数组,其长度由score_num决定public: Student(char *n,char *m,int num,double *s) name=new charstrlen(n)+1; stuno=new charstrlen(m)+1; strcpy(name,n); strcpy(stuno,m); score_num=num; score=s; Student() delete name; delete stuno;

6、 cout析构endl; double GetSum() 求总成绩 double s=0; for(int i=0; iscore_num; i+) s = s+*(score+i); return s; double GetAver() 求平均成绩 return GetSum()/score_num ; double GetMax() 求最高分数 double max=*score; for(int i=0; i=max) max=*(score+i); return max; double GetMin() 求最低分数 double m=*score; for(int i=0; iscor

7、e_num; i+) if(*(score+i)=m) m=*(score+i); return m; void Show() coutnameendl stunoendl GetAver()endl GetMax()endl GetMin()endl; return ; ; void main() double a3=50.0,80.0,60.0; Student student001(stu001,001,3,a); student001.Show(); return ;5. 程序调试a.(1) 未初始化函数(2) 调试加入初始化函数(3) 程序可运行(4) 输入数据(5) 结果正确b.运

8、行结果6.实验总结在实验中其实程序的写入并不难,只是我们应该注意一些具体的细节,比如函数的初始化,函数的具体方法等等。7.附录 #include#includeclass Studentprivate: char *name; char *stuno; int score_num; double *score; public: Student(char *n,char *m,int num,double *s) name=new charstrlen(n)+1; stuno=new charstrlen(m)+1; strcpy(name,n); strcpy(stuno,m); score_

9、num=num; score=s; Student() delete name; delete stuno; cout析构endl; double GetSum() double s=0; for(int i=0; iscore_num; i+) s = s+*(score+i); return s; double GetAver() return GetSum()/score_num ; double GetMax() double max=*score; for(int i=0; i=max) max=*(score+i); return max; double GetMin() double m=*score; for(int i=0; iscore_num; i+) if(*(score+i)=m) m=*(score+i); return m; void Show() coutnameendl stunoendl GetAve

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论