Lab 12: Intro to UML

UML Class Diagram basics

The Unified Modeling Language (UML) is a graphical language used to represent the structure and behavior of systems. While UML includes various diagram types for modeling different systems, we will focus on a specific subset related to visualizing classes and their relationships. UML serves as an effective communication tool, allowing teams to quickly convey the architecture and connections within a program through visual representations.

Here’s an example of a UML class diagram featuring multiple classes with an “inheritance” relationship:

§1 UML Class Diagram Overview #

A class diagram is a type of diagram that describes the structure of a system with a visual representation of its classes, attributes, methods, and the relationships among objects. A class diagram typically contains:

§2 Example Class #

Lets begin with a practical example. Here is a simple class prototype which represents a circle.

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

And here is how this Circle class would be represented in UML notation:

This class contains a member center which references another class, let’s add this Point class to the diagram:

Even without code, with the UML diagram we can effectively communicate that the Point class has two member variables: x and y. The solid line with a white diamond connecting the two classes signifies an aggregation relationship between them, indicating that the Point class is a part of the Circle class, but can exist independently of the Circle class.

§3 UML Diagram Relationships #

Relationships are used to show how different elements within a system interact with one another. Specifically, UML relationships are essential for modeling the connections between classes, objects, and other components in a system. the above example demonstrates an “Aggregation” relationship, there are several different types of relationships that can be represented in UML.

Common UML Relationships:

- Association: has a

- Inheritance: is a

- Realization: implements

- Dependency: uses

- Composition: contains a

- Aggregation: has a

UML Relationships

Some more examples of each relationship type:

§4 UML Diagram Creation #

There are many online tools that can be utilized to create UML diagrams. Here is a non-exhaustive list of options for creating diagrams:

I used mermaid.live for creating the diagrams in this lab. If you use this, you will want to first check the docs

§5 Further Reading #

§6 Assignment #

For this lab, you will create a few UML diagrams to visualize the following, be sure to include Access Specifiers and relationships in your diagrams:

Part 1:
Create a UML diagram representing the following Book class:

class Book {
private:
  std::string title;
  std::string author;
  std::string ISBN;
  double price;

public:
  // Method to display book information
  void getDetails();

  // Method to set a new price
  void setPrice(double newPrice);

  // Method to get the price
  double getPrice();
};

Part 2:
Design a UML class diagram for the following classes and relationships:

Write a short explanation of why you used a specific relationship for linking each class.


Part 3:
Create a UML class diagram for a Car class that has:

Write a short explanation of why you used a specific relationship for linking each class.

For Submission: place all three diagrams and explanations on a document and submit as a single PDF.