array of object in C++

an array of object in C++ is based on two different concepts such as an array of structure, the concept follows,


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

In the array of object in C++, each element of an array becomes an 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 as if you want to store 10 student record, then you do not need to declare 10 objects for each student. You can make the class object an array of an element.

In an 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,

array-of-object-in-cpp-with daigram

 

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

cpp-array-of-object-devHere 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-

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

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: ";
       cin>>roll_no;
       cout<<"Enter Name : ";
       gets(name);
}

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

int main()          // main function
{
  
       student obj[2]; // 2 students

       for(int i=0; i<2; i++)
      {
              cout<<"Enter "<<i+1<<" student record: ";
              obj[i].get_record();
      }

       for(int j=0; j<2; j++)
      {
              cout<<"\ndisplay "<<j+1<<" student record: ";
              obj[j].show_record();
}
return 0;
}

we can also use the following code instead of a loop, but its only fine for 1-2 statements. such as,

student obj[2];

obj[0].get_record(); //1st student
obj[0].get_record(); //2nd student

obj[1].show_record(); //1st student
obj[1].show_record(); //2nd student

Whereas we have to use the loop when the statement is incremented. You can understand the above program with the help of the following diagram –

array-of-object in cpp

the above code will give the following result.

OUTPUT:-

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

Explanation:-

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

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

more about the array of object

previous- class and object in C++

next- friend function in C++

Like it?