473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

creating dynamics derived classes

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
Jul 22 '05 #1
4 1594
On Mon, 12 Jan 2004 22:32:25 +0000, Gonzalo Aguirre wrote:
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


Jul 22 '05 #2
"Gonzalo Aguirre" <go*****@sondrobe.homelinux.org> wrote in message
news:bt************@ID-217155.news.uni-berlin.de...
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?

Jul 22 '05 #3
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! */
...
/* and so on */

}

thank in advance
Jul 22 '05 #4
"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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Edward Diener | last post by:
I am creating a component and I want one of my properties to be an embedded class with its own properties. When the component designer shows this property I want it to be able to expand this...
4
by: Brad Kartchner | last post by:
I'm attempting to write a program with several child classes derived from a single parent class. The parent class has a couple of variables that need to be present in each of the child classes. ...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
6
by: Vincent Finn | last post by:
Hi, I have what I thought was a simple question but I can't find an answer. I need to access (create) unmanaged C++ classes in C#. They aren't COM and they aren't struct just simple classes. ...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
1
by: Charles Law | last post by:
I have a base class MyBaseClass, and several classes that inherit from it: MyClass1, MyClass2, etc. The base class implements IEnumerable(Of IMyBaseClassRow). The base class has a DataTable...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.