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;
}

nicholafAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
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)?
0
nicholafAuthor Commented:
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?
0
nicholafAuthor Commented:
forget it

i did it already
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

DexstarCommented:
nicholaf:

> 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.

This is your header file (Call it Class.h):

      #ifndef _CLASS_H_
      #define _CLASS_H_

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

      double erfcc(double x);
      double CumStandardNormal(double x);
      double CumNormal(double x, double mean, double 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;
      };

      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%
      };

      #endif // #ifdef _CLASS_H_


This is your object .CPP file.  Call it "Class.cpp":

      #include "Class.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));
      }

      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){};

      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));
      };


This is your Main.cpp file:
      #include "Class.h"


      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;
      }


> Also can you please explain what #define does?

#define is a preprocessor macro.  The compiler does like a search/replace in your code for each #define when it is compiled.

So, if you do this:
     #define X_VALUE     100

     int x = X_VALUE;

The compiler changes it to this:
     int x = 100;


Hope that helps,
Dex*
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Dexstar - for future reference: Doing homework Qs grouds for removal from this site.
0
nicholafAuthor Commented:
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.  
0
DexstarCommented:
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*
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.