Dynamic Array Using Class

Hi all,

I am trying to do a little review with classes and pointers. The program that I am working on deals with the grades of the students in a class. What I would like to do is create a record for each student. The algorithm or "idea" I have for this is to use a double pointer to create an array of pointers that in turn point to each student record. Each student record will have member variables for 2 quizzes, a midterm and a final exam. I am not real sure if I am doing this correct. Any and all help is greatly appreciated. Thank you for your time.

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
#include <iostream>

using namespace std;

class StudentRecord
{
	//To be filled at a later time
	public:
		
	private:

};

int main()
{
	char ch;
	int numOfStudents;
	StudentRecord **students;

	cout << "Enter the number of students in the class: ";
	cin >> numOfStudents;
	cout << endl;

	students = new StudentRecord*[numOfStudents];
	for(int i = 0; i < numOfStudents; i++)
		students[i] = new StudentRecord[1];




	cin.get(ch);
	return 0;
}
The algorithm or "idea" I have for this is to use a double pointer to create an array of pointers that in turn point to each student record.

Yes, you're doing it correctly, if that is what you want, but my question is why the array of pointers? It seems totally unnecessary. Just allocate an array of students and be done with it.

Better yet, use a std::vector.

BTW, don't forget to deallocate both your array of pointers and the individual student records, otherwise you have a memory leak.

Topic archived. No new replies allowed.