Inheritance using Stack Class Problem

This is a discussion on Inheritance using Stack Class Problem within the C++ Programming forums, part of the General Programming Boards category; Crap.. Do you know of any way to accomplish this? I thought this would work: Code: int Stack::pop() { if(current ...

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Crap.. Do you know of any way to accomplish this?

    I thought this would work:

    Code:
    int Stack::pop()
    {
    	if(current == 0)
    		return 0;
    	else
    	{
    		list.DeleteItem(current);
    		current--;
    		return list.GetItem(current);
    	}
    	
    }
    But then it will still return a '-1' in the last spot?

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Daved, you around?

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    It's a pretty simple solution. Think about what you're trying to do. The pop() method should remove the last item from the stack and return it while remembering that the next item below it is now the last item.

    Your code removes the last item from the stack, updates current to refer to the new last location, then returns the item at that new location. All you've got to do is figure out how to return the item at the original "current" location instead of what it becomes after the delete and decrement.

    Or you can look at your other thread, although 7stud's version is even a little more complicated than it needs to be.

Page 2 of 2 FirstFirst 12
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, 04:01 PM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01: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, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21