944,051 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1047
  • C++ RSS
Nov 4th, 2009
0

Association

Expand Post »
Right, you're going to think I'm crazy but is Association like "Has is"
For example:

I have 2 classes, Teacher and Classroom, to create association would be:
Teacher has a Classroom?

Thanks
Similar Threads
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Nov 4th, 2009
0
Re: Association
Association can have lots of meanings. Any reference from one class to another is a kind of association. "Has-a" is one; it could also be "uses."

"Has-a" is often called aggregation or composition. In terms of C++; it's commonly implemented by a member variable of the owned type, or by having a pointer or reference in the owning class to the owned class. The initialization of the member variable or pointer happens in the constructor (for example, if the teacher is permanently assigned to a room).

"Uses" can also be implemented as a pointer in the using class to the used class, but in this case, the pointer might be changed after construction by a setter method (for example, if the teacher moves from room to room).
Reputation Points: 36
Solved Threads: 9
Newbie Poster
boblied is offline Offline
24 posts
since Mar 2009
Nov 4th, 2009
0
Re: Association
More food for thought... this probably goes beyond your current question, but I think it helps to demonstrate the modelling process:

The unmentioned "Class" object could be the true parent here... a Class would have (among other things) a Teacher who teaches the class, and a Classroom where the class is taught. This seems more natural to me than the Teacher or the Classroom "owning" or "using" the other.

If the class moves from room to room, then instead of having a Classroom, a Class could have a "Schedule" object. A Schedule would be a list of "Session" (there's probably a better name for this I can't think of right now) objects, each of which has a Time and a Place--Classroom could be a type (subclass) of Place, but there could be others, for example a Cafe (for informal classes) or Website (for e-learning courses).
Reputation Points: 182
Solved Threads: 72
Posting Pro in Training
gusano79 is offline Offline
476 posts
since May 2004
Nov 5th, 2009
0
Re: Association
Ahh, thanks but I'm still really confused
If I had a teacher and a classroom what could I use as the association because there needs to be 1
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Nov 5th, 2009
0
Re: Association
Click to Expand / Collapse  Quote originally posted by Phil++ ...
Ahh, thanks but I'm still really confused
If I had a teacher and a classroom what could I use as the association because there needs to be 1
Any way in which the two are related can be an association. Which one applies depends on what the problem is, and whether we're starting from a Teacher and want to find out about Classroom, or the other way around. Here are some examples:
  • Teacher teaches-in Classroom
  • Teacher has-desk-in Classroom
  • Teacher uses Classroom
  • Classroom is-occupied-by Teacher
  • Classroom hosts Teacher

Any of these are going to be represented similarly in a UML diagram:
C++ Syntax (Toggle Plain Text)
  1. +-------+ teaches-in +---------+ +-------+ has-desk-in +---------+
  2. |Teacher|---------------->|Classroom| |Teacher|---------------->|Classroom|
  3. +-------+ +---------+ +-------+ +---------+
  4.  
  5. +-------+ uses +---------+
  6. |Teacher|---------------->|Classroom|
  7. +-------+ +---------+
  8.  
  9. +---------+ is-occupied-by +-------+ +---------+ hosts +-------+
  10. |Classroom|---------------->|Teacher| |Classroom|---------------->|Teacher|
  11. +---------+ +-------+ +---------+ +-------+

Any of them are also going to be implemented similarly in C++.
C++ Syntax (Toggle Plain Text)
  1. class Teacher {
  2. private:
  3. // Rooms in which a teacher might teach
  4. vector<Classroom> teaches_in;
  5. ...
  6. };
  7.  
  8. class Teacher {
  9. private:
  10. // Room where teacher's primary desk is
  11. Classroom* room_where_desk_is;
  12. ...
  13. };
  14.  
  15. class Teacher {
  16. private:
  17. // Classrooms that a teacher uses.
  18. vector<Classroom> rooms_used;
  19. ...
  20. };
  21.  
  22. class Classroom {
  23. private:
  24. // Teachers that occupy this room during the day
  25. vector<Teacher> is_occupied_by;
  26. ...
  27. };
  28.  
  29. class Classroom {
  30. private:
  31. // Primary teacher that this room hosts
  32. Teacher* host;
  33. ...
  34. };
Reputation Points: 36
Solved Threads: 9
Newbie Poster
boblied is offline Offline
24 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC