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>
#include <string>
using namespace std;
class Player
{
public:
double getbatting_avg()
{
batting_avg = m_run / (double)m_match;
return batting_avg;
}
double getwicket_per()
{
wicket_per = m_wicket / (double)m_match;
return wicket_per;
}
Player(string name, int id, int match, int run, int wicket)
{
m_name= name ;
m_id = id;
m_match = match;
m_run = run;
m_wicket = wicket;
}
double batting_avg;
double wicket_per;
string m_name;
int m_id;
int m_match;
int m_run;
int m_wicket;
};
int main()
{
// example of using an instance of a class
Player first("John", 11111, 100, 58, 32);
first.getbatting_avg();
first.getwicket_per();
cout << "Player " << first.m_name << " Batting average=" << first.getbatting_avg() << endl;
cout << "Player " << first.m_name << " Wickets per match=" << first.getwicket_per() << endl;
}
Comments
Leave a comment