Array Of Class Objects
An array of objects is frequently used to handle a group of objects that reside contiguously in the memory. Let consider the following class specification:
class student
{
Private :
int roll_no;
char name[20];
public :
void setdata(int roll_no_in,char *name_in);
void outdata();
};
The identifier student is a user-defined data type and can be used to create objects which associate to students of various courses. The subsequent definition creates an array of objects of the student class:
student science[10];
student medical [5];
student engg[25];
The array science holds ten objects, namely science [0]....science [9] of type student class, the medical array holds 5 objects and the engg array holds 25 objects.
An array of objects is stored in the memory in the similar way as a multidimensional array created at compile time. A representation of the objects is created; member functions are stored separately and shared through all the objects of student class.
An array of objects behaves same to any other data-type array. The individual component of an array of objects is referenced by using their index and member of an object is accesses using the dot operator. Example for, the statement.
Engg[i].setdata(10,"Rajkumar");
Sets the data members of the ith element of the array engg. As same, the statement
Engg[i].outdata();
Will show the data of the ith element of the array engg[i]. The program student1.cpp describes the use of the array of objects.
//student1.cpp: array of student data type
include <iostream.h>
#include<string.h>
class stduent
{
private:
int roll_no;
char name[20];
public:
void setdata(int roll_no_in,char *name_in)
{
roll_no = roll_no_in;
strcpy(name,name_in);
}
void outdata()
{
cout << "Roll No = " << roll_no <<endl;
cout << "Name = " <<name <<endl;
}
};
void main()
{
int i,roll_no,count;
char response,name[20];
student s[10]; count=0; for(i=0;i<10;i++)
{
cout << "Initialize object (y/n) :";
cin >> response;
if ( response =='y' || response =='Y')
{
cout << "Enter roll no. Of student :";
cin >> roll_no;
cout << "Enter name of student :";
s[i].setdata(roll_no,name);
count++;
} else
break;
}
cout << "student details ..."<<endl;
for(i=0;i<count;i++)
s[i].outdata();
}
Run
Initialize object (y/n) : y
Enter roll no. Of student : 1
Enter name of student : Rajkumar
Initialize object (y/n) : y
Enter roll no. Of student : 2
Enter name of student : Tejaswi
Initialize object (y/n) : y
Enter roll no. Of student : 3
Enter name of student : Savithri
Initialize object (y/n) : n
Student details ... Roll no = 1
Name = Rajkumar
Roll no = 2
Name = Tejaswi
Roll no = 3
Name = Savithri
In main(), the statement
Student s[10];
Creates an array of 10 possible objects of the student class. That should be clearly understood in which an array of objects permits better organization of the program instead of having 10 various variables and every one of them is the object of the student class. Remember that the subscripted notation used for object is same to the manner in which arrays of other data types are commonly handled. The statement
S[i].outdata();
Will Execute the outdata() member function in the student class for the ith object of the s array.