Index


RISC World

Football Manager

Part 6 of Paul Johnsons series

What goes for one, goes for all

C++ has amongst it's armoury some really powerful programming facilities, one of them being that of inheritance.

Simply put, this can be thought of as a common set of properties. It is best described as an "is a" relationship. The simplest demonstration of this is that of cars and lorries.

Say we wanted a base class (that is a fundamental class) which has the common properties of both lorries and cars, we need a class name. First off, is a car a lorry? Obviously not and the reverse is also true, so we need to something which is common to both. Quite simply, they are both vehicles. That is, they both satisfy the "is test" (a car is a vehicle, a lorry is a vehicle).

Next up, draw up a list of properties available to both cars and lorries. These will be (say) number of wheels, fuel type and number of people it can hold. All of these can be in the base class (called vehicle).

We then define attributes which only apply to cars and lorries (say cargo type and what side drive the steering wheel is for the lorry and number of passengers and fuel type for the car). The classes look like this:

class vehicle 
{
  public :
     vehicle() {};
     virtual ~vehicle() {};
     int set_no_wheels(int);
     int set_no_people(int);
	virtual void show_all();
  protected:
     int noWheels;
     int noPeople;
};
&bnsp;
class car : public vehicle
{
   public:
      car() {};
      virtual ~car() {};
      int set_no_passengers(int x) 
		{x > noPeople ? return -1 : passengers = x;} 
      bool set_fuel_type(bool);
      virtual void show_all();
   private:
      int passengers;
      bool fuel_type;
};
&bnsp;
class lorry : public vehicle
{
   public:
     lorry() {};
     virtual ~lorry() {};
     int set_cargo_type(int);
     bool set_hand_drive(bool);
     virtual void show_all();
   private:
     int cargo_type;
     bool driver_side;
};

Okay, some parts need explaining. Most of it is self explanatory though.

Protected:
This means that the classes which inherit the base class have access to this variable, but cannot change it.

     virtual~

All inherited destructors have to be used as virtual destructors

     virtual void show_all();

This is a special method. Not only would void car::show_all() display the members of the car class (if that is what you wanted), but also those of the vehicle class. This can be really useful as it can save a pile of time, we don't need to re-write the same code every time. It is a virtual method as it is created at runtime. Think about it, if your own class has a method called foo and you inherit a class with another method called foo then the compiler will throw a wobbler!

Applying this to the task in hand

We need a base class. In this case we need to think what the base class will be. A player is not a team, a manager is not a team, the list can go on, so let's take it from another angle. A player is a member of a team. A team will have a certain number of staff as well as other attributes (money, statistics and the such). A team is a member of a league.

We can start with a league class. This will contain a small number of items, league number, top three and bottom three teams and a couple of methods which calculate the team positions and also display the league tables.

Each team will have a number of players which are a lore unto themselves (and therefore a class of their own), but the team has a manager, cash flow and stats. The players belong to a team and have a value, fitness levels, skill and if they're injured or not.

So, lets put some flesh onto these bones, these will change as the code is developed.

class league
{
  public:
     league() {};
     virtual ~league() {};
     int league_division() {return division;}
     virtual void display_league();
     void setup_league();
     void next_game();
     void calculate_league();
   protected:
     int division;
};
 
class teams : public league
{
  public:
     teams() {};
     virtual ~teams() {};
     virtual void display_team(int);
     void display_stats();
     double return_money() {return cash;}
     int return_skilllevel() {return skill;}
  private:
     int team_no;
     int skill;
     double cash;
};
 
class players : public teams
{
  public:
     players() {};
     virtual ~players() {};
     virtual void display_team(int);
     int is_player_injured(int);
     void set_player_injured(int);
     void setup_team(int);
  private:
};

Okay, I've started to flesh out the program on how the class system will look. What I've not said is how the data will be stored. That's for next time when I'll look into linked lists vs the standard template library.

Paul Johnson

 Index