主题:如何动态创建类的对象,以及delete用法?
关于动态创建,我的课本上只举出了创建动态数组的用法,刚好碰到作业有要求动态创建某个类的对象,郁闷得很。
#include "Student.h"
#include "StudentLiberalArts.h"
#include "StudentScience.h"
#include "iostream.h"
void main()
{
int count=4;
CStudent *pStu[4];
new CStudentLiberalArts;
new CStudentScience;
CStudentLiberalArts student0,student1;
student0.setStudent();
student1.setStudent();
pStu[0]=&student0;
pStu[1]=&student1;
CStudentScience student2,student3;
student2.setStudent();
student3.setStudent();
pStu[2]=&student2;
pStu[3]=&student3;
while(count>0)
{
count--;
pStu[count]->Average();
}
题中类的声明及相关函数已经声明实现,接下去如何使用delete语句?还是我之前的new用法有错误?
PS: 原题要求:
1) 定义一个CStudent的指针数组pStu,数组长度为4;
2) 动态创建2个CStudentLiberalArts的对象,地址存于pStu数组的0-1个元素内;
3) 动态创建2个StudentScience的对象,地址存于pStu数组的2-3个元素内;
4) 利用while循环调用数组里每个元素的计算平均成绩操作;
#include "Student.h"
#include "StudentLiberalArts.h"
#include "StudentScience.h"
#include "iostream.h"
void main()
{
int count=4;
CStudent *pStu[4];
new CStudentLiberalArts;
new CStudentScience;
CStudentLiberalArts student0,student1;
student0.setStudent();
student1.setStudent();
pStu[0]=&student0;
pStu[1]=&student1;
CStudentScience student2,student3;
student2.setStudent();
student3.setStudent();
pStu[2]=&student2;
pStu[3]=&student3;
while(count>0)
{
count--;
pStu[count]->Average();
}
题中类的声明及相关函数已经声明实现,接下去如何使用delete语句?还是我之前的new用法有错误?
PS: 原题要求:
1) 定义一个CStudent的指针数组pStu,数组长度为4;
2) 动态创建2个CStudentLiberalArts的对象,地址存于pStu数组的0-1个元素内;
3) 动态创建2个StudentScience的对象,地址存于pStu数组的2-3个元素内;
4) 利用while循环调用数组里每个元素的计算平均成绩操作;