Array of Object in C++

array of object in C++ are based on two different concept such as array of structure, concept are follows-

The array of structure in C++ and array of object are both the same.

In the array of object in C++ each elements of an array becomes a object (same class’s), which means the array of object in c++ is a collection of objects of the same class. Just as an object is a collection of data-members and function-members. so each element of an array is a collection of data-member and function-member.

The array of object is important like if you want to store 10 student record, then you do not need to declare 10 object for each student. You can make the class object an array of element.

In array of object, make the object an array type variable. And because every element of the array will now be an object, these objects are accessed with index value just like a simple array. In this, the object name will be the same but due to the index value being different, the object will also be different.

as you can see the below diagram,


and we know that object is a collection of data member and function member so,


Here we have created an object by declaring an array of sizes. The greater the size of the array, the more objects will be created. Creating an object means that how many students he wants to store the record

Let’s try this in an example, where we store 2 student record-


class student
      // public member by default
          int roll_no;
          char name[20]; 

       public:               // public member declaration
          void get_record(void);
          void show_record(void);

void student::get_record() // get record by user
       cout<<"\nEnter roll no: ";
       cout<<"Enter Name : ";

void student::show_record()       // display record
       cout<<"\nRoll no: "<<roll_no;
       cout<<"\nName : "<<name;

void main()          // main function
       student obj[2];

       for(int i=0; i<2; i++)
              cout<<"Enter "<<i+1<<" student record: ";

       for(int j=0; j<2; j++)
              cout<<"\ndisplay "<<j+1<<" student record: ";

Enter 1 student record:
Enter roll no: 11
Enter Name : Rahul sherma
Enter 2 student record:
Enter roll no: 12
Enter Name : Rakesh sherma

DIsplay 1 student record:
Roll no: 11
Name : Rahul sherma
DIsplay 2 student record:
Roll no: 12
Name : Rakesh sherma


as two object (obj[0], obj[1]) will be created in this program. Which will store two student records-

First object obj[0] first student and another object obj[1] is storing another student record.

Here is the diagram below, that show how the records of these two students have been stored in the program-


If you want, you can store 10 students record instead of two by increasing the array of size, Remember here, the object name is the same (obj), but due to the index value ([value]) being different, they become different.

Did you notice

In array of object, we have used static memory method to store student’s record but we can also create object dynamically and store student’s record which will be better than static memory method.

previous- Class and object in C++

next- Friend function | Friend Class