• UML类图详解_泛化关系


    泛化其实就是继承关系,还是比较简单的,那么我们就把之前有些问题的博客UML类图重新来实现一次。

    依旧是这个图

    下面我们来看一个例子

    Account.h

     1 #include <cstdlib>
     2 #include <vector>
     3 #include "Bid.h" 
     4 using namespace std;
     5  
     6 class Account 
     7 {
     8 public:
     9     void setBid(Bid*); 
    10     int calcAsset(); 
    11 private:
    12     vector<Bid*> bidObj;
    13 };

    Account.cpp

     1 #include "Account.h"
     2             
     3 void Account::setBid(Bid *theBid)
     4 { 
     5      bidObj.push_back(theBid);
     6 }
     7 
     8 int Account::calcAsset()
     9 {
    10      int size,theAsset=0;
    11      size=bidObj.size();
    12      for(int i=0;i<size;i++)
    13          theAsset=theAsset+bidObj[i]->calcAsset();
    14      return theAsset; 
    15 } 

    Bid.h

     1 #include "Fund.h" 
     2  
     3 class Bid 
     4 {
     5 public:
     6     void setFund(Fund*);
     7     virtual int calcAsset()=0; 
     8     virtual float getUnit()=0;
     9 protected:
    10     Fund *fundObj; 
    11 };

    Bid.cpp

    1 #include "Bid.h"
    2 
    3 void Bid::setFund(Fund *theFund)
    4 {
    5      fundObj=theFund;
    6 }

    Fund.h

    1 class Fund
    2 { 
    3 public:
    4     Fund(float);
    5     float getPrice();
    6 private:
    7     float price;
    8 };

    Fund.cpp

     1 #include "Fund.h"
     2 
     3 Fund::Fund(float thePrice)
     4 {
     5     price=thePrice;
     6 }
     7 
     8 float Fund::getPrice()
     9 {
    10     return price;
    11 }

    SingleBid.h

    1 class SingleBid : public Bid
    2 {
    3 public:
    4     SingleBid(float);    
    5     int calcAsset();
    6     float getUnit(); 
    7 private: 
    8        float unit;
    9 }; 

    SingleBid.cpp

     1 #include "SingleBid.h"
     2 
     3 SingleBid::SingleBid(float theUnit)
     4 {
     5     unit=theUnit; 
     6 } 
     7 
     8 int SingleBid::calcAsset()
     9 {
    10     return unit*fundObj->getPrice();
    11 }
    12 
    13 float SingleBid::getUnit()
    14 {
    15     return unit;
    16 } 

    RegularBid.h

     1 #include <cstdlib>
     2 #include <vector>
     3 #include "BidItem.h" 
     4 #include "Bid.h"
     5 using namespace std;
     6 
     7 class RegularBid : public Bid
     8 {
     9 public:
    10     RegularBid();  
    11     int calcAsset();
    12     float getUnit();   
    13     
    14 private:
    15     vector<BidItem*> itemObj;
    16 }; 

    RegularBid.cpp

     1 #include "RegularBid.h"
     2 
     3 RegularBid::RegularBid()
     4 {
     5     BidItem *myItem; 
     6     myItem=new BidItem(59.53); 
     7     itemObj.push_back(myItem);
     8     myItem=new BidItem(89.85); 
     9     itemObj.push_back(myItem);
    10     myItem=new BidItem(91.45); 
    11     itemObj.push_back(myItem);
    12 } 
    13 
    14 float RegularBid::getUnit()
    15 {
    16     int size;
    17     float theUnit=0;
    18     size=itemObj.size();
    19     for(int i=0;i<size;i++)
    20          theUnit=theUnit+itemObj[i]->getUnit();
    21     return theUnit;   
    22 } 
    23 
    24 int RegularBid::calcAsset()
    25 {
    26     return getUnit()*fundObj->getPrice();
    27 }

    BidItem.h

    1 class BidItem 
    2 {
    3 public:
    4     BidItem(float); 
    5     float getUnit();
    6 private:
    7     float unit;
    8 };

    BidItem.cpp

     1 #include "BidItem.h"
     2 
     3 BidItem::BidItem(float theUnit)
     4 { 
     5      unit=theUnit;
     6 }
     7 
     8 float BidItem::getUnit()
     9 {
    10     return unit;
    11 }

    main.cpp

     1 // main.cpp
     2 //
     3 
     4 #include <cstdlib>
     5 #include <iostream>
     6 #include "SingleBid.h"
     7 #include "RegularBid.h"
     8 #include "Account.h" 
     9 #include "Fund.h" 
    10 using namespace std; 
    11  
    12 int main(int argc, char *argv[])
    13 { 
    14     Fund *myFund; 
    15     SingleBid *mySingleBid; 
    16     RegularBid *myRegularBid; 
    17     Account myAccount; 
    18     
    19     myFund=new Fund(19.84); 
    20     mySingleBid=new SingleBid(935.76);
    21     mySingleBid->setFund(myFund); 
    22     myAccount.setBid(mySingleBid); 
    23     cout << "大华大华基金单位及净值: "
    24          << "(" << mySingleBid->getUnit() << ")"
    25          << "(" << myFund->getPrice() << ")" << endl; 
    26          
    27     myFund=new Fund(37.83); 
    28     mySingleBid=new SingleBid(721.45);
    29     mySingleBid->setFund(myFund); 
    30     myAccount.setBid(mySingleBid);
    31     cout << "日盛上选基金单位及净值: "
    32          << "(" << mySingleBid->getUnit() << ")"
    33          << "(" << myFund->getPrice() << ")" << endl;
    34     
    35     myFund=new Fund(49.61); 
    36     myRegularBid=new RegularBid();
    37     myRegularBid->setFund(myFund); 
    38     myAccount.setBid(myRegularBid);
    39     cout << "富邦长虹基金单位及净值: "
    40          << "(" << myRegularBid->getUnit() << ")"
    41          << "(" << myFund->getPrice() << ")" << endl << endl;
    42     
    43     cout << "总资产为: "
    44          << myAccount.calcAsset() << endl << endl;
    45            
    46     system("PAUSE");
    47     return EXIT_SUCCESS;
    48 }

    下面我们来画一下UML图,并且用UML自动生成C++代码来做一个比较

    生成代码对比 

    Account.h

    达到预期

    Bid.h

    达到预期
    Fund.h

    达到预期

    SingleBid.h

    达到预期

    RegularBid.h 

    达到预期

    BidItem.h 

    达到预期

  • 相关阅读:
    表空间的改变
    特殊字符转换
    oracle下创建临时表
    约束
    删除数据库记录的同时删除磁盘文件
    数据文件
    级联删除case
    oracle net连接方式

    在Oracle服务器端配置监听器
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7750506.html
Copyright © 2020-2023  润新知