You are on page 1of 2

CPP CODE TIPS

Sadique Ali's C++ Blog ..Quest For Knowledge…

HOME ABOUT

23 UML Class Diagram Explained With SEARCH


DEC
2013
C++ samples Search …
posted in C++ by Sadique Ali E

As you know a Class diagram is a diagram showing di"erent classes in a system their
attribute, operation and the relationship among di"erent objects.
TOTAL VISITORS

418,368 hits

ARCHIVES

October 2020

September 2020

August 2020

August 2019

July 2019

February 2019

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

A class representation June 2017

May 2017

April 2017

class Circle { March 2017

private: February 2017


double radius; January 2017
Point center;
December 2016
public:
November 2016
setRadius(double radius);
setCenter(Point center); October 2016
double getArea(); September 2016
double getCircumfrence();
August 2016
};
July 2016

June 2016

May 2016
Class diagram for the above class is shown below.
April 2016

March 2016

February 2016

December 2015

November 2015

October 2015

September 2015
Di!erent visibility of the class can be represented as
August 2015
“+” Public
July 2015
“-” Private
“#” Protected June 2015

May 2015
Di!erent Parameter direction
April 2015
“in”           The parameter is an input parameter.
March 2015
“Inout”    The parameter is capable of both input and output.
“Out”        The parameter is an output parameter. February 2015

January 2015
Di!erent type of members in a class
December 2014
1) Static members are represented as underlined.
November 2014
2) Pure virtual functions are represented as italics.
October 2014
Class relationship September 2014

August 2014
In a system a class may be related to di"erent classes,following are the di"erent
relation ship. July 2014

June 2014
Association (knows a)
May 2014
Dependency (uses a)
March 2014
Composition (has a)
Aggregation (has a) February 2014

 Inheritance (is a) January 2014


 Class template December 2013

Advertisements

CATOGORIES
REPORT THIS AD

Di!erent Multiplicity in a relation


C
“0..1”            No instances, or one instance (optional, may)
C++
“1”                  Exactly one instance
“0..* or *”    Zero or more instances C++ Threading

“1..*”              One or more instances (at least one) C++11


Association C++14
One object is aware of another; it contains a pointer or reference to another object.
C++17
Representaion
C++20

Design Pattern

Java

C++ Example mocking in c

Modern C++

NetWorking
Class X {
POSIX

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

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

Uncategorized
void f() { y_ptr->Foo();}
---- VC++/MFC

Y *y_ptr; // pointer Video Streaming


};

DISCLAIMER
Dependency
One class depends on another if the independent class is a parameter variable or “The information in this weblog
local variable of a method of the dependent class is provided “AS IS” with no
warranties, and confers no
Representaion
rights.”

C++ Example December 2013

M T W T F S S
class X {
  1
...
void f1(Y y) {…; y.Foo(); } 2 3 4 5 6 7 8

void f2(Y *y) {…; y->Foo(); }


9 10 11 12 13 14 15
void f3(Y &y) {…; y.Foo(); }
void f4() { Y y; y.Foo(); …} 16 17 18 19 20 21 22

void f5() {…; Y::StaticFoo(); } 23 24 25 26 27 28 29


...
30 31  
};

    Jan »

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 SUBSCRIBE TO BLOG VIA
have confusion between aggregation and association .Association di"ers from EMAIL
aggregation only in that it does not imply any containment.
Enter your email address to
 Representaion
subscribe to this blog and
receive noti#cations of new
posts by email.

Email Address
C++ Example

Subscribe
Example 1

Join 25 other subscribers


class Window
{
public:
//... VISITORS SINCE SEPTEMBER
private: 2020
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 LAST MONTH VIEW

A Rectangle class has its style, which may be shared by other shapes also; life time of TOP POSTS & PAGES
style doesn’t depend on Rectangle class.
UML Class Diagram Explained
With C++ samples

A Simple Logger Class In C++

QT interview questions

File System Watcher in C++ for


windows

Composition Di"erent ways to get the name

Composition is the stronger form of aggregation. Composition can occur when a of the calling function in C++

class is a collection or container of other classes, but where the contained classes UDP socket class in C++
have a strong life cycle dependency on the container—essentially, if the container is Thread Pool class using modern
destroyed, its contents are also destroyed c++
Representation
Plug In Architecture In C++

C++ wrapper class for shared


memory

Reading and Writing a UTF-8 File


C++ Example In C++

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

class X { Generate
reliable data
...
Ensure 100 %
Y a; // 1; Composition accuracy in time
Y b[10]; // 0..10; Composition and efficiency
}; Wallet HR

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

class X { REPORT THIS AD

...
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.

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

C++ represenatation

template
class X {
...
...
...
};
X Y
...
X a;
...

Share this:

! Twitter " Facebook

Loading...

Related

Decorator Pattern Proxy Design Pattern Factory Design Pattern


Explained with Explained with in C++
C++ sample C++ Sample December 16, 2014
October 31, 2016 March 31, 2017 In "C++"
In "C++" In "C++"

!Class Diagram, Class Diagram Explained With ! 30 Comments


C++ codes

« C++ class for pthread C++ Wrapper For SQLITE C API »


conditional variable

30 thoughts on “UML Class Diagram Explained With C++ samples”

xs4rahulgoel

July 20, 2014 at 7:54 pm

Reblogged this on Technical Bytes and commented:


Nice Article on the class diagrams

Reply

Parthasarathy

October 7, 2014 at 4:49 am

Really useful article

Reply

Shalin

October 21, 2014 at 1:20 pm

Very useful information specially for the 2nd year assignments OOP. I
found more in the UML diagram tutorial

Reply

Pingback: Uml Relationships Explained | Cuties Live

Willem

December 30, 2015 at 11:34 am

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

Reply

Sadique Ali E

February 25, 2016 at 5:53 am

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
di"ers from aggregation only in that it does not imply any
containment.

Reply

Manuel Malagon

November 7, 2017 at 7:54 pm

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?

Sadique Ali E

November 8, 2017 at 5:40 am

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.

Josh

May 12, 2017 at 11:37 am

Yes! Finally a decent post about UML C++ diagrams that is straight to the
point!

Reply

Sadique Ali E

May 19, 2017 at 10:22 am

Thanks for the comments..Keep watching this blog

Reply

Josh

May 12, 2017 at 11:38 am

Reblogged this on The Irregular Gamer and commented:


Great post about C++ UML diagram creation

Reply

Clayton Huntsman

May 21, 2017 at 8:09 pm

Enjoyed the blog. Nice reminder in the class relationships paragraph.


Good work.

Reply

Sadique Ali E

January 31, 2018 at 9:42 am

Thanks

Reply

Ken

December 8, 2017 at 11:09 pm

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.

Reply

Tony

July 12, 2018 at 1:17 pm

Thanks for the helpful article Sadique. I appreciate it.

I think there is a mistake in the very #rst class diagram. The connection
drawn between the Vehicle class and the Wheel class is wrong. You have
the white diamond drawn at the Wheel class, when it should be drawn at
the Vehicle class. The Vehicle aggregates the Wheel, not the other way
around.

Cheers,

Tony

Reply

Sadique Ali E

July 13, 2018 at 7:06 am

Thanks for the info; You are correct.I will update it

Reply

Adrian

July 19, 2018 at 5:32 am

Nice article

Reply

Adrian

July 19, 2018 at 5:35 am

This are all the possibilities of interactions between classes?

Reply

Sadique Ali E

July 19, 2018 at 5:43 am

Yes,According to UML this are the possible cases

Sadique Ali E

July 19, 2018 at 5:42 am

Thanks

Reply

Carlos Maceira

June 4, 2019 at 11:49 am

Very good post, it helped me a lot to understand UML relations.

Reply

Pingback: How easy is coding? – Zeger has a blog

m4l490n

December 5, 2020 at 12:25 am

How would you model a map? for example, you have two classes, A and B.
Class B has a map of

class A {
};

class B {
private:
std::map classBs;
};

Reply

sadasivam

February 6, 2021 at 9:26 am

Hi, I am new to uml modelling language. Somehow, I will grab the basics
and excel in it. I want to know which UML books can be useful in terms of
begineer. I need the guide for, cpp code generation for every concepts. I
referred lot of UML reference guide. In every guide, the #nal code
generation is sonewhat missing. So, for a begineer it is unable to grab
what will be the exact output generated. If this helps, then I can able to
learn by working with it. Can you guide me in this?

Reply

sadash

February 7, 2021 at 5:14 am

Hi, is there any uml reference book with sample codes for every
concepts? As a begineer, its to work with it but, dont know how to
approach this.

Reply

sadash

February 7, 2021 at 6:36 am

Is there any guide regarding every concepts in uml for cpp?

Reply

Arkan

March 4, 2021 at 2:48 am

How about and example for “realization”?

Reply

Arkan

March 4, 2021 at 4:05 am

You make a distinction between (directed) association and


aggregation/composition.
According to several source you should not because “Aggregation and
Composition are subsets of association meaning they are speci#c cases
of association”.
https://www.visual-paradigm.com/guide/uml-uni#ed-modeling-
language/uml-aggregation-vs-composition/

In other words: Association = { (bidirectional/directed) association,


aggregation, composition}
with association you just don’t know whether the class owns/manage the
memory of the member.
Cheers.

Reply

Ilya

January 18, 2022 at 10:34 pm

in your Aggregation example you de#ne class Window like this:

class Window
{
public:
//…
private:
vector itsShapes;
};

1. the type of itsShape’s should be vector


2. here, itsShapes will be destroyed when the Window object is
destroyed which doesn’t match the description you provide: “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”

Reply

adityamall90

August 12, 2022 at 5:00 pm

I was almost hopeful that someone would give examples with classes not
being circle or rectangle or some geometric shape for god’s sake, but
today is not that day.

Reply

Leave a Reply

Enter your comment here...

Advertisements

Fast and Responsive | Call Now


Privacy
Create a&free
Cookies: This
or site
bloguses cookies. By continuing to use this website, you agree to their use.
Lenovo Exclusive Store - RT Computers - Hubli
website at WordPress.com.
To #nd out more, including how to control cookies, see here: Cookie Policy
Close and accept

REPORT THIS AD

You might also like