Calling the variables from a constructor function in a function outside the class

1
2
3
4
5
6
7
8
9
/class CRectangle {
private:
double S0;
double U;
double D;
double R;
public:
CRectangle (vector<double> _S0,double _U,double _D,double _R);
};/


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?
and define the variables

Initialize the members, you mean.

How would I then call these values from a separate function?

You can't call values. What values anyway? What function?
What do you want to do?
Last edited on
Here's an initialization list to set your parameters:
And here are some get functions to get your values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class CRectangle {
private:
    vector<double> S0;
    double U;
    double D;
    double R;
public:
    //CTOR
    CRectangle (double _S0, double _U, double _D, double _R) : //Header
        U(_U), D(_U), R(_R) //Initialization list for normal parameters
        {  S0.push_back(_S0);  } //Function stuff (for the vector)

    //Get functions
    double get_S0(int i) { return S0[i]; }
    double get_U() { return U; }
    double get_D() { return D; }
    double get_R() { return R; }
};
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/#include <iostream>
#include <cmath>
#include <vector>
using namespace 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.
Last edited on
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?
For this functionality you once again need to do one of two things:
1) Make RiskNeutProb a function in the class
2) make the vector public
Topic archived. No new replies allowed.