Generic stack problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziamughal
    New Member
    • Jun 2007
    • 9

    #1

    Generic stack problem

    Dear Sir i have made a stack in which i can store the strings the whole code is below , i want to make this stack a generic stack,
    what i need to do, look at the code and give me some suggestions
    the cpp file is attached with this question.
    Attached Files
  • readerMadsen123
    New Member
    • Dec 2009
    • 5

    #2
    You should start along the line of..

    template <class T>
    class stack
    {

    public:
    stack();
    void push(const T&);
    void pop();
    void display();
    const T& topelement();
    bool isEmpty();
    void makeEmpty();

    private:
    struct Node
    {
    T data;
    Node* preptr;
    };
    Node* current,*head,* top;


    };

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This is C++. Use the Standard Library stack<> template.

      Comment

      • readerMadsen123
        New Member
        • Dec 2009
        • 5

        #4
        Yes, but i think the guy wants to roll his own - for educational purposes or something

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          That's precisely why he should use the standard library stack.

          A C++ instructor should not re-implement basic concepts. Instead, he should explain how to use the C++ language to implement the concept.

          Comment

          • readerMadsen123
            New Member
            • Dec 2009
            • 5

            #6
            ziamughal, if you are trying to figure out how generics work in C++, try modifying your stack implementation to be generic.
            If you just need a stack, go with the one from standard template library, like weaknessforcats says.

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              If it's a "C++ class" then yes, teach them standard library and it' ok. But if it's a CS course, the goal of such assignments is not only to teach students to implement stack from scratch, but also weed out those who are unable to do even this.

              Comment

              Working...