Write a program having following classes
Player (Base Class)
Members:
Cricket player (Derived Class)
Members:
Hockey player (Derived Class)
Members:
Football player (Derived Class)
Members:
#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;
}
Comments
Leave a comment