Answer to Question #216233 in C++ for asad

Question #216233

Write a program having following classes

Player (Base Class)

Members:

  • Show_Stats()
  • Other class variables and functions

Cricket player (Derived Class)


Members:

  • Show_Stats
  • Other class variables and functions

Hockey player (Derived Class)

Members:

  • Show_Stats
  • Other class variables and functions

Football player (Derived Class)

Members:

  • Show_Stats
  • Other class variables and functions


  • In the main function, make objects of each derived class and use their functions to show players statistics (e.g. for cricket player: Runs scored, Wicket taken etc. Similarly for Football, Goals scored etc)
  • Common variables/functions should be part of base class and additional members of each derived class will be part of them




1
Expert's answer
2021-07-11T14:33:43-0400
#include <iostream>
#include <string>


using namespace std;




class Player{
private:
	int age;
	string name;
	int numberMatches;
	int numberRuns;
public:
	Player(){}
	Player(int age,string name,int numberMatches,int numberRuns){
		this->age=age;
		this->name=name;
		this->numberMatches=numberMatches;
		this->numberRuns=numberRuns;
	}


	virtual void Show_Stats(){
		cout<<"Age: "<<age<<"\n";
		cout<<"Name: "<<name<<"\n";
		cout<<"Matches: "<<numberMatches<<"\n";
		cout<<"Runs: "<<numberRuns<<"\n";
	}
};


class Cricket:public Player{
private:
	int wickets;
public:
	Cricket(int age,string name,int numberMatches,int numberRuns,int wickets):Player(age,name,numberMatches,numberRuns){
		this->wickets=wickets;
	}


	void Show_Stats(){
		cout<<"Cricket Player\n";
		Player::Show_Stats();
		cout<<"Wickets: "<<wickets<<"\n";
	}
};
class Hockey:public Player{
private:
	string NAT;
	string postion;
public:
	Hockey(int age,string name,int numberMatches,int numberRuns,string NAT,string postion):Player(age,name,numberMatches,numberRuns){
		this->NAT=NAT;
		this->postion=postion;
	}
	void Show_Stats(){
		cout<<"Hockey Player\n";
		Player::Show_Stats();
		cout<<"NAT: "<<NAT<<"\n";
		cout<<"Postion: "<<postion<<"\n";
	}
};


class Football:public Player{
private:
	int goals;
	int shots;
	int passes;
public:
	Football(int age,string name,int numberMatches,int numberRuns,int goals,int shots,int passes):Player(age,name,numberMatches,numberRuns){
		this->goals=goals;
		this->shots=shots;
		this->passes=passes;
	}


    void Show_Stats(){
		cout<<"Football Player\n";
		Player::Show_Stats();
		cout<<"Goals: "<<goals<<"\n";
		cout<<"Shots: "<<shots<<"\n";
		cout<<"Passes: "<<passes<<"\n";
	}
};




int main () {
	Player* players[3];
	players[0] = new Cricket(25,"Mike Smith",152,120,5);
	players[1] = new Hockey(22,"Peter Johnson",120,100,"FIN","LW");
	players[2] = new Football(20,"Feder Rosen",152,120,45,32,55);
	for(int i=0;i<3;i++){
		players[i]->Show_Stats();
		cout<<"\n";
	}
	system("pause");
	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