| | |
Association
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
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).
"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).
•
•
Join Date: May 2004
Posts: 95
Reputation:
Solved Threads: 9
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).
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
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
0
#5 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
- 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)
+-------+ teaches-in +---------+ +-------+ has-desk-in +---------+ |Teacher|---------------->|Classroom| |Teacher|---------------->|Classroom| +-------+ +---------+ +-------+ +---------+ +-------+ uses +---------+ |Teacher|---------------->|Classroom| +-------+ +---------+ +---------+ is-occupied-by +-------+ +---------+ hosts +-------+ |Classroom|---------------->|Teacher| |Classroom|---------------->|Teacher| +---------+ +-------+ +---------+ +-------+
Any of them are also going to be implemented similarly in C++.
C++ Syntax (Toggle Plain Text)
class Teacher { private: // Rooms in which a teacher might teach vector<Classroom> teaches_in; ... }; class Teacher { private: // Room where teacher's primary desk is Classroom* room_where_desk_is; ... }; class Teacher { private: // Classrooms that a teacher uses. vector<Classroom> rooms_used; ... }; class Classroom { private: // Teachers that occupy this room during the day vector<Teacher> is_occupied_by; ... }; class Classroom { private: // Primary teacher that this room hosts Teacher* host; ... };
![]() |
Similar Threads
- Word Association Game (Posting Games)
- No files association with ANY file (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






