containment class model

I have modeled an aggregation of door,tire,steering wheel of a car. But i want to know how do i model the relationship of car to door as(1:2..5).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class tire
{
//...
};
class steering
{
//...
};
class door
{
//...
};
class car
{
private:
   tire t[4];//4 tires(1:4) relationship
   steering s;//1 steering(1:1) relationship
   door ?;//2..5 doors(1:2..5) relationship
};

How do i model such a relationship where i would not know how many doors there could be?

either that would be dynamic, so may be you can take a list of doors in the car class.
or specialize the car class for may be a car with 4 doors and a car with 5 doors. Obviously you can do that as these all cars will have different models.
i didnt get it. could you illustrate. Thanks

edit: without having the need to create a specialised class.

list<door> l;

is that what u mean?
Last edited on

yes I mean that.

addDoor() method can add doors to the car. Do you think there is problem in that?
A quick one:

1
2
3
4
5
template<int numDoors>
class car
{
    door d[numDoors];
}
@writeonsharma:

i think the addDoor() method would be a solution but would you loose the idea of aggregation cos u are providing an interface rather than containment. Its an idea but i suppose it will not capture containment as it shld.

@webjose:
im not a template junkie but how would this help. do you pass numDoors in the constructor.
cos i have always seen
1
2
3
4
5
template<class numDoors>
class car
{
//
}


what does the syntax u stated do.
Last edited on
You can do:

car<2> myConvertible;

Or:

1
2
class Convertible : public car<2> { }; 
Convertible myConvertible;


Or:

1
2
typedef car<2> Convertible;
Convertible myConvertible;

This is perfectly an aggregation relationship. Aggregation is used in this way only and I think you can go with this. Look for some examples on the net. The discussion can be between if you want to go for aggregation or inheritance as I said in my first post. If you dont want to go for this kind of aggregation, than you will have to go for inheritance.

Containment is also like aggregation but we have multiple instance of object inside the class.

for example if you look for a design of maze, room and door implementation. A maze can have many rooms with many doors. And the room can have 1, 2 or n doors inside that. Add door method helps in adding doors to the room and similarly, add room method add's rooms to the maze.

does this justify my idea?
Topic archived. No new replies allowed.