Passing array of objects as parameter in C++ Last Updated : 20 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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: C++ // C++ program to illustrate the // passing the array of objects // to function parameter #include <bits/stdc++.h> using namespace std; // Class Student class Student { int roll; char name[50]; int total; public: // Function to input Roll Number void getdata() { cout << "Enter your Roll: " << endl; cin >> roll; cout << "Enter your Name: " << endl; cin.ignore(); cin.get(name, 50); cout << "Enter your Total " << "Marks: " << endl; cin >> total; } // Function to pass the array of // objects int pos(Student obj[], int size) { int pos = 0; int max = obj[0].total; // Traverse the array of object for (int i = 0; i < size; i++) { if (obj[i].total > max) { max = obj[i].total; pos = i; } } return pos; } // Function to display the students // details void putdata() { cout << "Roll: " << roll << endl; cout << "Name: " << name << endl; cout << "Total Marks: " << total << endl; } }; // Function that have array of objects void arrayOfObjects() { Student s[3], s1; int pos; for (int i = 0; i < 3; i++) { s[i].getdata(); } pos = s1.pos(s, 3); cout << "Highest scoring Student" << " Details:" << endl; s[pos].putdata(); } // Driver Code int main() { // Function Call arrayOfObjects(); return 0; } Output: Explanation: 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) Comment More infoAdvertise with us Next Article Passing array of objects as parameter in C++ D debojyotidas9294 Follow Improve Article Tags : C++ Programs C++ DSA Arrays cpp-parameter-passing C++-Class and Object +2 More Practice Tags : CPPArrays Similar Reads Passing a Function as a Parameter in C++ Passing a function as an argument is useful in dynamically changing the behaviour of the function. This concept has already been used while passing a custom comparator function as an argument in sort() function to sort a sequence of objects as per the need.Function that is passed as argument is call 4 min read Passing and Returning Objects in C++ In C++ we can pass class's objects as arguments and also return them from a function the same way we pass and return other variables. No special keyword or header file is required to do so. Passing an Object as argument To pass an object as an argument we write the object name as the argument while 4 min read Const Reference vs Normal Parameter Passing in C++ In C++, when we call a function we can also pass the parameters to the function in many ways like pass by value, pass by reference, pass by pointer, and by const reference. Each parameter-passing technique has its own advantages and disadvantages. In this article, we will learn the difference betwee 4 min read How to add reference of an object in Container Classes We all are familiar with an alias in C++. An alias means another name for some entity. So, a reference variable is an alias that is another name for an existing variable/object etc. Below is the program for adding reference to a variable: CPP // C++ program to illustrate // aliasing in variable #inc 4 min read How to Sort a Vector of Custom Objects in C++? In C++, vectors are dynamic arrays and sorting a vector of custom objects means arranging the objects in a certain order. In this article, we will learn how to sort a vector of custom objects in C++. Sorting a Vector of Custom Objects in C++To sort a vector of custom objects in C++, we can use the s 2 min read Like