Thread: OOP Problem?

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    OOP Problem?

    One day, I wrote a simple Stack class:

    Code:
    class LinkedList {
    
    public:
      int value;
      LinkedList *next;
    
      LinkedList() : next(0) { }
    };
    
    class Stack
    {
    protected:
      LinkedList *top;
    
    public:
      Stack() : top(0) { }
      
      void push( int value );
      int pop();
    };
    I compiled it and then use it in some project...


    3 years later, I need a stack that counts every item that pushed or popped into it
    I lost the Stack source code and the only thing left is its header and compiled binary
    due to OOP matter, I just extend it then.

    Code:
    class CountingStack : public Stack
    {
    protected:
      int count;
    
    public:
      CountingStack() : count(0) { }
    
      void push( int value ) {
        //count++
      }
      
      int pop() {
        //count--
      }
      
      int getCount();
    };
    the next year , I need a stack that throws an Exception when it's empty
    but it doesn't need to count every item that pushed or popped into it
    So I wrote this ExceptionStack

    Code:
    class ExceptionStack : public Stack
    {
    public:
      int pop() {
        //throw Exception if the stack is empty ( top == NULL )
      }
    };
    And now, I need both CountingStack and ExceptionStack features
    but again, I lost their source code!
    Because of the project's time limit, I couln't rewrite the Stack code.
    So, I guess I need to write this kind of class, but I found a problem...

    Code:
    class CountingExceptionStack : public CountingStack, public ExceptionStack
    {
    public:
      void push( int value )
      {
        CountingStack::push( value );
      }
      
      /*
      int pop()
      {
        What should I write here??
      }
      */
    };
    Just GET it OFF out my mind!!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Take std::stack and write your own stack class around it in 5 minutes?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by audinue
    I lost the Stack source code and the only thing left is its header and compiled binary
    due to OOP matter, I just extend it then.
    However, Stack was not designed to be a base class. It has no virtual functions for you to override.

    There are two ways to implement CountingStack that I would consider correct. One way is to provide the same interface as Stack, but have a Stack member variable instead of using inheritance, then forward the various member functions (i.e., CountingStack's member functions would just call the corresponding member functions of the Stack member variable). Another way is to use private inheritance, then use using declarations to make the various member functions public, except for push(), which you implement.

    For ExceptionStack, there is an even simpler solution, assuming that Stack has an empty() member function that returns true if the stack is empty: define a non-member function that performs the pop() and which throws an exception if the stack is empty. The same solution can be applied for CountingStack. In fact, if you define it as a function template instead, then it will work for any stack class that defines pop() and empty().

    Of course, the problem is that Stack does not have a member function that returns true if the stack is empty, and indeed it has no way to determine if the stack is empty. This makes Stack quite useless, but in a real class I would expect that the interface would be sufficient (and class interfaces typically provide too much as member functions, rather than too little, which is not necessarily a good thing).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think it's safe to say that the stack thing is a metaphor for some considerably more complex class that you cannot reimplement in 5 minutes using standard library facilities.

    The answer is, you're screwed. Unless your headers give you a clue that the entire work the two pops did was in a separate helper function that you could call directly, there's no way to get the functionality of both.

    If the helper functions are private, you can [i]probably[i] change the visibility specifier in the existing classes without breaking binary compatibility.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  5. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM