UML_使用Astah對C++代碼反向建模(a.k.a cpp代碼映射為類圖的教程)


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
訪客追蹤插件



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com