Multiple inheritance
Just as for European options, we can distinguish subclasses of American options according to the payoff, such as puts, calls, digital calls, bull spreads and many others. For now we restrict ourselves to puts and calls.
In Section 2.2 we saw how to use inheritance in C++ to set up subclasses. A new feature here is that an option with a particular payoff such as a put can be either of European or American type. This can be modelled in C++ by means of multiple inheritance. In addition to the EurOption class, we define a new AmOption class, and make Put a subclass of both the EurOption and AmOption classes. Similarly, Call (and possibly more classes for other payoffs) will also be inheriting from both the EurOption and AmOption classes, as in Figure 3.1. The code to capture this structure follows below.
Listing 3.1 Options07.h
- #ifndef Options07_h
- #define Options07_h
-
- #include “BinModel02.h”
-
- class EurOption
- {
- private:
- int N; //steps to expiry
- public:
- void SetN(int N_){N=N_;}
- virtual double Payoff(double z)=0;
- //pricing European option
- double PriceByCRR(BinModel Model);
- };
-
- class AmOption images
- {
- private:
- int N; //steps to expiry
- public:
- void SetN(int N_){N=N_;}
- virtual double Payoff(double z)=0;
- //pricing American option
- double PriceBySnell(BinModel Model); images
- };
-
- class Call: public EurOption, public AmOption images
- {
- private:
- double K; //strike price
- public:
- void SetK(double K_){K=K_;}
- int GetInputData( );
- double Payoff(double z);
- };
-
- class Put: public EurOption, public AmOption images
- {
- private:
- double K; //strike price
- public:
- void SetK(double K_){K=K_;}
- int GetInputData( );
- double Payoff(double z);
- };
-
- #endif
复制代码