Polymorphism problem-help please

Hello all,

I am working on a project for my C++ course; I have all of it written out (all the classes, and the main). A little background on the project: I have 3 classes intertwined: one is an abstract base class, and two are derived classes from that base class. First I am to flip a coin to determine whether to instantiate a coin (if the coin lands on heads) or a die (if the coin lands on tails) based on how it lands; I do this in a standalone function and I return a pointer to the base class. With this done, I am to roll/flip whatever was created a certain number of times and display the output of those rolls/flips through a histogram class.

This initially sounded easy to me. However, I am getting an error time after time. After some brief debugging, I determined that the error is with the histogram object when I try to update the bins. This is because there are bogus values for the min and max values that have been passed to it even though I have an accessor to set those values, but it didn't work. So this means that my base class pointer doesn't know the min and max values, even though I have it pointing to the object it initially created..I am at quite a loss here. For a second I thought of instantiating a base class object, but that can't be right; I know there has to be a way to do it through a pointer..

I don't want any code as replies, this is more of a conceptual problem for me..Please let me know if this description wasn't clear enough, and I'll do my best to clarify. Thank you!
What does "my base class pointer doesn't know the min and max values" mean? A pointer is a pointer, and what are max and min? Are they member variables of the base class or what are they? Are you sure you set the correct max and min variables?
That is exactly my problem, that's why I'm confused; I guess I must not be setting them correctly. Here is my standalone function I was talking about in my earlier post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
aRandomNumber* createCoinOrDie()
{
	aRandomNumber *aRandomNumberPtr = 0;
	aCoin coinOrDie(0, 1);
	
	int result = coinOrDie.generate();   //Flips the coin
	if (result == 0)
	{
		aCoin coin(0, 1);
		aRandomNumberPtr = &coin;
		aRandomNumberPtr->setRange(0, 1);
	}
	else
	{
		aDie die(1,6);
		aRandomNumberPtr = ¨
		aRandomNumberPtr->setRange(1, 6);
	}
	return aRandomNumberPtr;
}


I do realize that doing "aRandomNumberPtr->setRange" on the die and coin are redundant, because my default constructor already sets the max and the min values; however I wanted to be extra sure they get set! Perhaps something is wrong with my pointer? I can't see anything wrong with it..but it's possible.

Also in my main function, I call this standalone function with this code:
1
2
      aRandomNumber* create = 0;
      create = createCoinOrDie();

Perhaps, this is incorrect? I will be happy to provide the rest of main if that would help.

Oh and min and max are member variables of the base class, they are inherited to the derived classes die and coin. I use min and max frequently in my class functions; so once I get this resolved it should be smooth sailing.
Last edited on
coin and and die are local variable and will not exist when they go out of scope. createCoinOrDie() will return a dangling pointer (a pointer to a deallocated object). Use dynamic allocation using new to create the objects instead.

Thank you so much Peter, I feel silly for forgetting about scope >.<

I am now calling createCoinOrDie() 5 times in a loop, and I am freeing the memory at the end using
delete create; //frees the memory for a new cycle . However, I noticed that whenever the function creates a die, I get an error about Heap corruption; I do not get this error after it creates coins however. I know this must be because the memory is not getting freed properly for the die. I'm a bit confused as to why. I will continue to play around with it and check my book. But if I can get any hints, that would be great.
Last edited on
You don't call delete inside createCoinOrDie() do you? Call delete when you are not going to use that object again.

aRandomNumber should have a virtual destructor. This is needed for deleteing base class pointers pointing to objects of derived classes to work properly.
No, I am using delete inside the loop where I am using createCoinOrDie() 5 times. I do it at the end of the loop so that the memory is cleared for when the loop starts over at the beginning. From what I understand about freeing memory is that it should be freed after the object is no longer needed. Once I print the histogram in the loop, I do not need it any longer so I free it right there in the loop before it goes on to call createCoinOrDie again. Is this incorrect? Kind of odd that it only frees coin objects but not die objects, even though I am freeing a base class pointer.

I've never used virtual destructors before, so I will read up on it and give it a shot!
Topic archived. No new replies allowed.