Question

Write a class called Player that has four data members: a string for the player's name,...

Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It should have set methods for each of the stats. It should have a bool method called hasMorePointsThan that takes a Player parameter and returns true if the player that is executing the function has more points than the Player that was passed as a parameter. Otherwise it should return false. For example, if we have the function call "p1.hasMorePointsThan(p2)", then it should return true if p1 has more points than p2, but return false otherwise.

Next write a class called Team that has five data members of type Player: a point guard, a shooting guard, a small forward, a power forward, and a center. The class should have a constructor that takes five Players and uses them to initialize each of those data members (in the given order). The class should have get and set methods for each data member. It should have a method named totalPoints that returns the sum of the points for all players on the team.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screenshot:

Sample Output:

Code to copy:

//Player.hpp
//Header files
#ifndef PLAYER_HPP
#define PLAYER_HPP
#include <string>
using namespace std;

// Implementation of the class Player
class Player
{

private:
   //Declare the four data members
   //a string for the player's name
   string name;
   //For each of these stats:
   //points, rebounds and assists.
   int points;
   int rebounds;
   int assists;

public:
   //A default constructor
   Player();
   //A constructor that takes four parameters
   Player(string, int, int, int);
   //Getters for each data member.
   //setters for each of the stats.
   string getName();
   void setPoints(int newPoints);
   int getPoints();
   void setRebounds(int newRebounds);
   int getRebounds();
   void setAssists(int newAssists);
   int getAssists();
   //A bool method called hasMorePointsThan
   //that takes a Player parameter
   bool hasMorePointsThan(Player p);

};
#endif

---------------

//Player.cpp

//include the header files
#include "Player.hpp"
#include "Team.hpp"

using namespace std;
//A default constructor and initializes
Player::Player()
{
   //It initializes the name
   //to the empty string("")
   name = "";
   //Each of the stats to - 1.
   points = -1;
   rebounds = -1;
   assists = -1;
}
//A constructor that takes four
//parameters and uses them to
//initialize the data members
Player::Player(string name, int points,
   int rebounds, int assists)
{
   this->name = name;
   this->points = points;
   this->rebounds = rebounds;
   this->assists = assists;
}

//getters for each data member.
string Player::getName()
{
   return name;
}
//setters for each of the stats
void Player::setPoints(int points)
{

   this->points = points;
}

int Player::getPoints()
{
   return points;
}

void Player::setRebounds(int rebounds)
{
   this->rebounds = rebounds;
}

int Player::getRebounds()
{
   return rebounds;
}

void Player::setAssists(int assists)
{
   this->assists = assists;
}

int Player::getAssists()
{
   return assists;
}
// Definition of the bool method
bool Player::hasMorePointsThan(Player p)
{
   if (points > p.points)
   {
       //It returns true if the player that is executing
       //the function has more points than the
       //Player that was passed as a parameter
       return true;
   }
   else
   {
       //Otherwise it should return false
       return false;
   }
}

----------------

//Team.cpp

//Includde the header files
#include "Team.hpp"
#include "Player.hpp"

//A constructor that takes
//five Players
Team::Team(Player pointGuard, Player
   shootingGuard, Player smallForward,
   Player powerForward, Player center)
{
   this->pointGuard = pointGuard;
   this->shootingGuard = shootingGuard;
   this->smallForward = smallForward;
   this->powerForward = powerForward;
   this->center = center;
}

// getters and setters
void Team::setPointGuard(Player pointGuard)
{
   this->pointGuard = pointGuard;
}

Player Team::getPointGuard()
{
   return pointGuard;
}

void Team::setShootingGuard(Player shootingGuard)
{
   this->shootingGuard = shootingGuard;
}

Player Team::getShootingGuard()
{
   return shootingGuard;
}

void Team::setSmallForward(Player smallForward)
{
   this->smallForward = smallForward;
}

Player Team::getSmallForward()
{
   return smallForward;
}

void Team::setPowerForward(Player powerForward)
{
   this->powerForward = powerForward;
}

Player Team::getPowerForward()
{
   return powerForward;
}

void Team::setCenter(Player center)
{
   this->center = center;
}

Player Team::getCenter()
{
   return center;
}
//Definition of the totalPoints and that
//returns the sum of the points
int Team::totalPoints()
{
   int totalPoints = 0;
   totalPoints += getPointGuard().getPoints();
   totalPoints += getShootingGuard().getPoints();
   totalPoints += getSmallForward().getPoints();
   totalPoints += getPowerForward().getPoints();
   totalPoints += getCenter().getPoints();

   return totalPoints;
}

---------

//Team.hpp

//Header files
#ifndef TEAM_HPP
#define TEAM_HPP
#include "Player.hpp"

// Implementation of the class Team
class Team
{
private:
   // Declare the five data members of type Player
   Player pointGuard;
   Player shootingGuard;
   Player smallForward;
   Player powerForward;
   Player center;
public:
   //The class should have a constructor that takes
   // five Players
   Team(Player, Player, Player, Player, Player);
   // getters and setters
   void setPointGuard(Player);
   Player getPointGuard();

   void setShootingGuard(Player);
   Player getShootingGuard();

   void setSmallForward(Player);
   Player getSmallForward();

   void setPowerForward(Player);
   Player getPowerForward();

   void setCenter(Player);
   Player getCenter();
   // Declare a method named totalPoints
   int totalPoints();
};

#endif

Add a comment
Know the answer?
Add Answer to:
Write a class called Player that has four data members: a string for the player's name,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Taxicab that has three int fields (data members) to store its current...

    Write a class called Taxicab that has three int fields (data members) to store its current x- and y-coordinates and the total distance it has driven so far (the actual distance driven by the Taxicab, not the Euclidean distance from it's starting point). The class should have a constructor that takes two parameters and uses them to initialize the coordinates, and also initializes the distance traveled to zero. The class should have a default constructor that sets all three fields...

  • Write a class named Taxicab that has three private data members: one that holds the current...

    Write a class named Taxicab that has three private data members: one that holds the current x-coordinate, one that holds the current y-coordinate, and one that holds the odometer reading (the actual odometer distance driven by the Taxicab, not the Euclidean distance from its starting point). The class should have an init method that takes two parameters and uses them to initialize the coordinates, and also initializes the odometer to zero. The class should have get members for each data...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

  • Write a class that encapsulates the concept of a baseball player with the important attributes (name,...

    Write a class that encapsulates the concept of a baseball player with the important attributes (name, position, hits, at bats, batting average). The class will have the following methods: Two constructors (a default and a constructor with all parameters) Accessors, mutators, toString, and equals methods The batting average property will not have a public setter, it will be updated any time the hits and/or at bats is set. The getter will round to three digits (.315 or .276, etc.) All...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT