WAP to create the classes as per the hierarchy shown below.Include the data members in the relevant classes to store the following information:-
Name, Id, No of matches played,No. of runs scored, No. of wickets taken.
Calculate the batting average and wickets per match for an all rounder player.Use parameterized constructor to initialize the object.
[ Batting average=No. of runs scored/No. of matches played
Wickets per match= No. Of wickets taken/ No. of matches played ]
#include <iostream>
using namespace std;
class Player{
private:
string Name;
int Id;
int No_of_matches_played;
int No_of_runs_scored;
int No_of_wickets_taken;
public:
Player(string n,int i,int np,int nr,int nw){
Name=n;
Id=i;
No_of_matches_played=np;
No_of_runs_scored=nr;
No_of_wickets_taken=nw;
}
void getBattingAverage(){
int avg=No_of_runs_scored/No_of_matches_played;
cout<<"\nThe batting average= "<<avg;
}
void getWicketsPerMatch(){
int w=No_of_wickets_taken/No_of_matches_played;
cout<<"\nWickets per match= "<<w;
}
};
int main()
{
Player p("John",123,10,30,20);
p.getBattingAverage();
p.getWicketsPerMatch();
return 0;
}
Comments
Leave a comment