实验 析构函数
来源:互联网 发布:海岛奇兵医疗升级数据 编辑:程序博客网 时间:2024/06/11 02:27
实验 析构函数
1、实验目的
通过实验理解析构函数的概念与其特殊应用。
2、实验内容
应用VC++6.0的编辑环境构造一个类Student,该类主要实现学生的基本操作,该学生类包含学生姓名、学生学号、学生成绩(课程数目不定,其存储空间应动态申请),实现对该学生信息的初始化、求该学生的总成绩、平均成绩、最高分与最低分以及最后输出,具体说明如下:
class Student{
public:
Student(char *n,char *s,int num,double *s);
~Student();
double GetSum();
double GetAver();
double GetMax();
double GetMin();
void Show();
private:
char *name;//student name
char *stuno;//student no
int score_num;//课程数量
double *score;//存储学生课程成绩的数组,其长度由score_num决定
};
源代码:
#include<iostream.h>
#include<string.h>
class Student{
private:
char *name; //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 char[strlen(n)+1];
stuno=new char[strlen(m)+1];
strcpy(name,n);
strcpy(stuno,m);
score_num=num;
score=s;
}
~Student()
{
delete []name;
delete []stuno;
cout<<"析构"<<endl;
}
double GetSum()
{
double s=0;
for(int i=0; i<score_num; i++)
s = s+*(score+i);
return s;
}
double GetAver()
{
return GetSum()/score_num ;
}
double GetMax()
{
double max=*score;
for(int i=0; i<score_num; i++)
{
if(*(score+i)>=max)
max=*(score+i);
}
return max;
}
double GetMin()
{
double m=*score;
for(int i=0; i<score_num; i++)
{
if(*(score+i)<=m)
m=*(score+i);
}
return m;
}
void Show()
{
cout<<name<<endl
<<stuno<<endl
<<GetAver()<<endl
<<GetMax()<<endl
<<GetMin()<<endl;
return ;
}
};
void main()
{
double a[3]={50.0,80.0,60.0};
Student student001("stu001","001",3,a);
student001.Show();
return ;
}
- 实验 析构函数
- 实验三构造与析构函数
- 实验4 析构函数 对象数组与指针
- 实验名称: 类的构造函数、析构函数和友员成员应用
- 【c++】通过具体实验对比分析“构造函数”与“析构函数”
- 实验 构造函数
- 函数参数传递实验
- TCP_API接口函数实验
- SQL_substr函数实验
- c++第一次实验--函数
- c++实验7-函数
- 函数实验答疑
- 实验1 C++函数
- pthread_join()函数理解实验
- GetSystemInfo函数实验
- 实验c++实验2-分段函数求值
- 实验: 使用安全串函数
- 实验: 虚函数细节验证.
- Enterprise Library Security Application Block
- 如何开发绚丽、高效率的界面(Windows嵌入式系统)(一)
- 用notepad2替代记事本
- JVM原理学习笔记
- RMI for myeclipse资料
- 实验 析构函数
- PHP网站优化的6个基础方法
- 传智播客学习笔记-OA工作流-jbpm笔记
- JMail发送附件
- Flex开源项目集锦
- 通过oracle,调用java类,并加载jar包到oracle中以支持java类
- c/c++文件读写操作总结
- 传智播客学习笔记-jbpm笔记2
- md5sum的使用