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

Recommended Answers

All 4 Replies

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).

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).

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 :(

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:

+-------+ 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++.

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;
    ...
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.