Write necessary class and member function definitions for a cricket player object. (Use array of objects).
The program should accept details from user (max 10) : player code, name, runs, Innings, played, number of times not out.
The program should contain following menu:-
Enter details of players.
Display average runs of a single player.
Average runs of all players
#include<iostream>
#include<conio.h>
using namespace std;
class cricket
{
public:
double average;
int playerCode,runs,numberOfInnings;
int numberOfTimesNotOut;
char playerName[20];
public:
void get()
{
cout<<"\nEnter the details of player: \n";
cout<<"Playercode Name Runs NumberOfInnings NumberOfTimesNotOut. (Separate each with space) \n";
cin>>playerCode>>playerName>>runs>>numberOfInnings>>numberOfTimesNotOut;
}
void cal()
{
average=(runs / numberOfInnings);
}
void display(int n)
{
cout<<"\nDetails of single player: \n";
cout<<"\nplayerCode\tname\trun\tno.Inning\tnot out\tAve.run\n";
cout<<"==========================================================\n";
cout<<n<<"\t"<<playerName<<"\t"<<runs<<"\t"<<numberOfInnings<<"\t\t"<<numberOfTimesNotOut<<"\t"<<average;
}
void display()
{
cout<<playerCode<<"\t"<<playerName<<"\t"<<runs<<"\t"<<numberOfInnings<<"\t"<<numberOfTimesNotOut<<"\t"<<average<<endl;
}
};
int main()
{
int n,i,ch;
cricket c[10];
//clrscr();
do
{
cout<<"\n\tOPTIONS:\n";
cout<<"\n1.Enter Details \n2.Display single Player\n3.Display all Players\n4.Quit";
cout<<"\n\nEnter youer Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter number of players to enter: \n";
cin>>n;
for(i=0;i<n;i++)
{
c[i].get();
c[i].cal();
}
break;
case 2:
cout<<"\nWhich player Average run display: ";
cout<<"\nEnter the player code: \n";
cin>>n;
for(i=0;i<n;i++)
{
if(c[i].playerCode==n)
{
c[i].display(n);
}
}
break;
case 3:
cout<<"\nDetails of single player: \n";
cout<<"\nPlayer Code\tName\tRuns\tNo. of Inning\tNot out\tAve.run\n";
cout<<"===========================================\n";
for(i=0;i<n;i++)
{
c[i].display();
}
break;
case 4:
break;
}
}
while(ch!=4);
getch();
}
Comments
Leave a comment