"Gonzalo Aguirre" <go*****@sondrobe.homelinux.org> wrote in message
news:bu************@ID-217155.news.uni-berlin.de...
On Tue, 13 Jan 2004 10:06:16 +1100, David White wrote:
i have a classes diagram like this
------------ * ----------
| Vehicle | ------------------------- | Parking|
------------ vehicle ----------
^
|
+--+--------+
| |
------- --------
| Car | | Moto |
------- --------
class Vehicle{
protected:
double weight;
bool stereo;
public:
Vehicle();
virtual ~Vehicle();
virtual void method() = 0; /* pure cirtual method */
...
};
class Car : public Vehicle{
private:
int doors;
public:
Cars();
~Cars();
void method(); /* method redefinition */
...
};
class Moto : public Vehicle{
private:
int aThing; /* my imagination is empty */
public:
Motos();
~Motos();
void method(); /* method redefinition */
...
};
class Parking{
private:
int capacity;
Vehicles *vehicles;
public:
Parking();
Parking(int);
~Parking();
void init(int, int); /* numbers of cars, number of motos */
void arrive(Vehicles &);
void leave(Vehicles &);
};
suppouse that i have to init() every day when i arrives to the parking,
and maybe some owners forget theirs cars/motos, so a should init the
parking with those numbers.
The problem is that it isn't a static number (today i have to init with
2 cars and 1 moto, and tomorrow no one forgets his/her car/moto).
So how should i create those objects??..
Create them the same way you create each vehicle that enters the car
park during the day. Create each car object and use the 'arrive' member. You
should not need the 'init' member at all. You only need 'arrive' and
'leave' and the Parking object can keep track of how many cars it has.
It was just an example (analogous to real problem). The problem is that i
need to create a "Parking" (CarPark) varying parameters.
Another example could be to create a chess game, that the player could
config the initial board, so i'll need to create pieces that depends on
parameters (think about pawn's promotion with several queens, etc)
/* a real simplificated chess game source */
Class Board{
private:
class Piece *piece;
public:
Board();
~Board();
void initConfig(int, int, int ...);
};
void
Board::initConfig(int BP, int BR, int BKn, int BB, ...) /*where BP (Black
pawn, Black Rock, and so on) */
{
piece = new class Pawn[BP];
piece = new class Rock[BR]; /* <--- that's the real problem! */
No, use a loop to create the pieces one at a time:
while(BR-- > 0)
{
Piece *pPiece = new Rook(/* white or black */);
// add Rook to board at appropriate position
}
Creating an array of objects is not suitable for this purpose because you
cannot delete them in the same way as the objects created one at a time.
...
/* and so on */
}
Okay, but I still think my original answer holds. Since the Parking object
has a collection of objects of different classes representing the vehicles
in the carpark, and possibly with varying attributes, the only possibility I
see is to create each vehicle object already parked and tell the Parking
object that it's already there (for this, you might want to add another
member, since 'arrive' is misleading because the vehicle is already there).
If you don't care about the vehicle classes and attributes for those
vehicles already there, then you can use an 'init' function and have the
Parking object create default objects for those vehicles. But this seems
ad-hoc and messy to me. It's more elegant to add vehicles already parked in
as similar a way as possible to those that arrive normally.
In the case of the chess game it's better to initialize it by making all the
moves that had been made in the game up to that point. In this way it's
similar to the initializing the Parking object. Rather than use a completely
different, unrelated method to initialize the game than to play it, it's
much neater to use initialize it in the same way you would play it. It's so
messy to initialize a chess game using a config member such as yours above.
You not only have to give it the positions of all the pieces, but whether
each King or Rook has moved at all (i.e., is castling allowed), whether a
pawn on the fourth rank moved there on the last move (and so can be captured
en passant), how many moves have been made since a pawn move or capture (the
50-move draw rule), how many times every position has occurred so far (3
times and a draw can be claimed), etc. It's much better just to have the
list of moves made and make them all, which the Chess game already knows how
to do, and will place the game in the correct state as a matter of course.
DW