Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class that represents a major-league baseball player. the \"player\' cl

ID: 3805785 • Letter: D

Question

Design a class that represents a major-league baseball player. the "player' class should have (at the very least) data members: name, team, height, weight, age, and batting Average. Provide a constructor that takes some, or all of these parameters as arguments (I recommend initializing batting Average to zero.) the class should have members Get Profile () (to print out all the player s information), and Add Batting Data (), that adds batting information, and updates the batting Average data member. We will define batting average as: Batting Average = (number Of Hits/number Of Times at Bat) Write a program that instantiates a player object (either hard-code the player's information, or prompt the user for it.) Loop N-times (where N = number of at-bats, user supplied) to add batting information (i.e. each time through the loop ask: "Hit? (y/n): '), and finally print out the player profile with the updated batting average. Make sure you keep/update the batting average information using member variable.

Explanation / Answer

#include<iostream>
#include<string>

class Player
{
   public:
       string name,team;
       int weight,height,age, noHits, noBat;
       float battingAverage;
      
       Player(string n, string t, int h, int w, int a)
       {
           name=n;
           weight=w;
           team=t;
           height=h;
           age=a;
           noHits=0;
           noBat=0;
           battingAverage=0;
       }
       void AddBattingData(int h, int b)
       {
           noHits+=h;
           noBat+=b;
           battingAverage=noHits/noBat;  
       }
       void GetProfile()
       {
           cout<<"Name:"<< name<<" Team: "<<team<<" Height: "<<height<<" Weight "<<weight<<" Average: "<<battingAverage;
       }
};
main() {
  
  
   Player p("As","T",12,12,12);
  
   p.AddBattingData(10,1);p.AddBattingData(20,1); p.GetProfile();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote