UML Class Diagram Explained With C++ samples

As you know a Class diagram is a diagram showing different classes in a system their attribute, operation and the relationship among different objects.

Class Diagram

Even I have been using it for long time; I always had confusion each time when I use it. This post is meant for such people; also it will be helpful for beginners, here I will explain the class diagram with C++ class example.

A class representation


class Circle {
private:
double radius;
Point center;
public:
setRadius(double radius);
setCenter(Point center);
double getArea();
double getCircumfrence();
};

Class diagram for the above class is shown below.
class_diagram_sample
Different visibility of the class can be represented as
“+” Public
“-” Private
“#” Protected

Different Parameter direction
“in”           The parameter is an input parameter.
“Inout”    The parameter is capable of both input and output.
“Out”        The parameter is an output parameter.

Different type of members in a class
1) Static members are represented as underlined.
2) Pure virtual functions are represented as italics.

Class relationship

In a system a class may be related to different classes,following are the different relation ship.

  • Association (knows a)
  • Dependency (uses a)
  • Composition (has a)
  • Aggregation (has a)
  •  Inheritance (is a)
  •  Class template

Different Multiplicity in a relation
“0..1”            No instances, or one instance (optional, may)
“1”                  Exactly one instance
“0..* or *”    Zero or more instances
“1..*”              One or more instances (at least one)
Association
One object is aware of another; it contains a pointer or reference to another object.
Representaion
association_class_diagram
C++ Example

Class X {

  X(Y *y) : y_ptr(y) {}

  void SetY(Y *y) { y_ptr = y;   }

  void f()        { y_ptr->Foo();}
  ----
  Y *y_ptr; // pointer
};

Dependency
One class depends on another if the independent class is a parameter variable or local variable of a method of the dependent class
Representaion

class_diagram_dependency
C++ Example

class X {
 ...
 void f1(Y y)  {…;  y.Foo();       }
 void f2(Y *y) {…;  y->Foo();      }
 void f3(Y &y) {…;  y.Foo();       }
 void f4()     {   Y y; y.Foo();  …}
 void f5()     {…; Y::StaticFoo(); }
 ...
};

Aggregation
Aggregation can occur when a class is a collection or container of other classes, but where the contained classes do not have a strong life cycle dependency on the container—essentially, if the container is destroyed, its contents are not. You may have confusion between aggregation and association .Association differs from aggregation only in that it does not imply any containment.
 Representaion
class_diagram_aggregation
C++ Example

Example 1

class Window
{
 public:
  //...
 private:
 vector<Shape*> itsShapes;
};

A window class contains a list of its shapes
class_diagram_winow_shapes
Example 2:
A car has it’s tiers, and the scope of tyre doesn’t depend on a car since a tyre can be used for another car also
class_diagram_car_tyres

A Rectangle class has its style, which may be shared by other shapes also; life time of style doesn’t depend on Rectangle class.

Aggregation-sample
Composition
Composition is the stronger form of aggregation. Composition can occur when a class is a collection or container of other classes, but where the contained classes have a strong life cycle dependency on the container—essentially, if the container is destroyed, its contents are also destroyed
Representation

class_diagram_composition
C++ Example

class Circle
{
private:
	 ...
	Point center;
....
};

class_diagram_circle_center

class X {
...
Y a; // 1; Composition
Y b[10]; // 0..10; Composition
};

class X {
X() { a = new Y[10]; }
~X(){ delete [] a; }
...
Y *a; // 0..10; Composition
};

class X {
...
vector a;
};

Inheritance (Generalization)
In Inheritance relationship a class is derived from another class. It is a “is a” relationship between two classes.
Representation
class_diagram_inheritance
Here X and Y are normal classes.

class_diagram_inheritance_2

Here Shape is an abstract class that is why it is shown in Italics. Draw () and Erase () methods of Shape class is pure virtual function, so it is also shown as italics.
Class Template
Template class mean generic classes.Languages like C++, java, C# supports generic programming.
Representation
class_diagram_template
C++ represenatation

template <class T>
class X {
...
...
...
};
X Y
...
X<Y> a;
...

Advertisements

13 thoughts on “UML Class Diagram Explained With C++ samples

  1. Pingback: Uml Relationships Explained | Cuties Live

  2. Very usefull article. But I have 1 question:
    When you explain the relationship “assossiation” you have written the following code:

    Class X {

    X(Y *y) : y_ptr(y) {}

    void SetY(Y *y) { y_ptr = y; }

    void f() { y_ptr->Foo();}
    —-
    Y *y_ptr; // pointer
    };

    In the description you talk about assossiation as that an object “knows the other”. In the code I see a stronger relationships. For example you call Foo() in void f() which would seem more like a dependcy relationship. I also see that you have a member Y *y_ptr which I would seem that there is relationship aggragation.

    Am I wrong here?

    Greetings Willem

    • here “Y *y_ptr ” in class “X” doesn’t have any life cycle dependency(composition) nor it implies it any containment(aggregation); so it is an association. Association differs from aggregation only in that it does not imply any containment.

      • So:

        if “Y *y_ptr” then it is Association because X contains a “reference” (pointer) to Y which means that Y have to exist independent of X in order to have a reference to it.

        if “Y y” then it is Composition because X contains an object Y whose existence depends on the existence of X.

        if “vector y” then it is Aggregation because X has a “container”, in this case, a vector, of Y.

        Is this correct?

      • case 1: if X contains vector; it is a composition;
        case 2:if X contains vector; and construction and destruction of Y* happen inside X (in constructor/destructor) it is composition
        case 3:if X contains vector; and construction and destruction of Y* not happening inside X (in constructor/destructor) then it can be aggregation/Aggregation based on the scenario.

  3. This article is very helpful! I wish, though that objects used had more visual examples to help visualizing the objects.
    Class X {
    X(Y *y) : y_ptr(y) {}

    Why not …
    Class Car { Car(Wheel * wheel): wheel_ptr(wheel)()

    Gives visual reference to explanation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s