Implement array of Objects using C++
Write a Program in C++ to implement array of Objects. Take the
user input of Name of player, Age and score. Sort the data based on the highest
Scores.
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class det
{
public:
int age,score;
char name[20];
void input();
void show();
void init();
};
void det::init()
{
age=0;
score=0;
char name1[20]="Blank";
strcpy(name,name1);
}
void det::input()
{
cout<<"Enter the Name: ";
cin>>name;
cout<<"Enter the Age: ";
cin>>age;
cout<<"Enter the score: ";
cin>>score;
}
void det::show()
{
cout<<" Name : "
<<name;
cout<<"\t Age :
" <<age;
cout<<"\t Score : " <<score
<<endl<<endl<<endl;
}
int
main()
{
int i,n=0,j,choice,index,count=0;
char ch;
det a[10],temp;
clrscr();
for(i=0;i<10;i++)
a[i].init();
do
{
cout<<" : : Menu : : ";
cout<<"\n1.Add\n2.Edit\n3.Delete\n4.Sort\n5.Print\n0.Exit"<<endl;
cout<<"Enter Choice: ";
cin>>choice;
switch(choice)
{
case 1:
ch=121;
for(i=n;i<=10;)
{
if(ch==121||ch==89)
{
a[i].input();
count++;
cout<<"Add more?(Y/N) :";
cin>>ch;
i++;
}
else
break;
}
n=count;
break;
case 2:
for(i=0;i<n;i++)
{
cout<<i+1;
a[i].show();
}
cout<<"Enter the Index to be
edited:";
cin>>index;
a[index-1].input();
cout<<"Updated \n\n";
break;
case 3:
for(i=0;i<n;i++)
{
cout<<i+1;
a[i].show();
}
cout<<"Enter the Index to be
deleted:";
cin>>index;
if ( index >= n+1 )
cout<<"Deletion not possible.\n";
else
{
for
( i = index - 1 ; i < n - 1 ; i++ )
a[i] = a[i+1];
n=n-1;
cout<<"Deleted\n\n";
break;
}
case 4:
for (i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (a[j].score>a[i].score)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"Sorted\n\n"<<endl;
break;
case 5:cout<<endl<<endl;
for(i=0;i<n;i++)
{
a[i].show();
}
break;
case 0:cout<<"Program
Terminated... Press Any key to Exit!";
break;
default : cout<<"Wrong Choice";
}
}while(choice!=0);
getch();
return 1;
}
Comments
Post a Comment