UML_使用Astah对C++代码反向建模(a.k.a cpp代码映射为类图的教程)

来源:互联网 发布:java用sleep内存泄露 编辑:程序博客网 时间:2024/05/17 15:38

C++ Reverse Engineering - Easiest way to reverse engineer C++ into an Astah Diagram

工作环境

macOS 10.12.6

(Copyright © http://blog.csdn.net/s_gy_zetrov. All Rights Reserved)

准备工作

  1. Astah software
  2. Astah plug-in download link
  3. Doxygen download link

插件安装

简单粗暴的方法是把下载好的插件(.jar)拷贝到/Application/Astah/plugin中然后重启Astah,或者参考官方文档link

使用Doxygen生成xml文件

  1. 使用默认的[Wizard]tab即可
  2. [Project]里面source code directory选择自己的C++文件所在路径,如果只有一个C++文件最好新建一个文件夹存放,防止其他文件的干扰导致xml生成不成功
  3. [Mode]里面选择All Entities和Optimize for C++ output
  4. [Output]中HTML和LaTeX全部叉掉,只选择最下面的xml
  5. 选择next后的页面点击Run Doxygen,生成xml,注意出现"*** Doxygen has finished"才算成功

C++代码生成类图

  1. Astah中新建一个class diagram,保存。
  2. 顶栏选择Tools - C++ - import C++
  3. 拖到画布中,类间的关系也会自动加入(Copyright © http://blog.csdn.net/s_gy_zetrov. All Rights Reserved)

cpp测试代码

#include <iostream>  using namespace std;  class Vehicle  {    public:      Vehicle(int weight = 0)      {        Vehicle::weight = weight;      }      void SetWeight(int weight)      {        cout<<"重新设置重量"<<endl;        Vehicle::weight = weight;      }      virtual void display() = 0;    protected:      int weight;  };  class Car:virtual public Vehicle//汽车  {    public:      Car(int weight=0,int aird=0):Vehicle(weight)      {        Car::aird = aird;      }      void display()      {        cout<<"我是汽车!"<<endl;      }    protected:      int aird;  };  class Ship:virtual public Vehicle//船  {    public:      Ship(int weight=0,float tonnage=0):Vehicle(weight)      {        Ship::tonnage = tonnage;      }      void display()      {        cout<<"我是船!"<<endl;      }    protected:      float tonnage;  };  class AmphibianCar:public Car,public Ship//水陆两用汽车,多重继承的体现  {    public:      AmphibianCar(int weight,int aird,float tonnage)      :Vehicle(weight),Car(weight,aird),Ship(weight,tonnage)      //多重继承要注意调用基类构造函数      {      }      void display()      {        cout<<"我是水陆两用汽车!"<<endl;      }  };  int main()  {    AmphibianCar a(4,200,1.35f);//错误    a.SetWeight(3);//错误    system("pause");   }

cpp测试代码生成效果

example.png

(Copyright © http://blog.csdn.net/s_gy_zetrov. All Rights Reserved)


visitor tracker
访客追踪插件


阅读全文
0 0
原创粉丝点击