#include <iostream>
#include <stdlib.h>
using namespace std;
class Student
{
char name[20];
long int stuno;
int score;
public:
Student(char name1[]="",long int no=0,int sco=0)
{
strcpy(name,name1);
stuno = no;
score = sco;
}
~Student();
void scoreset(int n){score=n;}
void print(){cout << stuno << '\t'<<name << '\t'<<score << endl;}
};
Student::~Student()
{
cout << "学生:"<<name<<"的析构函数被调用"<< endl;
}
void main()
{
Student stu[5]={
Student("MA",5019001,94),
Student("HU",5019002,95),
Student("LI",5019003,88)
};
stu[3]=Student("ZHU",5019004,85);//这两句会调用一次析构函数,因为Student("ZHU",5019004,85)相当于创建一个无名对象再赋值给stu[3]
//这个无名对象需要释放掉
stu[4]=Student("LU",5019005,90);
stu[1].scoreset(98);
for(int i = 0; i<5;i++)
stu[i].print();
}
析构函数被调用顺序验证
最新推荐文章于 2024-01-18 20:25:01 发布