Program should be written in C++. Please include comments. Create a class for ba
ID: 3699237 • Letter: P
Question
Program should be written in C++. Please include comments.
Create a class for baseball players. Define the attributes/data and functions (constructor, setters, getters and functions specific to a baseball player.
Consider that initially, the batting average of a new baseball player object is 0. If the player then gets to bat and hits the ball, the batting average will be (0 + 1)/2 = 0.500. So one member function can be a batting_average function. Think about and try to implement this in the class and what you will need to do in the main program to simulate a hit or miss and update the players batting average. You can include any other functions you think are appropriate. If you don't know much about baseball (like me), talk to someone who knows the game.
Explanation / Answer
batting.cpp
#include <iostream>
using namespace std;
class Player {
public:
int x,y;
//setter
void set_player_id(int a)
{
player_id = a;
}
void set_bat_avg(float c)
{
bat_avg = c;
}
//function batting_average calculation
void batting_average (int hit_count,int status)
{
x = hit_count;
y = bat_avg*x;
bat_avg =(float) (y+status)/(x+1);
}
//constructor
Player(int id)
{
player_id = id;
bat_avg = 0;
}
//getters
int get_player_id()
{
return player_id;
}
float get_batting_average ()
{
return bat_avg;
}
protected:
int player_id; // Palyer number
float bat_avg; //batting average
};
int main()
{
int id,choice,count = 0;
cout << "Please enter player's number: ";
cin >> id;
Player player(id);
cout << " player's number is:"<<player.get_player_id() << endl;
//this loop will continuously take the input of the status of batsman
while(1)
{
cout << "1.Hit 2.Miss 3.out Please enter correct choice:";
cin >> choice;
count = count + 1;
if (choice == 1)
{
player.batting_average(count,1);
}
else if (choice == 2)
{
player.batting_average(count,0);
}
else if (choice == 3)
{
break;
}
else
cout << " please enter proper choice";
cout<<" Current batting average:"<<player.get_batting_average()<<endl;
}
player.batting_average(count,0);
cout<<" Final batting average:"<<player.get_batting_average()<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.