Answer to Question #194023 in C++ for Sankalp

Question #194023

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


1
Expert's answer
2021-05-16T14:40:20-0400
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Player{
    int code, runs, played, innings, times_not_out;
    string name;
    public:
    Player(){}
    Player(int c, int r, int p, int in, int t, string s){
        code = c;
        runs = r;
        played = p;
        innings = in;
        times_not_out = t;
        name = s;
    }
    int getCode(){
        return code;
    }
    void displayruns(){
        cout<<name<<endl;
        cout<<runs<<endl<<endl;
    }
};
class Players{
    Player *players[10];
    int size = 0;
    public:
        Players(){
        }
        void addPlayer(int c, int r, int p, int in, int t, string s){
            if(size == 10){
                cout<<"\nMaximum number of players reached\n";
                return;
            }
            for(int i = 0; i < size; i++) if(players[i]->getCode() == c){
                cout<<"\nPlayer code already exists\n";
                return;
            }


            players[size] = new Player(c, r, p, in, t, s);
            size++;
        }
        void display(int code){
            for(int i = 0; i < size; i++){
                if(players[i]->getCode() == code){
                    players[i]->displayruns();
                    return;
                }
            }
            cout<<"\nPlayer not found!\n";
            return;
        }
        void display(){
            for(int i = 0; i < size; i++) players[i]->displayruns();
        }
};
int main(){
    int choice;
    int code, runs, played, innings, times_not_out;
    string name;
    Players player;
    do{
        cout<<"1. Enter details of players\n";
        cout<<"2. Display average runs of a single player.\n";
        cout<<"3. Average runs of all players\n";
        cin>>choice;
        switch(choice){
            case 1: cout<<"Enter name: ";
                    cin>>name;
                    cout<<"Enter player code: ";
                    cin>>code;
                    cout<<"Enter runs: ";
                    cin>>runs;
                    cout<<"Enter innings: ";
                    cin>>innings;
                    cout<<"Enter times not out: ";
                    cin>>times_not_out;
                    cout<<"Enter played games: ";
                    cin>>played;
                    player.addPlayer(code, runs, played, innings, times_not_out, name);
                    break;
            case 2: cout<<"Enter player code: ";
                    cin>>code;
                    player.display(code);
                    break;
            case 3: player.display();
                    break;
            default: break;
        }
    }while(choice < 4 && choice > 0);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS