I then use CRectangle::CRectangle(vector<double> _S0,double _U,double _D,double _R) to define it and define the variables. How would I then call these values from a separate function?
/#include <iostream>
#include <cmath>
#include <vector>
usingnamespace std;
class BinModel
{
private:
vector<double> S0;
double U;
double D;
double R;
public:
BinModel (vector<double> _S0, double _U, double _D, double _R);
};
double RiskNeutProb(BinModel& Model);
BinModel::BinModel (vector<double> _S0, double _U, double _D, double _R)
{
S0 = _S0;
U=_U;
D=_D;
R=_R;
for (size_t i = 0; i < S0.size(); i++)
{
cout << "S[" << i << "]=" << S0[i];
cout << endl;
}
cout << "Enter U: "; cin >> U;
cout << "Enter D: "; cin >> D;
cout << "Enter R: "; cin >> R;
cout << "U= " << U << endl;
cout << "D= " << D << endl;
cout << "R= " << R << endl;
};
double RiskNeutProb(BinModel& Model)
{
return (S0[0]-D)/(U-D);
}
int main ()
{
vector<double> S0(2); //declare that S0 is a vector with two elements
for (int i = 0; i < 2; i++)
{
cout << "Enter s0 " << i+1
<< ": " << flush;
cin >> S0[i];
cout << endl;
}
double U, D, R;
BinModel Model(S0,U,D,R); //Create BinModel object called Model by calling the constructor
cout << "p=" << RiskNeutProb(S0,U,D,R);
return 0;
}/
This is the full code, however it is telling me that S0, D,U aren't declared in RiskNeutProb. Can you tell me why this is? I'm trying to use the members defined in the constructor function above.
It's because RiskNeutProb is not a member of BinModel. You can only access private members from inside the class itself.
To use RiskNeutProb you have three options:
1) Make S0, D, and U public and do this: return (Model.S0[0]-Model.D)/(Model.U-Model.D)
2) Make RiskNeutProb a public function in the BinModel class
3) Make "get" or "accessor" functions that let you access the private members S0, D and U like I wrote above.
Ok that's fine that's actually what I thought. However this brings me on to another issue, I would like to introduce another function in the class BinModel. It will produce another vector using some of the members of the class. Can I then call the answer of this in my function RiskNeutProb?