Inheritance using Stack Class Problem
I am having problems implementing a SearchableStack Class as a subclass of Stack class.
Here is the design:
List Class -->(composition)-->Stack Class-->(inheritance)-->SearchableStack Class
Ok, we are not allowed to use "protected" fields, thus not allowing me to access the private data members of the parent class (yes this is stupid, but so is college).. We do this for security reasons, so I have to use the public functions in which I created for for the Stack to achieve the search..
My code is not returning correct results, when I search for something in the stack, and it is returned - the stack for some reason now has a 0 (zero) in the last stack memory location. And other various bugs.
Any help would be greatly appreciated.
SearchableStack.h
Code:
#include "Stack.h"
using namespace std;
class SearchableStack : public Stack
{
public:
SearchableStack();
bool find(int);
private:
Stack tempStack;
}
SearchableStack find function:
Code:
bool SearchableStack::find(int n)
{
int temp;
//Stack tempStack2;
cout<<"In Find"<<endl;
while(!isEmpty())
{
cout<<"in FIRST while"<<endl;
temp = pop();
tempStack.push(temp);
if(temp == n)
{
cout<<"in FIRST if"<<endl;
while(!tempStack.isEmpty())
{
cout<<"in SECOND while"<<endl;
push(tempStack.pop());
}
return true;
}
}
while(!tempStack.isEmpty())
{
cout<<"in THIRD while"<<endl;
push(tempStack.pop());
}
cout<<"@@@ END OF FUNCTION hmm, no return before here?"<<endl;
return false;
}
ALSO, I tried not haveing the private data member tempStack -- and just created one in the member function, and that was no dice either..
Thanks