A question about values in member variables

In my program, I have a class with two subclasses. The superclass has a function that adds the value of its two private member variables and assigns it to a protected member variable with the use of a public member function. That variable is then used to assign its value to a member variable of one of the subclasses in another function, like so:

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
class Foo
{
public:
  void fooFunct();
protected:
  int protVar;
private:
  int priVar1, priVar2;
};

void Foo::fooFunct()
{
  fooVar1 = rand() % 6 + 1;
  fooVar2 = rand() % 6 + 1;
  protVar = fooVar1 + fooVar2;
}

class Bar: public Foo
{
public:
  void barFunct();
private:
  int barVar;
};

void Bar::barFunct()
{
  barVar = protVar;
}


Here's the deal. When a Bar class object is created, its constructor will call fooFunct() first, and barFunct() after. fooFunct seems to work just fine, I've checked the variables by cout'ing them and they show all the correct values. However, when I useprotVar in barFunct(), it somehow changes value so that it always has the value 9.

Is there something I am not thinking about? It can't be the rand() functions because the value 9 is displayed every time I rebuild and rerun the program, and I always have the console print out the values of fooVar1, fooVar2 and protVar after they're used in fooFunct(). The only thing I can see as the reason is if protVar becomes a "different variable" when used by a function of a Bar class object. As in, I would have to explicitly call protVar from the Foo class object by doing something like this:

aFooObject.get_protVar();

So the question is, why is protVar acting up?
Last edited on
Here's what your code could be:
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
#include <iostream>
#include <ctime>

class Foo{
public:
	void fooFunct();
protected:
	int protVar;
private:
	int priVar1, priVar2;
};

void Foo::fooFunct(){
	priVar1 = rand() % 6 + 1;
	priVar2 = rand() % 6 + 1;
	protVar = priVar1 + priVar2;
	std::cout << priVar1 << " + " << priVar2 << " = " << protVar;
}

class Bar: public Foo{
public:
	Bar();
	void barFunct();
private:
	int barVar;
};

Bar::Bar(){
	fooFunct();
	barFunct();
	std::cout << "\n-> " << barVar;
}

void Bar::barFunct(){
	barVar = protVar;
}

int main(){
	srand(time(0));
	Bar b;
	std::cin.get();
	return 0;
}

It works.

Try compiling this.
Compare it to your own code.

Good luck.
I must be blind. I can't see any difference :/

Edit: Except for the fact that I accidentally mixed priVar and fooVar together, but that's irrelevant as they're definitely not mixed up in my code.
Last edited on
Post yours.
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
#include <iostream>
#include <ctime>

class Table {
public:
  Table () {}
  Table (const Table& aTable) {}
  ~Table () {}
  void DiceRoll();
protected:
  int myDicePair;
private:
  int myFirstDice, mySecondDice;
};

class EvenOrOdd: public Table {
public:
  EvenOrOdd(Table&);
  EvenOrOdd(const EvenOrOdd& EvenOdd) {}
  ~EvenOrOdd() {}
  void DecisionCheck();
private:
  int myWin;
};

void Table::DiceRoll() {
  myFirstDice = rand() % 6 + 1;
  mySecondDice = rand() % 6 + 1;
  myDicePair = myFirstDice + mySecondDice;
  
  std::cout << "\n First dice: \t" << myFirstDice;
  std::cout << "\n Second dice:\t" << mySecondDice;
  std::cout << "\n Both dice: \t" << myDicePair << std::endl;
}

void EvenOrOdd::DecisionCheck() {
  myWin = myDicePair % 2;
  std::cout << myWin;
}

EvenOrOdd::EvenOrOdd(Table& aTable) {
  DiceRoll();
  DecisionCheck();
}

int main () {
  srand( time(0) );
  Table aTable;
  EvenOrOdd newGame(aTable);
}


Last edited on
Worked fine for me.
Exactly. It's working for me too. But together with the rest of the program, it doesn't work the way it's supposed...
I seed it at the beginning of my main function, and the rand() function should not do its randomizer thing until the appropriate class object is created with its constructor.

And I ran a test by printing it out and apparently, myWin, myDicePair and myDicePair % 2 all show. So it's like they all get these values when they're used in another function.

And finally, it seems like it doesn't like that I put srand() in the global scope.

EDIT: Whoever posted before me removed his/her comment.
Last edited on
Actually, I think I have figured it out!

Here is how my constructor USED to look like:

1
2
3
4
5
6
7
8
EvenOrOdd::EvenOrOdd(Player& aPlayer, Table& aTable)
do {
    aPlayer.Betting();
    SetDecision();
    aTable.DiceRoll();
    DecisionCheck(aPlayer);
    ResultCheck(aPlayer);
  } while (mySelection == 1 && aPlayer.GetMoney() > 0);


And this is what it looks like now:

1
2
3
4
5
6
7
8
EvenOrOdd::EvenOrOdd(Player& aPlayer)
do {
    aPlayer.Betting();
    SetDecision();
    DiceRoll();
    DecisionCheck(aPlayer);
    ResultCheck(aPlayer);
  } while (mySelection == 1 && aPlayer.GetMoney() > 0);


I think what's happening in the first code snippet is that the constructor is first using a Table class object to call the DiceRoll function. But the values produced in that functions are stored in the variables tied to that object. The function after try using a different set of variables that belong to the EvenOrOdd class object.

In the second code snippet, all relevant functions are all ones that belong to the EvenOrOdd class object!

Does that sound right?
Last edited on
yes
Topic archived. No new replies allowed.