ARRAYS OF OBJECT IN C++
The following program illustrates the use of array of abject to store information of student.The program declared class student having data members rollno age height and weight,and two function to get and display this information.Since information of more than one student is stored, an array of object that is std[Max] is creadted
// array of calss objects
#include <iostream.h>
#include<conio.h>
#define.max 30
class student
{
private:
int rollno;
int age;
float height, weight;
public:
void grtinfo()
{
cout<<"roll no: ";
cin>> rollno;
cout<<"age: ";
cin>>age;
cout<<"Height:";
cin>>height;
cout<<"Weight:";
cin>>weight;
}
void disinfo()
{
cout<<endl;
cout<<"Roll no="<<rollno<< endl;
cout<<"age="<<age<<endl;
cout<<"Height="<<Height<<endl;
cout<<"Weight="<<Weight<<endl;
}
};
void main()
{
student std[max]; // array of object having max=30
int i,n;
cout<<"How many students?\n"<<endl;
cin>>n;
cout<<"enter the student details \n"<<endl;
for(i=0;i<n;i++)
{
cout<<endl;
std[i].getinfo();
}
cout<<"The list of student's is as follow \n";
for(i=0; i<n; i++);
std[i].disinfo();
getche();
}
You should get the result
How many student?
3
roll no:1
age :18
Height:134
Weight:45
roll no:4
age :20
Height:143
Weight:46
roll no:27
age :20
Height:147
Weight:50
The list of student's is as follows
roll no:1
age :18
Height:134
Weight:45
roll no:4
age :20
Height:143
Weight:46
roll no:27
age :20
Height:147
Weight:50
age :20
Height:147
Weight:50
The list of student's is as follows
roll no:1
age :18
Height:134
Weight:45
roll no:4
age :20
Height:143
Weight:46
roll no:27
age :20
Height:147
Weight:50
0 comments:
Post a Comment