Using C++ and without using Pointers Write a class called Player that has four d
ID: 3711374 • Letter: U
Question
Using C++ and without using Pointers
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-100 (this is not a valid value for any of the stats, and is one we should notice if it shows up where it shouldn't). 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.
For example, these classes might be used as follows:
The methods of the Player class should have the following names:
getName
setPoints
getPoints
setRebounds
getRebounds
setAssists
getAssists
hasMorePointsThan
The methods of the Team class should have the following names:
setPointGuard
getPointGuard
setShootingGuard
getShootingGuard
setSmallForward
getSmallForward
setPowerForward
getPowerForward
setCenter
getCenter
totalPoints
The files must be named: Player.hpp, Player.cpp, Team.hpp and Team.cpp
Explanation / Answer
Hi Dear,
Please find my implementation.
main.cpp
#include <iostream>
#include <string>
#include "Player.hpp"
#include "Team.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
Player p1("Charlotte", 24, 10, 7);
Player p2("Emily", 21, 13, 9);
Player p3("Anne", 20, 10, 8);
Player p4("Jane", 19, 10, 10);
Player p5("Mary", 18, 11, 8);
p5.setRebounds(12);
Team team1(p1, p2, p3, p4, p5);
Player p = team1.getShootingGuard();
cout << p1.getName() << endl;
cout << p1.getPoints() << endl;
cout << p1.getRebounds() << endl;
cout << p1.getAssists() << endl;
cout << p.getName() << endl;
cout << p.getPoints() << endl;
cout << p.getRebounds() << endl;
cout << p.getAssists() << endl;
cout << "team1 total points: " << team1.totalPoints() << endl;
}
Player.hpp
#ifndef PLAYER_HPP
#define PLAYER_HPP
#include <string>
using std::string;
class Player
{
public:
Player();
Player(string, int, int, int);
string getName();
void setPoints(int newPoints);
int getPoints();
void setRebounds(int newRebounds);
int getRebounds();
void setAssists(int newAssists);
int getAssists();
private:
string name;
int points;
int rebounds;
int assists;
};
#endif
Player.cpp
#include "Player.hpp"
#include "Team.hpp"
using std::string;
Player::Player()
{
name = "";
points = -1;
rebounds = -1;
assists = -1;
}
Player::Player(string userName, int userPoints, int userRebounds, int userAssists)
{
name = userName;
points = userPoints;
rebounds = userRebounds;
assists = userAssists;
}
string Player::getName()
{
return name;
}
void Player::setPoints(int newPoints)
{
points = newPoints;
}
int Player::getPoints()
{
return points;
}
void Player::setRebounds(int newRebounds)
{
rebounds = newRebounds;
}
int Player::getRebounds()
{
return rebounds;
}
void Player::setAssists(int newAssists)
{
assists = newAssists;
}
int Player::getAssists()
{
return assists;
}
Team.cpp
#include "Team.hpp"
#include "Player.hpp"
/*********************************************************************
** Description: constructor for Team with parameters
** parameters are in this order: point guard, shooting guard, small forward, power forward, and center
*********************************************************************/
Team::Team(Player userPG, Player userSG, Player userSF, Player userPF, Player userC)
{
pointGuard = userPG;
shootingGuard = userSG;
smallForward = userSF;
powerForward = userPF;
center = userC;
}
void Team::setPointGuard(Player newPG)
{
pointGuard = newPG;
}
Player Team::getPointGuard()
{
return pointGuard;
}
void Team::setShootingGuard(Player newSG)
{
shootingGuard = newSG;
}
Player Team::getShootingGuard()
{
return shootingGuard;
}
void Team::setSmallForward(Player newSF)
{
smallForward = newSF;
}
Player Team::getSmallForward()
{
return smallForward;
}
void Team::setPowerForward(Player newPF)
{
powerForward = newPF;
}
Player Team::getPowerForward()
{
return powerForward;
}
void Team::setCenter(Player newC)
{
center = newC;
}
Player Team::getCenter()
{
return center;
}
/*********************************************************************
** Description: adds up all the points for each of the five positions,
*********************************************************************/
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
#ifndef TEAM_HPP
#define TEAM_HPP
#include "Player.hpp"
class Team
{
public:
Team();
Team(Player, Player, Player, Player, Player);
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();
int totalPoints();
private:
Player pointGuard;
Player shootingGuard;
Player smallForward;
Player powerForward;
Player center;
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.