高级继承与收益类层次结构
立即解锁
发布时间: 2025-08-21 00:25:58 阅读量: 1 订阅数: 4 


C++ for Financial Engineers: An Object-Oriented Approach
# 高级继承与收益类层次结构
## 1. 收益函数计算示例
首先,我们来看一些基本的收益函数计算示例。以下是计算普通看涨期权收益的代码:
```cpp
CallPayoff call(20.0);
cout << "Give a stock price (plain Call): ";
double S;
cin >> S;
cout << "Call Payoff is: " << call.payoff(S) << endl;
```
接下来,我们创建一个牛市价差期权收益的示例。牛市价差期权有四个成员数据,分别是两个执行价格、买入看涨期权的成本和卖出第二个看涨期权的价格。代码如下:
```cpp
double K1 = 30.0; // Strike price of bought call
double K2 = 35.0; // Strike price of sell call
double costBuy = 3.0; // Cost to buy a call
double sellPrice = 1.0; // Sell price for call
BullSpreadPayoff bs(K1, K2, costBuy, sellPrice);
cout << "Give a stock price (BullSpread): ";
cin >> S;
cout << "Bull Spread Payoff is: " << bs.payoff(S) << endl;
cout << "Bull Spread Profit is: " << bs.profit(S) << endl;
```
其中,`profit()` 函数的 C++ 代码如下:
```cpp
double BullSpreadPayoff::profit(double S) const
{ // Profit
return payoff(S) - (buyValue - sellValue);
}
```
## 2. 轻量级收益类
定义基类中的纯虚函数,然后在派生类中实现该函数的方法在实践中是可行的,但存在一些缺点:
- 为每种新的收益函数创建一个“重量级”的派生类。
- 一旦创建了收益类的实例,就无法将其更改为另一个类的实例。例如,在使用 C++ 对选择期权进行建模时,这种方法可能会很困难。
为了解决这些问题,我们采用另一种方法,即策略模式。UML 类图如下:
```mermaid
classDiagram
class PayoffStategy {
<<abstract>>
+payoff(double S) const
}
class Call {
-K: double
+CallStrategy(double strike)
+payoff(double S) const
}
class Bull {
// 省略具体成员
}
class Payoff {
-ps: PayoffStategy*
+Payoff(PayoffStategy& pstrat)
}
PayoffStategy <|-- Call
PayoffStategy <|-- Bull
Payoff *-- PayoffStategy
```
`Payoff` 类的头文件如下:
```cpp
class Payoff
{
private:
PayoffStrategy* ps;
public:
// Constructors and destructor
Payoff(PayoffStrategy& pstrat);
// Other member functions
};
```
策略类的代码如下:
```cpp
class PayoffStrategy
{
public:
virtual double payoff(double S) const = 0;
};
class CallStrategy : public PayoffStrategy
{
private:
double K;
public:
CallStrategy(double strike) { K = strike;}
double payoff(double S) const
{
if (S > K)
return (S - K);
return 0.0;
}
};
```
使用新配置的示例代码如下:
```cpp
// Create a strategy and couple it with a payoff
CallStrategy call(20.0);
Payoff pay1(call);
```
## 3. 超轻量级收益函数
这种设计技术创建一个带有函数指针作为成员数据的类,该函数指针将被分配给表示某种收益函数的“真实”函数。以下是具体的类定义:
```cpp
class OneFactorPayoff
{
private:
double K;
double (*payoffFN)(double K, double S);
public:
// Constructors and destructor
OneFactorPayoff(double strike,
double(*payoff)(double K,double S));
// More ...
double payoff(double S) const; // For a given spot price
};
OneFactorPayoff::OneFactorPayoff(double strike,
double (*pay)(double K, double S))
{
K = strike;
payoffFN = pay;
}
double OneFactorPayoff::payoff(double S) const
{ // For a given spot price
return payoffFN(K, S); // Call function
}
```
使用这个类的步骤如下:
1. 编写你想要使用的收益函数。
2. 使用你选择的收益函数创建 `OneFactorPayoff` 的实例。
3. 测试并使用收益类。
具体的收益函数示例如下:
```cpp
double CallPayoffFN(double K, double S)
{
if (S > K)
return (S - K);
return 0.0;
}
double PutPayoffFN(double K, double S)
{
// max (K-S, 0)
if (K > S)
return (K - S);
return 0.0;
}
```
使用代码示例:
```cpp
int main()
{
OneFactorPayoff pay1(20.0, CallPayoffFN);
cout << "Give a stock price (plain Call): ";
double S;
cin >> S;
cout << "Call Payoff is: " << pay1.payoff(S) << endl;
OneFactorPayoff pay2(20.0, PutPayoffFN);
cout << "Give a stock price (plain Put): ";
cin >> S;
cout << "Put Payoff is: " << pay2.payoff(S) << endl;
return 0;
}
```
## 4. 继承的风险:反例
在创建 C++ 类层次结构时,我们需要注意一些问题。一般来说,从类 B 派生类 D 会在 B 和 D 之间引入依赖关系,即 B 的更改可能会导致 D 的更改。
我们通过一个二维形状的例子来说明继承可能带来的问题。我们考虑矩形和正方形,最初我们可能会认为正方形是矩形的一种特殊情况。以下是矩形类的接口:
```cpp
class Rectangle : public Shape
{
protected: // Derived classes can access this data directly
Point bp; // Base point, Point class given
double h; // height
double w; // width
public:
Rectangle()
{
bp = Point();
h = 1.
```
0
0
复制全文
相关推荐









