creating dynamics derived classes

G

Gonzalo Aguirre

i have a classes diagram like this

------------ * ----------
| Vehicles | ------------------------- | Parking|
------------ vehicles ----------
^
|
+--+--------+
| |
-------- ---------
| Cars | | Motos |
-------- ---------



class Vehicles{
protected:
double weight;
bool stereo;
public:
Vehicles();
~Vehicles();

virtual method() = 0; /* pure cirtual method */
...
};

class Cars : public Vehicles{
private:
int doors;
public:
Cars();
~Cars();

method(); /* method redefinition */
...
};

class Motor : public Vehicles{
private:
int aThing; /* my imagination is empty */
public:
Motos();
~Motos();

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??..


void
Parking::init(int c, int m){
vehicles = new Cars[c];
vehicles = new Motos[m]; // <--- IT CANNOT BE!!!..
....
}

thanks in advance
 
G

Gonzalo Aguirre

i have a classes diagram like this

------------ * ----------
| Vehicles | ------------------------- | Parking|
------------ vehicles ----------
^
|
+--+--------+
| |
-------- ---------
| Cars | | Motos |
-------- ---------



class Vehicles{
protected:
double weight;
bool stereo;
public:
Vehicles();
~Vehicles(); ^--- virtual

virtual method() = 0; /* pure cirtual method */
...
};

class Cars : public Vehicles{
private:
int doors;
public:
Cars();
~Cars();

method(); /* method redefinition */
...
};

class Motor : public Vehicles{
private:
int aThing; /* my imagination is empty */
public:
Motos();
~Motos();

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??..


void
Parking::init(int c, int m){
vehicles = new Cars[c];
vehicles = new Motos[m]; // <--- IT CANNOT BE!!!..
....
}

thanks in advance
 
D

David White

Gonzalo Aguirre said:
i have a classes diagram like this

------------ * ----------
| Vehicles | ------------------------- | Parking|
------------ vehicles ----------
^
|
+--+--------+
| |
-------- ---------
| Cars | | Motos |
-------- ---------



class Vehicles{

Why is your class name a plural? Does this class represent one vehicle or
more than one? If it is one, call it Vehicle.
protected:
double weight;
bool stereo;
public:
Vehicles();
~Vehicles();

virtual ~Vehicles();
virtual method() = 0; /* pure cirtual method */

You need a return type on this function.
...
};

class Cars : public Vehicles{

Again, a plural for a class name.
private:
int doors;
public:
Cars();
~Cars();

method(); /* method redefinition */

Return type still needed.
...
};

class Motor : public Vehicles{

Now your class name is a singular. How can one Motor be multiple Vehicles?
private:
int aThing; /* my imagination is empty */
public:
Motos();
~Motos();

method(); /* method redefinition */
...
};

class Parking{

Wouldn't CarPark be a better name?
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.

The best thing would be for the Parking (or CarPark) object to exist all the
time. The real car park exists all the time doesn't it? So why should you
destroy your Parking object at the end of each day and create it again the
next day?

BTW, do you really need to know what kinds of vehicles are in the car park,
and do you need an actual object for each one? Unless you have reasons for
doing it this way, it would be enough for the Parking object to simply keep
a count rather than a collection of objects.

DW

P.S. For parking purposes, why do you need to know if a vehicle has a
stereo? So it can be stolen?
 
G

Gonzalo Aguirre

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 &);
};
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! */
...
/* and so on */

}

thank in advance
 
D

David White

Gonzalo Aguirre said:
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 &);
};
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top