Arrays of object doubt

Question :
2. Declare a class named ‘StudentRec’ with three private members: ‘enrolNo’ of type int, ‘CGPA’ of type float and ‘branch’ of type string. Declare an array of objects named ‘Student’ of size 5 of class ‘StudentRec’. Write public member functions: (i) void sort (StudentRec Student[], int N ) to sort the data in ascending order with respect to ‘CGPA’ and (ii) void print (StudentRec Student[], int N ) to display the sorted and unsorted students’ records. Write main to test these member functions.

Doubt. The sorting part I will do later. My doubt is if in the below code(line 29)"Student[5].print(Student, N );" is correct way to call the function print? Also Student[0].print(Student, N ) gives correct output. Why ?

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
#include<iostream>
#include<cstring>
using namespace std;

class StudentRec
{
public:
int enrolNo;
float CGPA;
string branch;
void sort (StudentRec Student[], int N );	
void print(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;
}
}
};

int main()
{
StudentRec Student[5];
int i,N=5;
for(i=0; i<5; i++)
cin>>Student[i].enrolNo>>Student[i].CGPA>>Student[i].branch;
Student[5].print(Student,  N );	
return 0;	
}
Last edited on
Student[5].print(Student, N );

Student[5] does not exist.

The array is of size 5; elements zero to four exist.

Student[0] does exist.
Student[1] does exist.
Student[2] does exist.
Student[3] does exist.
Student[4] does exist.
But this program is showing correct output. Why ?
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.

Last edited on
Have you had static class members yet?

These two member functions should logically be static. Then there would be no "called on" object at all.
Yes, static has been done. Understood. There can be 2 solutions
1. Create another object say 'a' of class Student Rec and then use a. print()
2. Use static int as told by you.

Topic archived. No new replies allowed.