c++umlrhapsodyclass-relationship

Rhapsody-UML Class Relations


What is the difference between association and composition relation ship in uml..
For Example..

class Student
{
Prof p;
//other features..
};
class Prof
{
Student s;
//other features..
};

where above code is an example for composition relationship in c++..
And in same way what is the coding for association relationship...?
While working in uml how to implement this and how to find which relationship should be prefered?
In same way how to implement multiplicity concept in this relation's...?
Explain this thing with some real time example as detailed as possible.....
Thank u in advance..


Solution

  • composition is a special kind of binary association.

    In case of A composed by B, an A instance is responsible to create and destroy B instance; this also means that one instance of B may compose at most one instance of A.

    In your code both classes use composition but there's an error because there's a circular composition (Student needs Prof and Prof needs Student).

    Usually a Prof is related to many students and a Student is related to many Profs; in these case you could use aggregation in both classes with multiplicity greater than 1.

    class Student;  // Forward declaration
    
    class Prof {
        std::vector<Student*> _students;
    };
    
    class Student {
        std::vector<Prof*> _profs;
    };