How do I count the indexes of a dynamic class array

So, our professor tasked us with designing a code that will, basically, have a class "Course". This class will include a pointer char array CourseName, along with an object from the class "Instructor", and then multiple objects from the class "Students". The question states that each object of the class must have a maximum of 30 students, and that we also need to implement a function that should count how many students there are in the course object. All of this led me to assume that we have to create a dynamic class array for the "Student" objects, and that its volume will be determined by the user who will pass an integer value through a parameterized constructor for the class.

My question exactly is, how do I count the number of students? Doing this with c-strings is easy because you can use the '\0' at the end as a condition, but I am not sure how to do this with a dynamic class array.

This is what comes to mind when I think of counting the indexes of a dynamic class array:

1
2
3
4
5
6
7
8
  
int temp =0;

while(S[temp] != "\0")
{
  temp++
}


Is there any better way to implement this? I have yet to test it out.
Something like this perhaps; you know the maximum number of students, so use that.
1
2
3
4
5
6
class Course {
  const char *courseName;
  Instructor  instructor;
  Student     students[30];
  int         numStudents;
};


Or this, except you allocate the student array yourself.
1
2
3
4
5
6
class Course {
  const char *courseName;
  Instructor  instructor;
  Student     *students;
  int         numStudents;
};


Or this, and use a vector to do all the allocation tracking and counting for you.
1
2
3
4
5
class Course {
  std::string courseName;
  Instructor  instructor;
  vector<Student> students;
};
If you know about std::vector (or can use them) then that would be the easier.
Why pointer array for CourseName and not std::string??

and that we also need to implement a function that should count how many students there are in the course object


This wouldn't usually be done that way. You'd maintain a running count of how many students had been added/removed. The function would then simply return the count value.
seeplus, the issue is that we're currently not allowed to use strings at all. And I have yet to study vectors.
It was assigned to us by our teacher in college so I can't really do much here.

Also, the problem is that the code also SPECIFICALLY mentions what attributes are allowed to be kept in the class. So, there is pretty much no workaround from this, I need to find a way to count the students in a specific function, which, again, they asked us specifically to do. I honestly don't know why they don't let us experiment with stuff like this, but then again we only just started studying inheritance and has-a relations so... idk.
OK. But bare in mind that this isn't how you'd normally code it. A possible class construct could be (with appropriate constructors etc):

1
2
3
4
5
class Course {
	const char* courseName;
	Instructor  instructor;
	Student     students[30];
};


Assuming that class Student has a member called name (and is public or has a public getter and is a pointer and not an array), then to count the number of students then something like:

1
2
3
4
5
6
7
8
9
public:
	size_t num_Studs() const {
		size_t cnt {};

		for (size_t i {}; i < 30; ++i)
			cnt += students[i].name != nullptr;

		return cnt;
	}



Last edited on
Topic archived. No new replies allowed.