array of class , array of objects

Array of Objects:-

An array whose elements are objects of class is called array of class or array of objects.It means each element of the array is an object of that class. In other words "An array of a class type is also known as an array of objects."
एक ऐरे जिसके एलेमेंट्स किसी क्लास के ऑब्जेक्ट हो,क्लास का ऐरे या ऑब्जेक्टस का ऐरे कहलाता है अर्थात ऐरे का प्रत्येक एलिमेंट क्लास का एक ऑब्जेक्ट होता है। दुसरे शब्दों में "क्लास के प्रकार का एक ऐरे,ऑब्जेक्टस का ऐरे कहलाता है।"     
Syntax:-

Declaration of an Array of objects:-
class_name array_name[size];
student s[100];

Accessing through array of objects:-
array_name[index].member_name;
s[i].name;

Program:-

#include<iostream.h>
#include<conio.h>
#define N 5
class student{
int rollno;
char name[20];
float marks;
int count;
public:
student(){
count=0;
}
void getstudent();
void setstudent();
};
void student::getstudent(){
cout<<"Student Detail:-"<<endl;
cout<<"Rollno="<<rollno<<endl<<"name="<<name<<endl<<"marks="<<marks<<endl<<"object count="<<count<<endl;
}
void student::setstudent(){
cout<<"Enter Student Details:-"<<endl;
cout<<"Enter Rollno"<<endl;
cin>>rollno;
cout<<"Enter name"<<endl;
cin>>name;
cout<<"Enter marks"<<endl;
cin>>marks;
count++;
}
void main(){
student s[N]; //array of objects or array of class
clrscr();
for(int i=0;i<N;i++)
s[i].setstudent();
for(int i=0;i<N;i++)
s[i].getstudent();
getch();
}

No comments:

Post a Comment