Article Consultation Solution Tool Course Member  
 
 
 
     
   
Mapping of Class Diagrams and Code (C++)
 
Zu Tao ,Pitaya System Engineering
Views    2024-4-29 
 

The class diagram is the main modeling form of code structure, and in order to make the reader see it more vividly, the mapping relationship between the class diagram and the code is introduced in the form of a legend.

Here's a legend for a design class diagram:

The following is a class diagram and a code example (C++ code) of the relationships between classes:

1) Association

2) Aggregation

3) Composition

4) Generalization

5) Realization

6) Dependency

Relationship of Classes:

Association

The Driver can drive the Car, and the relationship between the two has been around for a long time.

#include "Car.h"

class Driver
{

public:
      Driver();
      virtual ~Driver();
      Car *m_Car; //assocation关系

  void Drive();

private:
      int name;
      int age;

};

 

Aggregation

Car aggregates wheels, and wheels can exist independently of cars.

class Car : public Device
{

public :
      Car ();
      virtual ~ Car ();
      Wheel * m_Wheel ; //aggregation 关系
      Motor m_Motor ;
 
      void Move ();
 
private :
      int mass ;
      int rate ;
 
};

 

Composition

Car contains Motor, which is an integral part of Car.

#include "Wheel.h"
 
class Car : public Device
{
 
public :
      Car ();
      virtual ~ Car ();
      Wheel * m_Wheel ;
      Motor m_Motor ; //composition 关系
 
      void Move ();
 
private :
      int mass ;
      int rate ;

};

 

Generalization

Motor and Wheel are subclasses of Device, inheriting the properties of Device: id and state, methods: Start() and Stop()

#include "Device.h"
class Wheel : public Device
{
 
public :
      Wheel ();
      virtual ~ Wheel ();
 
      void Rotate ();
 
private :
      int radius ;
      int width ;

};

 

Realization

Device implements the Start() and Stop() methods of the Runnable interface.

 

#include "Runnable.h"
 
class Device : public Runnable //Realization 关系
{
 
public :
      Device ();
      virtual ~ Device ();
      int id ;
      int state ;
 
      void Start ();
      void Stop ();
 
};

 

Dependency

The car's move operation relies on enviroment

#include "Wheel.h"
#include "environment.h"
 
class Car
{
 
public :
      Car ();
      virtual ~ Car ();
      Wheel * m_Wheel ;
      Motor * m_Motor ;
 
      void Move ( enviroment e ); //dependency 关系
 
private :
      int mass ;
      int rate ;

};

 

Note: The model in this article uses the modeling tool EA to model and generate code.


UML Diagram Series:

1.UML Overview
2.Use Case Diagram
3.Activity Diagram
4.Class Diagram
5.Object Diagram
6.Sequence Diagram
7.Interpretation of the Mapping Relationship between Class Diagram and Code (C++)

 

Postscript

I hope you have benefited from reading this.

If you are willing to share your experience, please submit it to us.

If you are interested in our training, consulting and tools:

  • Course: Analytical design based on UML and EA  
  • Course: MBSE (Model-Based Systems Engineering)  
  • Course: Model-Based Requirements Management) Methods and Practices
  • Course: System Design and Modeling Based on SysML and EA
  • Course: Enterprise Architecture Modeling
  • Course: System Architecture Modeling Methods and Cases
  • Course: Domain-Driven Modeling and Design
  • Course: Model-Based Design
  • Course: Business Modeling and Business Analysis
  • Modeling tool: EA
  • Model-based requirements management tool: iSpace
  • Consulting Solution: MBSE (Model-Based Systems Engineering)
  • Consulting Solution: Model-Driven Development Based on UML
  • Welcome to contact us: umlooo@hotmail.com

       
    Views  
     
    Related Articles

    UML Overview
    UML Diagram: Use Case Diagram
    UML Diagram: Activity Diagram
    UML diagram: class diagram
    UML Diagram: Object Diagram
    UML Diagram: sequence diagram
     
    Related Courses

    MBSE (Model-based Systems Engineering)
    System analysis and design based on UML
    Business Modeling & Business Analysis
    System design and modeling SysML EA
    Model-based requirements management
    Business Modeling &; Domain-Driven
     
    Related Tool

    MBSE Platform
    Modeling Tools EA
    Requirements Management
    Automatic modeling
    Multi-level simulation -Sys Simulator
    Code Engineer

    Tool News
    June 2024 EA v17.0 Beta release
    November 2022 EA v16.1 release notes
    November 2022 EA v16.1 official release
    July 2022 EA v16.05 release notes
    April 2022 EA16.0 official release
     
    Latest Article
    UML Overview
    UML Diagram: Use Case Diagram
    UML Diagram: Activity Diagram
    UML diagram: class diagram
    UML Diagram: Object Diagram
    UML Diagram: sequence diagram
    Specification changes from UML 2.4 to UML 2.5
    DDS models and modeling tools
    More...   
    MBSE Tool
    MBSE Platform
    Modeling Tools EA
    Requirements Management
    Automatic modeling
    Multi-level simulation -Sys Simulator
    Code Engineer
    DocGenerator
    Model Checker
    R&D management
    TestDriver
    Model Based Quality Manager (inspector)
    More...   
    Successful Case
    Gac Research Institute SysML+EA+ Software
    Modeling tools EA, WebEA, and learning
    China Automotive Intelligence modeling tools EA...
    Ekatong MBSE tool chain consulting
    Avic UAV MBSE tool chain
    Geely Auto buys EA tools
    Huaco Auto parts buy EA tools
    Dongfeng LAN Map Auto purchase EA tools