Solved

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

Posted on 2003-10-30
7
228 Views
Last Modified: 2010-04-01
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;
}

0
Comment
Question by:nicholaf
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9652067
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
 

Author Comment

by:nicholaf
ID: 9652096
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
 

Author Comment

by:nicholaf
ID: 9652209
forget it

i did it already
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 19

Accepted Solution

by:
Dexstar earned 500 total points
ID: 9652237
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
 
LVL 86

Expert Comment

by:jkr
ID: 9652245
Dexstar - for future reference: Doing homework Qs grouds for removal from this site.
0
 

Author Comment

by:nicholaf
ID: 9652308
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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9653343
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 322
Template syntax for variable length arrays 9 75
Dynamically allocate memory 9 64
How do i run a c++ file? 15 47
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question