Association

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster

Association

 
0
  #1
Nov 4th, 2009
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
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #2
Nov 4th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 95
Reputation: gusano79 is on a distinguished road 
Solved Threads: 9
gusano79 gusano79 is offline Offline
Junior Poster in Training
 
0
  #3
Nov 4th, 2009
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).
--smg
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster
 
0
  #4
Nov 5th, 2009
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
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #5
Nov 5th, 2009
Originally Posted by Phil++ View Post
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:
  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++.
  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. };
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC