You are on page 1of 3

Array of Objects

============================================
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

class example{

public:

/* CONSTRUCTOR */

example()
{

id=0;
strcpy(name,"Unknown");
marks=0;

/* SETTERS */

void setId(int d)
{
id=d;
}

void setName(char *n)


{
strcpy(name,n);
}

void setMarks(float m)
{
marks=m;
}

/* GETTERS */

int getId()
{
return id;
}
char *getName()
{
return name;
}

float getMarks()
{
return marks;
}

private:

int id;
char name[50];
float marks;

};

/* MAIN */

void main()
{

example ob[5];
example hold;

int Id;
char Name[50];
float Marks;

for(int i=0,j=1; i<5,j<=5; i++,j++)


{

cout<<"Enter the ID of Student "<<j<<": ";


cin>>Id;
ob[i].setId(Id);

cout<<"\nEnter the Name of Student "<<j<<": ";


cin>>Name;
ob[i].setName(Name);

cout<<"\nEnter the Marks of Student "<<j<<": ";


cin>>Marks;
ob[i].setMarks(Marks);

system("cls");

for(int pass=0; pass<5-1; pass++)


for(int c=0; c<5-1;c++)
if(ob[c].getMarks()<ob[c+1].getMarks())
{

hold=ob[c+1];
ob[c+1]=ob[c];
ob[c]=hold;

system("cls");

cout<<"Top 3 Students are\n\n";

for(int k=0; k<3; k++)


{

cout<<"ID: ";
cout<<ob[k].getId()<<endl;
cout<<"Name: ";
cout<<ob[k].getName()<<endl;
cout<<"Marks ";
cout<<ob[k].getMarks()<<endl<<endl;

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006