I have a two classes: ------------ --------------- X Y ------------ ------------
ID: 653196 • Letter: I
Question
I have a two classes:
------------ ---------------
X Y
------------ ---------------
relation
------------ ------------------ ---------------
A() C()
B()
D()
E()
------------ ---------------
I want to inherit just these two functions from X class, where they are defined with the friend access modifier. My class will be:
---------------
Y
---------------
---------------
C()
A()
D()
---------------
Y class uses some functions of X class namely A and D. How can I model this relationship in a UML class diagram?
Explanation / Answer
If the methods of X are instance methods, you can use the association relationship, which is a solid line between the two classes, however this might indicate to the reader of the model that the relationship is bidirectional. If an instance of Y maintains a reference to X in it and X does not maintain any kind of reference to 'Y', you can add the open-ended arrow to the end pointing toward X to add directionality. If Y can't exist without X, you can also consider using aggregation to indicate this.
If the methods of X are static, consider the dependency relationship, represented by a dotted line and open ended arrow from Y to X. This relationship is used Y calls static methods of X or if the functions of Y take X as a parameter but a reference to X is not maintained within an instance of Y.
In your specific case, when you're trying to model the friend relationship in C++, the information that I can find (such as this question on Stack Overflow) seems to suggest that you should annotate the members of X with the <> stereotype. From this point, I'd use either the association or dependency relationship discussed above, based on how Y is accessing X, with association if Y maintains an instance of X and dependency if Y receives instances of X as parameters to functions as they are needed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.