Image

C++ - Object - Object array

Object array in C++

C++ provides new concept object array by which we can create multiple copies of object.

e.g:
#include<iostream.h>
#include<conio.h>
const int MAX=10;
class Student
{
  private:
	  int sid;
	  char sname[20];
	  float per;
  public:
	  void set();
	  void show();
};
	  void Student::set()
	  {
	  cout<<"\nEnter Student id ";
	  cin>>sid;
	  cout<<"\nEnter Student name ";
	  cin>>sname;
	  cout<<"\nEnter Student Percentage ";
	  cin>>per;
	  }
	  void Student::show()
	  {
	  cout<<"\nStudent id         "<<sid;
	  cout<<"\nStudent Name       "<<sname;
	  cout<<"\nStudent Percentage "<<per;
	  }

void main()
{
 clrscr();
 int i,n;
 cout<<"\n Enter how many records you want to store ";
 cin>>n;
 Student s[MAX];
 for(i=0;i<n;i++)
 {
 s[i].set();
 }
 cout<<"\n Records are : ";
  for(i=0;i<n;i++)
 {
 s[i].show();
 }
 getch();
}
O/P
Enter record
2   
Student Id                Student Name        Student Percentage
101                             Swati                            60
102                             Vivek                            70