I have removed a lot of extra code in program to simplify the problem. I have a Student Class that hold a Students Name & 5 grades. The Array class is supposed to hold a collection of Student objects. With the code below I can instantiate 2 Array objects and all of the appropriate constructors / destructors are called. If I add the Copy Method then the program complies, but then crashes when run. Any idea why A2 is not destroyed properly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
public:
explicit Student (const char * = "");
~Student ();
protected:
void LocalCopy (const char *);
private:
char * pChar;
size_t CharCount;
size_t MaxSize;
int Grades[5];
};
Student::Student(const char * p):CharCount(0), MaxSize(0), pChar(0)
{
cout << "Student Default Constructor Called." << endl;
for (int i = 0; i < 5; i++)
Grades[i] = 0;
LocalCopy(p);
cout << "Student Default Constructor Completed." << endl;
}
Student::~Student()
{
cout << "Student Destructor Called." << endl;
delete[] pChar;
cout << "Student Destructor Completed." << endl;
}
void Student::LocalCopy (const char * p)
{
CharCount = strlen (p);
MaxSize = CharCount;
pChar = new char [CharCount + 1];
strcpy (pChar, p);
}
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include "Student.h"
#include <memory.h>
using namespace std;
class Array
{
public:
Array();
~Array();
Array & Copy (const Array &);
protected:
private:
Student * pStudent[5];
int StudentCount;
int MaxStudents;
};
inline Array::Array()
{
cout << "Array Default Constructor Called." << endl;
for (int i = 0; i < 5; i++)
pStudent[i] = new Student;
cout << "Array Default Constructor Completed." << endl;
}
inline Array::~Array()
{
cout << "Array Destructor Called." << endl;
for (int i = 0; i < 5; i++)
delete pStudent[i];
cout << "Array Destructor Completed." << endl;
}
inline Array & Array::Copy(const Array & A)
{
cout << "Copy Method Called." << endl;
return *this = A;
}
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include "Student.h"
#include "Array.h"
using namespace std;
int main()
{
Array A1;
Array A2;
//A1.Copy(A2);
return 0;
}
|
Output without calling the Copy Method
Array Default Constructor Called.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Array Default Constructor Completed.
Array Default Constructor Called.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Array Default Constructor Completed.
Array Destructor Called.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Array Destructor Completed.
Array Destructor Called.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Array Destructor Completed.
Press any key to continue . . .
Output using the Copy Method
Array Default Constructor Called.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Array Default Constructor Completed.
Array Default Constructor Called.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Student Default Constructor Called.
Student Default Constructor Completed.
Array Default Constructor Completed.
Copy Method Called.
Array Destructor Called.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Student Destructor Called.
Student Destructor Completed.
Array Destructor Completed.
Array Destructor Called.
Student Destructor Called.
//At this point I get the error that Lab6.exe has stopped working.
Press any key to continue . . .