泛化其实就是继承关系,还是比较简单的,那么我们就把之前有些问题的博客UML类图重新来实现一次。
依旧是这个图
下面我们来看一个例子
Account.h
#include <cstdlib>
#include <vector>
#include "Bid.h"
using namespace std; class Account
{
public:
void setBid(Bid*);
int calcAsset();
private:
vector<Bid*> bidObj;
};
Account.cpp
#include "Account.h" void Account::setBid(Bid *theBid)
{
bidObj.push_back(theBid);
} int Account::calcAsset()
{
int size,theAsset=;
size=bidObj.size();
for(int i=;i<size;i++)
theAsset=theAsset+bidObj[i]->calcAsset();
return theAsset;
}
Bid.h
#include "Fund.h" class Bid
{
public:
void setFund(Fund*);
virtual int calcAsset()=;
virtual float getUnit()=;
protected:
Fund *fundObj;
};
Bid.cpp
#include "Bid.h" void Bid::setFund(Fund *theFund)
{
fundObj=theFund;
}
Fund.h
class Fund
{
public:
Fund(float);
float getPrice();
private:
float price;
};
Fund.cpp
#include "Fund.h" Fund::Fund(float thePrice)
{
price=thePrice;
} float Fund::getPrice()
{
return price;
}
SingleBid.h
class SingleBid : public Bid
{
public:
SingleBid(float);
int calcAsset();
float getUnit();
private:
float unit;
};
SingleBid.cpp
#include "SingleBid.h" SingleBid::SingleBid(float theUnit)
{
unit=theUnit;
} int SingleBid::calcAsset()
{
return unit*fundObj->getPrice();
} float SingleBid::getUnit()
{
return unit;
}
RegularBid.h
#include <cstdlib>
#include <vector>
#include "BidItem.h"
#include "Bid.h"
using namespace std; class RegularBid : public Bid
{
public:
RegularBid();
int calcAsset();
float getUnit(); private:
vector<BidItem*> itemObj;
};
RegularBid.cpp
#include "RegularBid.h" RegularBid::RegularBid()
{
BidItem *myItem;
myItem=new BidItem(59.53);
itemObj.push_back(myItem);
myItem=new BidItem(89.85);
itemObj.push_back(myItem);
myItem=new BidItem(91.45);
itemObj.push_back(myItem);
} float RegularBid::getUnit()
{
int size;
float theUnit=;
size=itemObj.size();
for(int i=;i<size;i++)
theUnit=theUnit+itemObj[i]->getUnit();
return theUnit;
} int RegularBid::calcAsset()
{
return getUnit()*fundObj->getPrice();
}
BidItem.h
class BidItem
{
public:
BidItem(float);
float getUnit();
private:
float unit;
};
BidItem.cpp
#include "BidItem.h" BidItem::BidItem(float theUnit)
{
unit=theUnit;
} float BidItem::getUnit()
{
return unit;
}
main.cpp
// main.cpp
// #include <cstdlib>
#include <iostream>
#include "SingleBid.h"
#include "RegularBid.h"
#include "Account.h"
#include "Fund.h"
using namespace std; int main(int argc, char *argv[])
{
Fund *myFund;
SingleBid *mySingleBid;
RegularBid *myRegularBid;
Account myAccount; myFund=new Fund(19.84);
mySingleBid=new SingleBid(935.76);
mySingleBid->setFund(myFund);
myAccount.setBid(mySingleBid);
cout << "大华大华基金单位及净值: "
<< "(" << mySingleBid->getUnit() << ")"
<< "(" << myFund->getPrice() << ")" << endl; myFund=new Fund(37.83);
mySingleBid=new SingleBid(721.45);
mySingleBid->setFund(myFund);
myAccount.setBid(mySingleBid);
cout << "日盛上选基金单位及净值: "
<< "(" << mySingleBid->getUnit() << ")"
<< "(" << myFund->getPrice() << ")" << endl; myFund=new Fund(49.61);
myRegularBid=new RegularBid();
myRegularBid->setFund(myFund);
myAccount.setBid(myRegularBid);
cout << "富邦长虹基金单位及净值: "
<< "(" << myRegularBid->getUnit() << ")"
<< "(" << myFund->getPrice() << ")" << endl << endl; cout << "总资产为: "
<< myAccount.calcAsset() << endl << endl; system("PAUSE");
return EXIT_SUCCESS;
}
下面我们来画一下UML图,并且用UML自动生成C++代码来做一个比较
生成代码对比
Account.h
达到预期
Bid.h
达到预期
Fund.h
达到预期
SingleBid.h
达到预期
RegularBid.h
达到预期
BidItem.h
达到预期