The class function you are calling does not in any way rely on the object it is being called on.
When you call the function print, on ANY student object, the same function code is run. The function looks something like this:
1 2 3 4 5 6 7 8 9
|
void print(Student* this, StudentRec Student[], int N )
{
int i;
for(i=0; i<5; i++)
{
cout<<"Student"<<" "<<i<<" " ;
cout<<Student[i].enrolNo<<" "<<Student[i].CGPA<<" "<<Student[i].branch<<endl;
}
}
|
You're not making use of the object
Student[5]
. The function being called does not rely on that object existing, so it never tries to access the object that doesn't exist.
The function has access to a pointer to the object you called it on; the
this pointer. The function happens not to make use of that pointer, so the class function in this case never tries to use the non-existent object, so nothing goes wrong.
This line of your code:
Student[5].print(Student, N );
could be thought of something like this:
Student::print(& Student[5], Student, N);
Because the function never uses the pointer to an object that doesn't exist, nothing goes wrong.