打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
UML Class Diagram Explained With C++ samples | CPP Code Tips

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

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

1
2
3
4
5
6
7
8
9
10
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.


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


C++ Example

1
2
3
4
5
6
7
8
9
10
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


C++ Example

1
2
3
4
5
6
7
8
9
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


C++ Example

Example 1

1
2
3
4
5
6
7
class Window
{
 public:
  //...
 private:
 vector itsShapes;
};

A window class contains a list of its 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

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


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


C++ Example

1
2
3
4
5
6
7
class Circle
{
private:
     ...
    Point center;
....
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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


Here X and Y are normal classes.

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


C++ represenatation

1
2
3
4
5
6
7
8
9
10
template
class X {
...
...
...
};
X Y
...
X a;
...
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Class Diagram Relationships UML | Examples Relationships Class Diagram
Rvalue Reference Quick Look
Applying Strategy Pattern in C Applications...
Function Objects (STL)
A Simple Trading System
Introduction to Dependency Injection (Introduction to CAB/SCSF Part 3) ? Rich Newman
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服