Class template for stack in C++

Here is the original requirements:
Write a class template for a linked-list implementation of a stack. The stack class must have the push, pop and isEmpty functions included, but with four additional functions: de- structor, copy constructor, overloaded assignment operator, and deepCopy. The private section shall only have one data member: a pointer called top. Be sure to have the pointer of the last node (at the bottom of the stack) set to NULL at all times. (This condition was- n’t necessary in the linked-list implementation of the queue, because the back pointer signified the last node—but it is generally necessary in linked lists). At the top of the class implementation file, include iostream (important because NULL is defined here) and use the std namespace.

Because my prof always don't talk much in the class, now I'm kind of confused with this project. I've made the push pop and isEmpty, although I'm not sure is it correct. But I've no idea how to make those four additional functions. Please give me some help; Here is the code I've done so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        class Stack{
    
    
    private:
        struct node
        {
            int value;
            node *next;
            
            node(int value, node *next = NULL)
            {
                this->value = value;
                this->next = next;
            }
        };
        node *top;
        
        
    public:
        Stack()
        {
            top = NULL;
        }
        void push(int number)
        {
            top = new node(number, top);
        }
        
        int pop()
        {
            int number = top->value;
            node *temp = top;
            top = top->next;
            delete temp;
            
            return number;
        }
        
        bool isEmpty()
        {
            if(top == NULL)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };
    
    
    int main(int argc, const char * argv[]) {
        Stack s;
        s.push(5);
        s.push(7);
        s.push(8);
        
        return 0;
    }
Last edited on
For destructor see:

http://en.cppreference.com/w/cpp/language/destructor

In the destructor for node -> delete next;
In the destructor for Stack -> delete top;

For copy constructor see:

http://www.cplusplus.com/articles/y8hv0pDG/

Deep copy is not a function. See:

http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/
You might want to try this as a test case for your code.
1
2
3
4
5
int main()
{
    Stack s;
    int val = s.pop();
}


By the way, you were instructed to create a class template for your stack. That would allow you to have a stack of ints, doubles, strings, doohickeys and whatchamacallits without changing your code. You have a stack that contains ints.

Do you need help with template classes, or are you doing initial development with ints and planning to generalize when you have it all working?
Topic archived. No new replies allowed.