Coupon Accepted Successfully!


Array of Objects

Arrays are collection of similar data types. Arrays can be of any data type including user-defined data type created using struct, class, and typedef declarations. We can also create an array of objects. The array elements are stored in continuous memory locations as shown in Figure. Consider the following example.

The player is a user-defined data type and can be used to declare an array of object of type player. Each object of an array has its own set of data variables.


Arrays of objects

As shown above, arrays of object of type player are created. The array cricket[5] contains name and age information for five objects. The next two declarations can maintain the same information for other player in arrays hockey[5] and football[5]. These arrays can be initialized or accessed like an ordinary array. The following program describes the working of array of objects.

8.21 Write a program to declare the array of objects. Initialize and display the contents of arrays.


Explanation: In the above program, the member function input() reads information of players. The display() function displays information on the screen. In function main() the statement player cricket[3]; creates an array cricket[3] of three objects of type player. The for loops are used to invoke member function input() and display() using array of objects.

Test Your Skills Now!
Take a Quiz now
Reviewer Name