实验 析构函数

来源:互联网 发布:海岛奇兵医疗升级数据 编辑:程序博客网 时间: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 ;
}

 

原创粉丝点击