Questions about creating arrays under classes
Dec 20, 2012 at 8:00am UTC
I'm working on the line up function which returns a line-up of 5 players – 2 guards, 2 forwards, and 1 center. However, in order to make it work, I have to create 3 different arrays which are for guards, forwards, and center. But I don't know how to create arrays for them under the class Team. Should I do something like " Player p[3] = new* Center" ? One more question, I'm not sure where to add those arrays as well. I tried to add them in my main function, but it is not possible to use because my line up function is in class Team.
Thank You for helping
Player.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include "enums.h"
#include "globalconstants.h"
#include "stringfunctions.h"
using namespace std;
class Player;
typedef Player* PPlayer;
class Player
{
public :
//constructors
Player(void );
Player(const string& new_name);
//accessors
string name(void ) const ;
int number(void ) const ;
//mutators
bool name(const string& new_name);
bool number(int new_number);
//custom methods
virtual void say(ostream& sink = cout) = 0;
virtual Position position(void ) const = 0;
private :
string _name;
int _number;
};
#endif
Player.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "Player.h"
//constructors
Player::Player(void )
{
_name = default_player_name;
_number = default_player_number;
}
Player::Player(const string& new_name)
{
if (!name(new_name))
throw string("Player constructor fails" );
number(default_player_number);
}
//accessors
string Player::name(void ) const
{
return _name;
}
int Player::number(void ) const
{
return _number;
}
//mutators
bool Player::name(const string& new_name)
{
string temp = trim(new_name);
bool good_name = temp.length() > 0;
if (good_name)
_name = temp;
return good_name;
}
bool Player::number(int new_number)
{
bool good_number = new_number >= first_player_number;
if (good_number)
_number = new_number;
return good_number;
}
Center.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef CENTER_H
#define CENTER_H
#include "Player.h"
class Center:
public Player
{
public :
Center(void );
Center(const string& new_name);
void say(ostream& sink = cout);
Position position(void ) const ;
};
#endif
Center.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include "Center.h"
Center::Center(void )
:Player()
{
}
Center::Center(const string& new_name)
:Player(new_name)
{
}
Position Center::position(void )const
{
return CENTER;
}
void Center::say(ostream& sink)
{
sink<<"My name is " <<name()<<endl;
sink<<"My number is " <<number()<<endl;
sink<<"My position is center" <<endl;
}
Team.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#ifndef TEAM_H
#define TEAM_H
#include "Player.h"
#include "Center.h"
class Team
{
public :
Team& operator =(const Team& right_hand_side);
Team(void );
Team(const Team& original);
~Team(void );
bool Add(PPlayer new_player);
void LineUp();
private :
int _unique_number;
int _population;
PPlayer* _player;
};
#endif;
Team.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "Team.h"
Team::Team(void )
{
_player = new PPlayer[team_size];
if (_player == NULL)
throw string("Team constructor fails" );
_unique_number = 0;
}
Team::~Team(void )
{
for (int index = 0; index < _population; index++)
delete _player[index];
delete [] _player;
}
bool Team::Add(PPlayer new_player)
{
bool good_add = _population < team_size;
if (good_add)
{
_player[_population] = new_player;
_player[_population]->number(_unique_number);
_unique_number++;
_population++;
}
return good_add;
}
void Team::LineUp()
{
}
Topic archived. No new replies allowed.