Link to home
Start Free TrialLog in
Avatar of nicholaf
nicholaf

asked on

URGENT bu quick help on splitting a C++ file into a header and a main file

Dear Experts,

I am in a time crunch and would love it if you guys can help me sift through this code and split this code into 2 or more files: header files for the class definitions and a file to hold the main function.  Also can you please explain what #define does?

Thanks!





#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <math.h>

#define ERFCC_1  -1.26551223
#define ERFCC_2   1.00002368
#define ERFCC_3   0.37409196
#define ERFCC_4   0.09678418
#define ERFCC_5  -0.18628806
#define ERFCC_6   0.27886807
#define ERFCC_7  -1.13520398
#define ERFCC_8   1.48851587
#define ERFCC_9  -0.82215223
#define ERFCC_10  0.17087277

double erfcc(double x)
{

double t,z,ans;

z=fabs(x);

t=1.0/(1.0+0.5*z);

ans=t*exp(-z*z+ERFCC_1+t*
              (ERFCC_2+t*
              (ERFCC_3+t*
              (ERFCC_4+t*
              (ERFCC_5+t*
              (ERFCC_6+t*
              (ERFCC_7+t*
              (ERFCC_8+t*
              (ERFCC_9+t*
               ERFCC_10)))))))));

return  x >= 0.0 ? ans : 2.0-ans;
}

double CumStandardNormal(double x)
// The cumulative standard normal distribution
{
return( (1.0 - 0.5*erfcc(x/sqrt(2.0))) );
}

double CumNormal(double x, double mean, double stddev)
// The cumulative normal distribution
{
return(CumStandardNormal((x-mean)/stddev));
}

//4.)      Create a call option class that inherits from the stock option class.
//Research the Black Scholes option pricing formula and allow the user to set
// the necessary parameters to calculate the price of the call option.
class stock_option
{
public:
      stock_option(      std::string stock_symbol,std::string stock_option_symbol,
                              double stock_open_price,double stock_closing_price,
                    double stock_strike_price,int stock_matuity_moth,
                    int stock_matuity_year);
private:
      std::string symbol;
      std::string option_symbol;
      double open_price;
      double closing_price;
    double strike_price;
    int matuity_moth;
    int matuity_year;
};


stock_option::stock_option(std::string stock_symbol,std::string stock_option_symbol,
                        double stock_open_price,double stock_closing_price,
                double stock_strike_price,int stock_matuity_moth,
                int stock_matuity_year):
                symbol(stock_symbol),option_symbol(stock_option_symbol),
                open_price(stock_open_price),closing_price(stock_closing_price),
                strike_price(stock_strike_price),matuity_moth(stock_matuity_moth),
                matuity_year(stock_matuity_year){};

class call_option :public stock_option
{
public:
call_option(std::string stock_symbol,std::string stock_option_symbol,
                        double stock_open_price,double stock_closing_price,
                double stock_strike_price,int stock_matuity_moth,
                int stock_matuity_year):
                stock_option(stock_symbol,stock_option_symbol,
                        stock_open_price,stock_closing_price,stock_strike_price,
                stock_matuity_moth,stock_matuity_year){};
double GetC();
void sett(double tin){t=tin;};
void setK(double Kin){K=Kin;};
void setr(double rin){r=rin;};
void setS(double Sin){S=Sin;};
void setsigma(double sigmain){sigma=sigmain;};
private:
      double x();
      double t;//time to expiration: 95 days (95/365) must be used
      double K;//strike price: 80
      double r;//risk free rate: 4.5%
      double S;//underlying stock price 88.42
    double sigma;//volatility of stock: 20%
};

double e = exp(1);

double call_option::x()
{
      return (log(S/K)+(r+sigma*sigma/2)*t)/(sigma/sqrt(t));
};

double call_option::GetC()
{
      return S*CumStandardNormal(x())+K*exp(-r*t)*CumStandardNormal(x()-sigma*sqrt(t));
};

int main(int argc, char* argv[])
{
      call_option IBMOption("IBM","IBMAP.X",9.4,9.2,80,1,2004);
    IBMOption.sett(95.0/365);
      IBMOption.setK(80);
      IBMOption.setr(4.5);
      IBMOption.setS(88.42);
      IBMOption.setsigma(20);
    std::cout<<"Test data result is "<<IBMOption.GetC()<<std::endl;
      double t;
    std::cout<<"Enter tha value of t"<<std::endl;
    std::cin>>t;
    IBMOption.sett(t);
      double K;
    std::cout<<"Enter tha value of K"<<std::endl;
    std::cin>>K;
    IBMOption.setK(K);
      double r;
    std::cout<<"Enter tha value of r"<<std::endl;
    std::cin>>r;
    IBMOption.setr(r);
      double S;
    std::cout<<"Enter tha value of S"<<std::endl;
    std::cin>>S;
    IBMOption.setS(S);
    double sigma;
    std::cout<<"Enter tha value of sigma"<<std::endl;
    std::cin>>sigma;
    IBMOption.setsigma(sigma);

    std::cout<<"The result is "<<IBMOption.GetC()<<std::endl;
      return 0;
}

Avatar of jkr
jkr
Flag of Germany image

Sorry, but

//4.)     Create a call option class that inherits from the stock option class.
//Research the Black Scholes option pricing formula and allow the user to set
// the necessary parameters to calculate the price of the call option.

pretty obviously sounds like homework - why don't you start and get back with your problems (if any)?
Avatar of nicholaf
nicholaf

ASKER

I'm actually help my friend grade this - but he is overworked and i'm trying to help - although my c++ is not very strong.  can you help?
forget it

i did it already
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dexstar - for future reference: Doing homework Qs grouds for removal from this site.
hey this is not homework.  this is a legit question because i'm helping my friend grade some homework.  he's desperate and i'm the only one he knows with any experience in programming.  i'm asking for your help so that i can split this so that he can more clearly grade this because i'm not that experienced in c++.  but i'm the only help that he's got.  

thanks so much Dexstar.  I'll stand behind you if any other comments come from people.  clearly you saw the difference between my situation and a student who just wants a good grade.  
jkr:

Yeah, I wouldn't intentionally do someone's homework for them.  I just couldn't conceive of a teacher handing their students that code and then giving them the assignment of breaking it up into separate files.  It seemed to me that the asker already had done the vast majority of the work, and just needed some help putting the final touches on it.

If they had posted this part:
     4.)     Create a call option class that inherits from the stock option class.
     Research the Black Scholes option pricing formula and allow the user to set
     the necessary parameters to calculate the price of the call option.

And then asked how to do it, I wouldn't've been so accomodating.  And besides, unless it was that obvious, how could I know if it was homework or not?

nicholaf:  Glad that helped you.

Dex*