![]() |
#2
w5277050902012-11-23 20:13
|

class fund
{
public:
fund(){};
virtual double get_netvalue() const=0;
virtual double get_stockprice() const=0;
virtual ~fund(){};
virtual fund* clone() const=0;//使用复制构造函数(克隆)
private:
};
class motherfund: public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢?
{
public:
motherfund(double netvalue_) : netvalue(netvalue_) {};
virtual ~motherfund(){};
virtual fund* clone() const//function clone
{return new motherfund(*this);
}
virtual double get_netvalue() const
{return netvalue;}
virtual double get_stockprice() const
{return netvalue;//不知道这样改了会不会bug....
}
private:
double netvalue;
};
//子基金类定义
class umbrellafund : public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢?
{
public:
umbrellafund(double netvalue_,double stockprice_) : netvalue(netvalue_),stockprice(stockprice_) {};
virtual ~umbrellafund(){};
virtual fund* clone() const //function clone
{return new umbrellafund(*this);
}
virtual double get_netvalue() const
{return netvalue;}
virtual double get_stockprice() const
{return stockprice;}
private:
double netvalue;
double stockprice;
};
以上是本来的类定义{
public:
fund(){};
virtual double get_netvalue() const=0;
virtual double get_stockprice() const=0;
virtual ~fund(){};
virtual fund* clone() const=0;//使用复制构造函数(克隆)
private:
};
class motherfund: public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢?
{
public:
motherfund(double netvalue_) : netvalue(netvalue_) {};
virtual ~motherfund(){};
virtual fund* clone() const//function clone
{return new motherfund(*this);
}
virtual double get_netvalue() const
{return netvalue;}
virtual double get_stockprice() const
{return netvalue;//不知道这样改了会不会bug....
}
private:
double netvalue;
};
//子基金类定义
class umbrellafund : public fund//不知道为什么把这个继承改成public openendfund就不对了。。?这是为什么呢?
{
public:
umbrellafund(double netvalue_,double stockprice_) : netvalue(netvalue_),stockprice(stockprice_) {};
virtual ~umbrellafund(){};
virtual fund* clone() const //function clone
{return new umbrellafund(*this);
}
virtual double get_netvalue() const
{return netvalue;}
virtual double get_stockprice() const
{return stockprice;}
private:
double netvalue;
double stockprice;
};
以下是我模仿老师要求的建立过渡类

class fundbridge
{public:
fundbridge(const fundbridge& bridge)
{
fundpointer=bridge.fundpointer->clone();
}
fundbridge(const fund& innerfund)
{fundpointer=innerfund.clone();
}
~fundbridge()
{delete fundpointer;
}
fundbridge& operator=(const fundbridge& bridge)
{if(this !=&bridge)
{delete fundpointer;
fundpointer=bridge.fundpointer->clone();
}
return *this;
}
private:
fund* fundpointer;
};
class LOFfund
{public:
LOFfund(const fundbridge& thefundbridge_,int shares_):thefundbridge(thefundbridge_),shares(shares_)
{}
double getshares() const
{return shares;}
private:
fundbridge thefundbridge;
int shares;
};
{public:
fundbridge(const fundbridge& bridge)
{
fundpointer=bridge.fundpointer->clone();
}
fundbridge(const fund& innerfund)
{fundpointer=innerfund.clone();
}
~fundbridge()
{delete fundpointer;
}
fundbridge& operator=(const fundbridge& bridge)
{if(this !=&bridge)
{delete fundpointer;
fundpointer=bridge.fundpointer->clone();
}
return *this;
}
private:
fund* fundpointer;
};
class LOFfund
{public:
LOFfund(const fundbridge& thefundbridge_,int shares_):thefundbridge(thefundbridge_),shares(shares_)
{}
double getshares() const
{return shares;}
private:
fundbridge thefundbridge;
int shares;
};
自我感觉建立的时候应该是没有什么错的吧。
不过在我想在我定义的函数中调用原来的成员函数getvalue()的时候
它就出bug了

double MonteCarlo_n2(const LOFfund& theloffund,
double Expiry,
double Vol,
double r,
unsigned long NumberOfPaths)
{//首先从fund的派生来获得其现价
double present_price=theloffund.getvalue();//就是这里啊!为什么不能访问呢?这里为什么不能访问呢?
double variance = Vol*Vol*Expiry;
double rootVariance = sqrt(variance);
double itoCorrection = -0.5*variance;
double movedSpot = present_price*exp(r*Expiry +itoCorrection);
double thisfutureprice;
double runningSum=0;
for (unsigned long i=0; i < NumberOfPaths; i++)
{
double thisGaussian = GetOneGaussianByBoxMuller2();
thisfutureprice = movedSpot*exp( rootVariance*thisGaussian);
runningSum += thisfutureprice;
}
double mean = runningSum / NumberOfPaths;
mean *= exp(-r*Expiry);
double future_price= mean;
return future_price;
}
double Expiry,
double Vol,
double r,
unsigned long NumberOfPaths)
{//首先从fund的派生来获得其现价
double present_price=theloffund.getvalue();//就是这里啊!为什么不能访问呢?这里为什么不能访问呢?
double variance = Vol*Vol*Expiry;
double rootVariance = sqrt(variance);
double itoCorrection = -0.5*variance;
double movedSpot = present_price*exp(r*Expiry +itoCorrection);
double thisfutureprice;
double runningSum=0;
for (unsigned long i=0; i < NumberOfPaths; i++)
{
double thisGaussian = GetOneGaussianByBoxMuller2();
thisfutureprice = movedSpot*exp( rootVariance*thisGaussian);
runningSum += thisfutureprice;
}
double mean = runningSum / NumberOfPaths;
mean *= exp(-r*Expiry);
double future_price= mean;
return future_price;
}
跪求解决啊啊啊啊!谢大神啊