Struggling with inheritance (passing variables between classes/functions)

Hi all,
Thanks in advance for taking a look at this, I'm just starting out, so I'm sure the form will make your head hurt. Nevertheless, what I have here is a class that initializes (i.e. asks the uses to input them) several parameters for a relatively complicated calculation. I want to define my classes in a way where a given class consists of closely related functions. Thing is, these initialized parameters will need to be passed back and forth to the different classes that need them. Without further ado, here is a lot of code, look for the bolded bits:
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <math.h>
using namespace std;
class initialize {
	double p,t,ant,mar,y,x,pc,tc,omega;
	int i, comps, j;
	char name;
	public:
		double pressure () {
		cout << "Enter in the pressure of the system" << endl;
		cin >> p;
		return p;}
		double temperature () {
		cout << "Enter in the temperature of the system" << endl;
		cin >> t;
		return t;}
		double crit_t (int i) {
			cout << "Enter the critical temperature, in Kelvin, for component # " << i << endl;
			cin >> tc;
			return tc;}
		double crit_p (int i) {
			cout << "Enter in critical pressure, in bar, for component # " << i << endl;
			cin >> pc;
			return pc;}
		double accentric (int i) {
			cout << "Enter the accentric factor for component # " << i << endl;
			cin >> omega;
			return omega;}
		int numcomps() {
			cout << "Enter in the number of components in the system" << endl;
			cin >> comps;
			return comps;}
		double antoine(int i, int k) {
			if (i == 0)
			{
				name = 'A';
			}
			else if ( i == 1)
			{
				name = 'B';
			}
			else if (i == 2)
			{
				name = 'C';
			}
			else
			{
				name = 'E';
			}
		cout << "Enter the " << name <<  k + 1<< " Antoine Constant" << endl;
		cin >> ant;
		return ant;}
		double margules(int i) {
		cout << "Enter the A" << i+1 << 2-i <<  " Margules constant" << endl;
		cin >> mar;
		return mar;}
		double y_frac (int i) {
		cout << "Enter in the vapor mole fraction for component # " << i + 1<<  endl;
		cin >> y;
		return y;}
		double x_frac (int i) {
		cout << "Enter in the liquid mole fraction for component # " << i + 1 <<  endl;
		cin >> x;
		return x;}
};
class root{
	double a, b, c, d, q, r, delta, s, t, rt[3], rho, theta, rtf;
	public:
		double minima(double a, double b, double c, double d){
			q = (3*a*c-b*b)/(9*a*a);
			r = (9*a*b*c-27*a*a*d-2*pow(b,3))/(54*pow(a,3));
			delta = pow(q,3)+pow(r,2);
			cout << " q " << q << " r " << r << " delta " << delta << endl;
			if (delta >= 0 )
			{
				cout << "1 root" << endl;
				s = pow((r + sqrt(delta)),1.0/3.0);
				s = s*(r + sqrt(delta))/abs((r + sqrt(delta)));
				t = pow(abs((r - sqrt(delta))),1.0/3.0);
				t = t*(r - sqrt(delta))/abs((r - sqrt(delta)));
				rt[0] = s+t-b/(3*a);
				rtf = rt[0];
			}
			else if (delta < 0)
			{
				cout << "3 roots" << endl;
				rho = -pow(q,3);
				rho = sqrt(rho);
				theta = acos(r/rho);
				rt[0] = 2*pow(rho,1.0/3.0)*cos(theta/3.0)-b/(3*a);
				rt[1] = -pow(rho,1.0/3.0)*cos(theta/3.0)-b/(3*a)-sqrt(3.0)*pow(rho,1.0/3.0)*sin(theta/3.0);
				rt[2] = -pow(rho,1.0/3.0)*cos(theta/3.0)-b/(3*a)+sqrt(3.0)*pow(rho,1.0/3.0)*sin(theta/3.0);
				cout << rt[0] << "\t" << rt[1] << "\t" << rt[2] << endl;

			}

				cout << rt[0] << endl;
			return 0;
		}
};



class vle {
	initialize prop;
	double p, t, aant[2],bant[2], cant[2], mar[2], y[2],pc[2],tc[2],omg[2];
	int i, m, j;
	public:
	double dew_t() {
		/*Feed in system presseure for Dew T calc */
		p = prop.pressure();
		/*Specify number of components*/
		m = prop.numcomps();
		/*Supply gas phase compositions, Antoine constants, Margules' parameters, and critical property data for Dew T calc */
		for (i = 0; i<= m-1; ++i)
		{
			y[i] = prop.y_frac(i);
			mar[i] = prop.margules(i);
			tc[i] = prop.crit_t(i);
			pc[i] = prop.crit_p(i);
			omg[i] = prop.accentric(i);
			for (j = 0; j <=2; ++j)
			{
				if (j == 0)
				{
					aant[i] = prop.antoine(j,i);
				}
				else if ( j==1 )
				{
					bant[i] = prop.antoine(j,i);
				}
				else if ( j == 2)
				{
					cant[i] = prop.antoine(j,i);
				}
			}
		}
		cout << "Pressure: " << p << " Mole fractions " << y[0] << '\t' << y[1] << endl;
		for (i=0; i<= m-1; ++i)
		{
			cout << "Antoine constants A" << i +1 << '\t' << aant[i] << " B" << i+1 << '\t' << bant[i] << '\t' << "C" << i+1 << '\t' << cant[i] << endl;
			cout << "Margules constant A" << i+1 << 2-i << "\t" << mar[i] << endl;	
		}
		return 0;
	}
	int return_m () {
	return m; 
	}
}param; 

class fug_coeff {
	vle param;
	root min;
	double phi,phisat,bigphi;
	public:
	double big_phi_init()
	{
		bigphi = 1.0;
		return bigphi;
	}
};
class preos_params: public vle {
	int n;
	public:
	double get_m()
	{
		n = param.return_m();
		cout << n << endl;
		return 0;
	}
}prparam;

int main () {
	/*cout <<"Enter cubic parameter" << endl;*/
	/*cin >> a >> b >> c >> d;*/
  vle param;
  root min;
  /*min.minima(a,b,c,d);*/
  param.dew_t(); 
  prparam.get_m();
  return 0;
}

Fortunately, most of that doesn't apply to any of you guys. The two classes in question are "vle" and "preos_params". Basically, I want to pass the variable "m" from the dew_t() function (in class vle) to class preos_params, where I will have several functions that utilize m. I DO NOT want to pass the entire dew_t function, or rather, I do not want the user to have to input values everytime a function in preos_param that uses m is invoked. Thoughts?

This is all crossed over and confused.!
Ha! Perhaps so. Any critiques on my form are welcome as well. I know it doesn't look that way, since my arrays are only holding 2 values at the moment, but the reason the class "vle" and "initialize" are written as they are, is because I need the program to prompt the user to enter the properties m times (m being an integer number the user also specifies), resulting in potentially large arrays of parameters. The best way I knew how to do this was code up a piece that queried the user once (initialize) and another that repeats the process m times (the dew_t function in vle). The many functions in initialize are an aesthetic choice (i.e. how I want the questions to be asked to the user). For what it's worth, This part of the code works fine, but if you know of a better way, I'm all ears. Admittedly, I'm forcing the use of classes, because I'm trying to teach myself OOP. This might not be (and is probably not), the best way to go.

To reiterate my question (which I think has a simple answer), how to I get the private variable m(or a copy, preferably), from line 113 to the preos_params class (lines 162-172)?

After reading on this more, my formal question is: Can you do a pass by value between classes? If so, how?
Last edited on
All other messes aside, you could either declare vle::m protected and access it directly in your preos_params object (assuming that dew_t() has already initialized it) or add a "get" method to vle.
Thanks Seymore15074, I think suggestion #1 is probably the way to go. I'd been sniffing down that path, but with declaring the variables public. Given that practically every parameter in vle is needed elsewhere, that pretty much renders doing this in a OOP style pointless. That's why I came here.

In short, I tried to implement that in the waining minutes of the workday yesterday, and the class inside of preos_param prints a '0' for m after I tell the program that it is '2' (and this result is also printed out previously). Obviously, I've fouled something up.

I think the larger problem here is my implementation is all backwards. MANY of the parameters in vle are constants, so the best thing would probably be to assign them as global constants. I know "global variables" is a bad word(s) in the C++ domain (in the spirit of learning C++, I've been avoiding them). For this particular algorithm, either I do that so every function can have access to them (as pretty much every function would need them), have a complex system of friendship (which is what I tried to implement before), or just write one big sprawling code (I'm a chemist, not a programmer, don't put it past me to do the latter!).

Anyways, thanks for your input. I think I'll scrap everything but the cubic solver and try again. Take care.
Topic archived. No new replies allowed.