Array of Objects:It is an array whose elements are of the class type. It can be declared as an array of any datatype.
Syntax:
classname array_name [size];
Below is the C++ program to illustrate the array of objects by calculating the highest marks among 3 students:
Output:
👁 Image
:
- In the main() function, objects of the Student class are created:
- Here, the first array of objects is s[3] and the other one is s1(a simple object).
- In the for loop, 3 sets of user input have been taken, (i.e, it is where the user will be entering the name, roll, total marks, for 3 students sets).
- Then passing the s[3] (array of an object which contains the sets of student details) and its size, through the s1 object in the pos(Student obj [], int size) member function.
- The pos(Student obj [], int size) function, is returning the position of the object of the highest total marks scoring student set, i.e, (0, 1or 2 index position of s[3] object array), which is stored in pos = s1.pos(s, 3).
- Display part: For calling the display function, S[pos].putdata() is used.
- The putdata() function is displaying the object details of the student class.
- Here, pos is sent (which stores the index position of the highest student set object) in s, to display the highest total marks scored student details.
Time Complexity: O(n)
Auxiliary Space: O(n)