3

When writing a UML class diagram for C++ code, is it necessary to include constructors and destructors of the relevant class?

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41

2 Answers2

1

In many cases, adding constructors/destructors to a C++ class diagram would just be textual boilerplate noise and not really meaningful. Only if you want your diagram to be a complete documentation of your C++ classes, you may want to add constructors.

Contrary to what @Marco is saying below, this is not a matter of the underlying business logic, but a purely ergonomic issue of avoiding cluttering and noise in (typically complex) diagrams.

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • This answer is incorrect: https://stackoverflow.com/questions/4006868/uml-classdiagram-constructor-with-parameters – Marco Jul 20 '19 at 16:26
0

The constructor can be added if it's part of the business logic, you just don't add a return type.
Would be under the functions/methods section:
+ClassName()
I would not add destructors. Again, depends on whether your business logic needs it.

Marco
  • 335
  • 2
  • 9
  • Depending on the target language (e.g. Python) you could add `__init__(self, parms)` or in some meta language `init(parms) -> ClassName` or whatever is convenient. – qwerty_so Oct 19 '18 at 22:48
  • A constructor cannot be part of the business logic since constructors are a completely technical, and not a business, concept. – Gerd Wagner Oct 23 '18 at 11:37
  • 1
    No, constructors can enforce business constrains if an entity requires certain characters to exist. For example, an employee cannot exist without an employee ID and name, this is business logic. But a business could not be issuing employee ID's, since it's small and will never keep track of this, then you would not add it, as it's not part of what makes an employee. Everything is use case, that's the joy of UML, it gets adapted to the need of the use case, and as long as everyone understands why, it's correct. – Marco Jul 20 '19 at 15:47