0

Question : 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(2nd last line ) Student[5].print(Student, N ); is correct way to call the function print? How else can this function be called via array of objects Also Student[0].print(Student, N ) gives correct output. Why ?

#include<iostream>
#include<cstring>
using namespace std;

class StudentRec
{
    private:
        int enrolNo;
        float CGPA;
        string branch;
    public:
        void assign()
        {
            cin>>enrolNo>>CGPA>>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++)
        Student[i].assign();
    Student[5].print(Student,  N );
    return 0;
}
8
  • 2
    Student[5].print(Student, N ); invokes undefined behavior as your array only has a size of 5 (meaning the last valid index is 4) Commented Oct 25, 2017 at 9:05
  • There is no Student[5]. Has your course explained how basic array indexing works yet? Commented Oct 25, 2017 at 9:05
  • 3
    On a different note - why are sort and print non-static member functions? They don't rely on the instance Commented Oct 25, 2017 at 9:07
  • 1
    Student[0].print ... but your teacher dosen't know anything about programming and C++- Commented Oct 25, 2017 at 9:18
  • 1
    Yes. The better alternative to consider what you've been told about proper C++ practices, and how to alter the design to use them. But if you do, you'll have to hope your teacher doesn't mark you down for not following their terrible design... which would ultimately mean you were better at coding than them. Commented Oct 25, 2017 at 9:19

1 Answer 1

1

As has been pointed out, Student[5].print(Student, N ); invokes undefined behavior as there is no Student[5]. However, your implementation of print doesn't actually use the object it is invoked on, so this is probably why this works in practice.

To give your program a somewhat reasonable design while keeping as close to the assignment as possible, you can declare the functions static:

static void print(StudentRec Student[], int N );

This means that, while the functions are declared inside the class and have access to private members of objects of the class, they don't rely on any concrete object to be invoked. You can then use them like this:

StudentRec::print(Student, N);

On a side note, your implementation of print doesn't actually use the parameter N.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that should remain within the wording, as a static member function is still a member function. (It's a static method, not an instance method.) Note also that you can still call static methods on instances, e.g. someStudentRec.print(whatever, n) would still work, though it's probably not a good way to write it, since it implies an instance method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.