declaring and accessing arrays in classes

Any help here would be most appreciated.

We are learning how to use composition. In this project there is the Main (), resistor .cpp and .h, capacitor .cpp and .h, and filter .cpp and .h. I have 4 arrays built in the resistor.cpp that I want to print through the main(). I put the arrays in the constructor and have watched them initialize with the proper values, but as soon as the program leaves the constructor the values are wiped away and the elements are left with the value " -858993460 " in all of the them.
Here is the code, it's pretty chopped up because I'm trying to just get this piece to work. I had to cut even more out to fit it in the post.

Filter Main()
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
#include "Capacitor.h"
#include <conio.h>
#include "Filter.h"
#include <iostream>
#include "Resistor.h"
#include <windows.h>
#include <iomanip>

using namespace std;

void clear_screen(void);

void main(void)
{
 int i;
 Resistor resObj;
 Capacitor capObj;
 Filter filt1;
 Resistor res1(4700.0, 0.10);
 Capacitor cap1(0.000012, 0.10);
 cout << setiosflags(ios::fixed)
  << setiosflags(ios::right)
  << setiosflags(ios::showpoint)
  << setprecision(3);
 cout << endl << endl;
cout << setiosflags(ios::fixed)
   << setiosflags(ios::right)
   << setiosflags(ios::showpoint)
   << setprecision(3);
 cout << endl << endl;
 cout << "Nominal Resistance Value =   " << setw(10) << resObj.getResistance() << " ohms" << endl;
 cout << "Resistor Tolerance Value =   " << setw(10) << resObj.getResTolerance()*100 << " Percent" << endl;
 cout << "Maximum Resistance Value =   " << setw(10) << resObj.getMaxResistance() << " ohms" << endl
 cout << "Minimum Resistance Value =   " << setw(10) << resObj.getMinResistance() << " ohms" << endl;
 cout << endl << endl;
 cout << setiosflags(ios::fixed)
   << setiosflags(ios::showpoint)
   << setiosflags(ios::left)
   << setprecision(3);
 cout << "Nominal Capacitance Value =  " << setw(10) << capObj.getCapacitance() *1000000<< " micro Farads" << endl;
 cout << "Capacitor Tolerance Value =  " << setw(10) << capObj.getCapTolerance()*100 << " Percent" << endl;
 cout << "Maximum Capacitance Value =  " << setw(10) << capObj.getMaxCapacitance() * 1000000 << " micro Farads" << endl;
 cout << "Minimum Capacitance Value =  " << setw(10) << capObj.getMinCapacitance() * 1000000<< " micro Farads" << endl;
 cout << endl << endl;
 do
	 {
	  cout << "Current resistor tolerance = " << (resObj.getResTolerance()*100) << " %" << endl;
	  cout << "Valid tolerances are 1%, 2%, 5% or 10%" << endl << endl;
	  cout << "Enter 1, 2, 5 or 10:  ";
	  cin >> tempValue;
	  cout << endl;
	 }
 while(tempValue != 1 && tempValue != 2 && tempValue != 5 && tempValue != 10);

 cout << "The tolerance is set to " << tempValue << " %" << endl << endl;
 resObj.setResTolerance(tempValue);

 cout << "Enter new resistor value for the filter" << endl;
 cout << "Valid resistance values for a " << resObj.getResTolerance() << " % resistor are: " << endl;
 
	if (resObj.getResTolerance() == 10)	{
		for( i = 0; i < 12; i++)
		   {
			if(!(i % 10))
			 cout << endl;
			 cout << resObj.getNominalE12Values(i) << "   ";
		   }
	}
	if (resObj.getResTolerance() == 5)	{
		for( i = 0; i < 24; i++)
		   {
			if(!(i % 10))
			 cout << endl;
			 cout << resObj.getNominalE24Values(i) << "   ";
		   }
	}
	if (resObj.getResTolerance() == 2)	{
		for( i = 0; i < 48; i++)
		   {
			if(!(i % 10))
			 cout << endl;
			 cout << resObj.getNominalE48Values(i) << "   ";
		   }
	}
}


Resistor.h
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
#pragma once
#include "Filter.h"
#include "Capacitor.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <array>

using namespace std;

class Resistor
{
private:
	double resTol, resNom, maxRes, minRes;
	int i;
	bool validEIA;
	int E12, E24, E48, E96;
	int nominalE12Values[12];
	int nominalE24Values[24];
	int nominalE48Values[48];
	int nominalE96Values[96];

public:
	Resistor();
	Resistor (double,double);
//	Resistor(const int, const int, const int, const int);
	double getResistance(void);
	double getResTolerance(void);
	double getMaxResistance(void);
	double getMinResistance(void);
	int getNominalE12Values(int);
	int getNominalE24Values(int);
	int getNominalE48Values(int);
	int getNominalE96Values(int);
	void setArray (void);
	bool setResistance(double);
	void setResTolerance(double);
};


Resistor.cpp
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
#include "Resistor.h"

using namespace std;

Resistor::Resistor()	{
	setArray ();
}
Resistor::Resistor (double a, double b)	{
		resNom = a;
		resTol = b;
}

double Resistor::getResistance(void)	{
	return resNom;
}

double Resistor::getResTolerance(void)	{
	return resTol;
}

double Resistor::getMaxResistance(void)	{
	maxRes = (resNom * (1.0 + resTol));
	return maxRes;
}

double Resistor::getMinResistance(void)	{
	minRes = (resNom * (1.0 - resTol));
	return minRes;
}

void Resistor::setArray (void)	{
	//EIA Standard array element sizes per tolerance class
	const int E12 = 12;	//E12    10% tolerance
	const int E24 = 24;	//E24     5% tolerance
	const int E48 = 48;	//E48     2% tolerance
	const int E96 = 96;	//E96     1% tolerance
//	10% standard values
	const int nominalE12Values[E12] =
	 { 100, 120, 150, 180, 220, 270,
	  330, 390, 470, 560, 680, 820};

	//5% standard values
	const int nominalE24Values[E24] =
	 { 100, 110, 120, 130, 150, 160, 180, 200, 220, 240,
	  270, 300, 330, 360, 390, 430, 470, 510, 560, 620,
	  680, 750, 820, 910};

	//2% standard values
	const int nominalE48Values[E48] =
	 { 100, 105, 110, 115, 121, 127, 133, 140, 147, 154,
	  162, 169, 178, 187, 196, 205, 215, 226, 237, 249,
	  261, 274, 287, 301, 316, 332, 348, 365, 383, 402,
	  422, 442, 464, 487, 511, 536, 562, 590, 619, 649,
	  681, 715, 750, 787, 825, 866, 909, 953};
}
bool Resistor::setResistance (double a)	{

	validEIA = false;
  if(resTol == 10)  {
   for( i = 0; i < 12; i++)   {
    if(a == nominalE12Values[i])    {
     validEIA = true;
	 resNom = a;
    }
   }
  }
  else if(resTol == 5)  {
   for( i = 0; i < 24; i++)   {
    if(a == nominalE24Values[i])    {
     validEIA = true;
	 resNom = a;
    }
   }
  }
  else if(resTol == 2)  {
   for( i = 0; i < 48; i++)    {
    if(a == nominalE48Values[i])	{
     validEIA = true;
	 resNom = a;
    }
   }
  }
  else	  {
   for( i = 0; i < 96; i++)    {
    if(a == nominalE96Values[i])    {
     validEIA = true;
	 resNom = a;
    }
   }
  }
  return validEIA;
}
void Resistor::setResTolerance (double a)	{
	resTol = a;
}

int Resistor::getNominalE12Values(int i)	{
	return nominalE12Values[i];
}


int Resistor::getNominalE24Values(int i)	{
	return nominalE24Values[i];
}

int Resistor::getNominalE48Values(int i)	{
	return nominalE48Values[i];
}

int Resistor::getNominalE96Values(int i)	{
		return nominalE96Values [i];
	}


Capacitor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include "Filter.h"
#include "Resistor.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

class Capacitor
{
private:
	double capTol, capNom, maxCap, minCap;
public:
	Capacitor(void);
	Capacitor (double,double);
	double getCapacitance(void);
	double getCapTolerance(void);
	double getMaxCapacitance(void);
	double getMinCapacitance(void);
	void setCapacitance(double);
	void setCapTolerance(double);
};


Capacitor.cpp
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
#include "Capacitor.h"


Capacitor::Capacitor(void)
{
}

Capacitor::Capacitor (double a, double b)	{
		capNom = a;
		capTol = b;
}
double Capacitor::getCapacitance(void)	{
	return capNom;
}

double Capacitor::getCapTolerance(void)	{
	return capTol;
}

double Capacitor::getMaxCapacitance(void)	{
	maxCap = (capNom * (1.0 + capTol));
	return maxCap;
}

double Capacitor::getMinCapacitance(void)	{
	minCap = (capNom * (1.0 - capTol));
	return minCap;
}

void Capacitor::setCapacitance(double a)	{
	capNom = a;
}
void Capacitor::setCapTolerance (double a)	{
	capTol = a;
}
Last edited on
You are not setting your members, but local variables that happen to share the same name
Sorry, I'm pretty inexperienced, so if you could give me a little more detail or an example maybe I can catch on.

Thanks
1
2
3
4
5
6
7
8
9
10
int x=42;
void foo(){
   int x=54;
}

int main(){
   std::cout << x << '\n';
   foo();
   std::cout << x << '\n';
}
42
42
I'm still not sure what you are telling me. I noticed in your example that the value of x didn't change. I think this is because you need to have the functions after the main. I don't have any functions in main (). All the function are in the classes that I created.
I need to create and initialize an array in one of my classes, then call it later. I got all of the arrays to initialize when the constructor was called, but as soon as the program was finished running through the constructor, the values were lost. I thought I was saving them to private members of the class (which I verified were taking the values when the program was going through the constructor), but the values didn't stay. I have no idea why.
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
#include <string>
#include <iostream>

struct scopeguard
{
    const std::string scopename ;

    scopeguard(const std::string& n) : scopename(n) 
        {std::cout << scopename << " begins\n\n" ;}
    ~scopeguard() 
        {std::cout << scopename << " ends\n\n" ;}
};

int x=42;

void foo() ;

void print( const int & intref )
{
    std::cout << "value: " << intref 
              << "\naddress: " << &intref << "\n\n" ;
}

int main(){
    scopeguard sg("main") ;
    print(x) ;
    foo();
    print(x) ;
}

void foo(){
    scopeguard sg("foo") ;
    print(x) ;
    int x=54;
    print(x) ;
}
main begins

value: 42
address: 0125D000

foo begins

value: 42
address: 0125D000

value: 54
address: 03C4FDB0

foo ends

value: 42
address: 0125D000

main ends


I thought I was saving them to private members of the class (which I verified were taking the values when the program was going through the constructor), but the values didn't stay. I have no idea why.


The why is that you did not save them to private members of the class. As you can see in the code above, the address of the x that is created in foo is not the same as the global x. In other words, they are not the same variable, despite the fact that they share the same name.
Last edited on
I understand what you are saying about x in the function foo not being the same as the global x. So in my case, your are saying that even though I initialized the arrays in the function void setArray void that the values were not passed into the arrays in the private part of Resistor.h.

If I'm understanding correctly, how would I do that? I have tried doing it in the default constructor and it didn't work (at least the way I tried).
Changing the minimal amount of code (presumably there should be values for nominalE96Values here?):

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
#include <algorithm>

void Resistor::setArray (void)	{
	//EIA Standard array element sizes per tolerance class
	const int E12 = 12;	//E12    10% tolerance
	const int E24 = 24;	//E24     5% tolerance
	const int E48 = 48;	//E48     2% tolerance
	const int E96 = 96;	//E96     1% tolerance
//	10% standard values
	const int nominalE12Values[E12] =
	 { 100, 120, 150, 180, 220, 270,
	  330, 390, 470, 560, 680, 820};

	//5% standard values
	const int nominalE24Values[E24] =
	 { 100, 110, 120, 130, 150, 160, 180, 200, 220, 240,
	  270, 300, 330, 360, 390, 430, 470, 510, 560, 620,
	  680, 750, 820, 910};

	//2% standard values
	const int nominalE48Values[E48] =
	 { 100, 105, 110, 115, 121, 127, 133, 140, 147, 154,
	  162, 169, 178, 187, 196, 205, 215, 226, 237, 249,
	  261, 274, 287, 301, 316, 332, 348, 365, 383, 402,
	  422, 442, 464, 487, 511, 536, 562, 590, 619, 649,
	  681, 715, 750, 787, 825, 866, 909, 953};

    std::copy(nominalE12Values, nominalE12Values+E12, this->nominalE12Values) ;
    std::copy(nominalE24Values, nominalE24Values+E24, this->nominalE24Values) ;
    std::copy(nominalE48Values, nominalE48Values+E48, this->nominalE48Values) ;
}
I understand now. I thought the arrays inside the function were referring to the arrays I set in the private part of the class, but............ well you already knew that : ) It worked.

If it's not to much trouble, there is something else I don't understand. In Main(), I instantiated an object of the class Resistor called resObj and one for Capacitor class called capObj. When I use them to try to reference the class in the dot notation to use a function of the class they don't work. But if I use res1 or cap1 I get the values I want back. Why?

What I'm talking about is in lines 31- 34 and 40-43 of the main ().
In the default constructor, you don't initialize or set the values of the variables you're querying in lines 31-34 and 40-43, so they aren't what you might expect in variables that have been default constructed (which resObj and capObj were.)
Topic archived. No new replies allowed.