Thread: Inheritance using Stack Class Problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    32

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Difficult to say with the limited code posted. It would be nice to see the List and Stack class code as well and also the initial values you are pushing onto the stack and the value you are searching for.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> yes this is stupid, but so is college
    It is proper C++ design to not allow access to the parent class data members.

    >> I tried not haveing the private data member tempStack -- and just created one in the member function
    Even if it didn't solve your problem, it is better to make it local to the function.

    I don't see what the problem is at quick glance. What does the isEmpty() function look like?

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by hk_mp5kpdw
    Difficult to say with the limited code posted. It would be nice to see the List and Stack class code as well and also the initial values you are pushing onto the stack and the value you are searching for.
    Agreed. The algorithm is sound. Substituting std::stack<int> for your Stack works, so the problem is in your Stack class, not SearchableStack.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I don't see your problem, but I was wondering if its better to have it sort of easier to read, or just make it:

    Code:
    bool SearchableStack::find(int n)
    {
    	int temp;
    	//Stack tempStack2;
    
    	cout<<"In Find"<<endl;
    
    	while(!isEmpty() && !(temp == n))
    	{
    		cout<<"in FIRST while"<<endl;
    
    		temp = pop();
    		tempStack.push(temp);
    	}
    
    	while(!tempStack.isEmpty())
    	{
    		cout<<"in SECOND while"<<endl;
    		push(tempStack.pop());
    	}
    
    	if(temp == n)
    		return true;
    
    	cout<<"@@@ END OF FUNCTION hmm, no return before here?"<<endl;
    	return false;
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You'd probably want to initialize temp in that code and make sure you initialize it to something other than n.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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.
    Really would be helpful to see your stack implentation and maybe the list implementation as well.

    My guess is that your tempstack is adding a 0 at the beginning when it's constructed but without the code, it's just a guess.

    Also without knowing more about the stacks public members I can't suggest any alternatives.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Ok, I updated the SearchableStack Class using the local value since its better practice.

    Here is everything you should need - and thanks for helping!

    Test #1 (finding something in the stack):
    I Push: 1, 2, 3, 4 onto the stack.
    Then I Print and its correct: 1234
    Then I find: 3
    Output:
    Enter item to find: 3
    In Find
    in FIRST while
    in FIRST if
    in SECOND while
    Item IS in the Stack!

    But then when I print the Stack after this "find"
    Output:
    Enter item to find: 3
    1
    2
    3
    0

    Yikes!

    Test #2 (find something not in Stack):
    I Push: 1, 2, 3, 4 onto the stack.
    Then I Print and its correct: 1234
    Then I find: 9
    Output:
    Enter item to find: 9
    Enter item to find: 9
    In Find
    in FIRST while
    in FIRST while
    in FIRST while
    in FIRST while
    in THIRD while
    in THIRD while
    in THIRD while
    in THIRD while
    @@@ END OF FUNCTION hmm, no return before here?
    Item is NOT in the Stack!

    Then I print:
    But then when I print the Stack after this "find"
    Output:
    Enter item to find: 3
    3
    2
    1
    0


    My Code:


    SearchableStack.h
    Code:
    //SearchableStack.h
    
    #include "Stack.h"
    
    using namespace std;
    
    class SearchableStack : public Stack
    {
    	public:
    				SearchableStack();
    		bool	find(int);
    
    	private:
    		//Stack tempStack;
    }
    Stack.h:
    Code:
    //Stack.h
    //Stack Wrapper Class
    
    #include <iostream>
    #include "LList.h"
    
    
    class Stack
    {
    	public:
    
    				Stack();
    		bool	isEmpty();
    		bool	isFull();
    		void	push(int);
    		int		pop();
    		void	print();
    
    	protected:
    		int		current;
    		LList	list;
    
    };
    LList.h:
    Code:
    //Linked List
    //LList.h
    
    #include <iostream>
    //#include "Stack.h"
    
    using namespace std;
    
    //const int SIZE = 5;
    
    struct intNode;
    
    typedef intNode* intNodePtr;
    
    struct intNode
    {
    	int value;
    	intNodePtr next;
    };
    
    class LList
    {
    
    	public:
    				LList();
    				~LList();
    		bool	IsEmpty();
    		void	MakeEmpty();
    		int		Length();
    		int		GetItem(int loc);
    		void	Add(int item);
    		bool	DeleteItem(int loc);
    		void    Print();
    
    	private:
    		intNodePtr	list;	
    		int			size;
    };
    SearchableStack.cpp
    Code:
    //SearchableStack.cpp
    
    #include <iostream>
    #include "SearchableStack.h"
    ; //i have no freaking clue...
    using namespace std;
    
    SearchableStack::SearchableStack()
    {
    	while(isEmpty() != true)
    	{
    		pop();
    	}
    }
    
    bool SearchableStack::find(int n)
    {
    	int temp;
    	Stack tempStack2;
    
    	cout<<"In Find"<<endl;
    
    	while(!isEmpty())
    	{
    		cout<<"in FIRST while"<<endl;
    
    		temp = pop();
    		tempStack2.push(temp);
    		
    		if(temp == n)
    		{
    			cout<<"in FIRST if"<<endl;
    
    			while(!tempStack2.isEmpty())
    			{
    				cout<<"in SECOND while"<<endl;
    				push(tempStack2.pop());
    			}
    
    			return true;
    		}
    	}
    
    	while(!tempStack2.isEmpty())
    	{
    		cout<<"in THIRD while"<<endl;
    		push(tempStack2.pop());
    	}
    	cout<<"@@@ END OF FUNCTION hmm, no return before here?"<<endl;
    	return false;
    
    }
    Stack.cpp
    Code:
    //Stack.cpp
    //Stack Wrapper Class
    
    #include "Stack.h"
    
    Stack::Stack()
    {
    	current = 0;
    	list.MakeEmpty();
    
    }
    bool Stack::isEmpty()
    {
    	if(list.IsEmpty() == true)
    		return true;
    	else
    		return false;
    }
    bool Stack::isFull()
    {
    	return false;
    }
    int Stack::pop()
    {
    	if(current == 0)
    		return 0;
    
    	list.DeleteItem(current);
    	current--;
    	return current;
    
    	
    }
    void Stack::push(int item)
    {
    	current++;
    	list.Add(item);
    }
    void Stack::print()
    {
    	list.Print();
    }
    LList.cpp
    Code:
    //Linked List
    //LList.cpp
    
    #include "LList.h"
    
    LList::LList()
    {
    	list = NULL;
    	size = 0;
    }
    
    LList::~LList()
    {
    	MakeEmpty();
    }
    
    bool LList::IsEmpty()
    {
    	if(list == NULL)
    		return true;
    	else
    		return false;
    }
    
    void LList::MakeEmpty()
    {
    	intNodePtr tempPtr;
    
    		while(list != NULL)
    		{
    			tempPtr = list;
    			list = list->next;
    			delete tempPtr;
    		}
    
    	size = 0;
    }
    
    int LList::Length()
    {
    	return size;
    }
    
    int LList::GetItem(int loc)
    {
    	if(loc > size || loc == 0)	// If location is out of the list range
    		return -1;					// Exit the function
    
    	intNodePtr tempPtr;
    	int tempCounter = 1;	//Temp Counter to keep track of how many nodes we have passed
    
    	tempPtr = list;
    
    	if(loc == 1)				//If we are looking for the first item in the list
    		return tempPtr->value;	//Return the value of the first one
    	else
    	{	
    		while(tempCounter < loc)
    		{
    			tempPtr = tempPtr->next;
    			tempCounter++;
    		}
    
    		return tempPtr->value;
    	}
    }
    
    void LList::Add(int item)
    {
    	size++;
    
    	if(list == NULL)
    	{
    		list = new intNode;
    		list->value = item;
    		list->next = NULL;
    	}
    	else
    	{
    		intNodePtr tempPtr;
    		tempPtr = list;
    
    			while(tempPtr->next != NULL)
    			{
    				tempPtr = tempPtr->next;
    			}
    
    		tempPtr->next = new intNode;
    
    		tempPtr = tempPtr->next;
    		tempPtr->value = item;
    		tempPtr->next = NULL;
    	}
    
    }
    
    bool LList::DeleteItem(int loc)
    {
    	intNodePtr tempPtr1 = list;
    	intNodePtr tempPtr2 = list;
    
    	for(int i = 0; i < loc - 2; i++)//make this loc -1 if need be (dierbach responce)
    	{
    		if (tempPtr2 != NULL)
    		{
    			tempPtr2 = tempPtr2->next;
    		}
    	}
    	if(tempPtr2 == NULL)
    		return false;
    	else
    	{
    		if(loc == 1)
    			list = list->next;
    		else
    		{
    			if (tempPtr1 != NULL)
    			{ 
    				tempPtr1 = tempPtr2->next;
    				tempPtr2->next = (tempPtr1->next);
    			}
    		}
    		
    		 size--;
    		 delete tempPtr1;
    		 return true; 
    	}
    }
    void LList::Print()
    {
    	if(size == 0)
    	{
    		cout<<"List is empty.."<<endl;
    		return;
    	}
    
    	intNodePtr tempPtr;
    
    	tempPtr = list;
    
    	if(size == 1)
    		cout<<tempPtr->value<<endl;
    	else
    	{
    		cout<<tempPtr->value<<endl;
    
    		while(tempPtr->next != NULL)
    		{
    			tempPtr = tempPtr->next;
    			cout<<tempPtr->value<<endl;
    		}
    	}		
    }
    If you need anything else please ask.

    Thanks!!

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Also you may see the:

    Code:
    //SearchableStack.cpp
    
    #include <iostream>
    #include "SearchableStack.h"
    ; //i have no freaking clue...
    using namespace std;
    I was getting an error "missing ; before using" so i put in the ; and it worked - i have no clue why or what for, but it worked......?

    Thanks.

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    You'd probably want to initialize temp in that code and make sure you initialize it to something other than n.
    Of course other than n, haha. It would be so rare that the random number temp is would actually be n, but you're right, good eye. So initialized to 0, that would be a fine version? I just wanted to make sure it wasn't a problem readability-wise, since thats how I'd do it.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I was getting an error "missing ; before using"
    That's because you are missing a semicolon at the end of the SearchableStack declaration.

    Looking at that code, what is current supposed to be inside the Stack class? Is it the current location inside the List? If so, why are you returning it from pop()? Shouldn't pop() be returning the result of GetItem(current) before you decrement it?

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Current is the number in which spot the top of the stack is.

    So if the stack has: 333, 4, 44, 2
    Current = 4

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    If so, why are you returning it from pop()?
    I was returning the next item in the stack..

    Dont think this effects my issue - ?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sure it does... you are returning current. The return value of pop is being pushed onto the Stack in the find function. Try inputting something other than 1234... like maybe 5678, then search for 6. It won't work because pop should return the next item on the stack, but instead is returning the next location in the list.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    So I should change code around in the Stack Class pop() member function? Opposed to messing with the SearchableStack Class add() func.?

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in pass a variable to a class
    By nima_pw in forum C# Programming
    Replies: 3
    Last Post: 06-09-2009, 07:30 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  4. Inheritance problem, help please!
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2002, 07:02 AM
  5. stack make file problem
    By puckett_m in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 11:51 AM