Do I need to create a destructor for my stack class? or does the compiler use one

This is a discussion on Do I need to create a destructor for my stack class? or does the compiler use one within the C++ Programming forums, part of the General Programming Boards category; Well Im creating a stack class using c++ and I'm not sure if I should create a destructor for my ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    Do I need to create a destructor for my stack class? or does the compiler use one

    Well Im creating a stack class using c++ and I'm not sure if I should create a destructor for my class. Is a constructor really needed even though Im using delete in my of my functions? Well here is my code, thanks for looking?

    Code:
    #include "stack.h"  // Stack class specification file
    
    Stack::Stack() { top = NULL; }  // default constructor
    
    bool Stack::isEmpty() {
      return top == NULL;
    }  // end isEmpty
    
    void Stack::push(StackItemType itemIn) {
      StackItems *newItem = new StackItems();  // create new node
      newItem->item = itemIn; // load values
      newItem->below = top;
      top = newItem; // make top point to this new item
    }  // end push
    
    bool Stack::pop() {
      if (isEmpty()) return false;
    
      // temporarily save current top pointer location so it can
      //   be later deleted (dynamic memory freed)
      StackItems *tempTop = top;
    
      top = top->below; // move top to one below it
      delete tempTop; // free space allocated to old top
      return true;
    }  // end pop
    
    bool Stack::pop(StackItemType& stackTop) {
     if (isEmpty()) return false;
    
      // stack is not empty; retrieve top
      stackTop = top->item;
      pop();
      return true;
    }  // end pop
    
    bool Stack::getTop(StackItemType& stackTop) {
      if (isEmpty()) return false;
    
      // stack is not empty; retrieve top
      stackTop = top->item;
      return true;
    }  // end getTop
    
    void Stack::displayAll() {
      if (isEmpty()) cout << "Stack is empty." << endl;
      StackItems *temp = top;
      while (temp != NULL) {
        cout << temp->item << endl;
        temp = temp->below;
        }
    } // end displayAll
    
    int Stack::getLength() {
      StackItems *temp = top;
      int count = 0;
      while (temp != NULL) {
        count++;
        temp = temp->below;
        }
      return count;
    } // end getLength
    Last edited by newbc; 07-04-2011 at 09:49 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    also what is a good way of retreaving the item from the bottom? I just can't think of any way. The bottom item in the stack will have a null pointer to the one below it, but I just can't seem to properly put it in code.

    Code:
    bool Stack::getbottom(StackItemType& stackb) {
      if (isEmpty()) return false;
    
      // stack is not empty; retrieve bottom
      top->below=NULL;
     
      return true;
    }
    Last edited by newbc; 07-04-2011 at 07:47 PM.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by newbc View Post
    Well Im creating a stack class using c++ and I'm not sure if I should create a constructor for my class. Is a constructor really needed even though Im using delete in my of my functions? Well here is my code, thanks for looking?
    Define need. Should I be able to use isEmpty on a newly constructed stack? If so, then yes, you do. In fact, should I use isEmpty at all? Stack::pop needs to NULL the pointers it deletes.

    Quote Originally Posted by newbc View Post
    also what is a good way of retreaving the item from the bottom? I just can't think of any way. The bottom item in the stack will have a null pointer to the one below it, but I just can't seem to properly put it in code.
    Use a doubly linked list to represent the stack internally and maintain a pointer to the tail.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    whiteflags

    Do you mean to have a back and front pointer like queues? if so my teacher doesn't like that he wants us to do it like this, by just having a top pointer.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by newbc
    also what is a good way of retreaving the item from the bottom?
    When the stack has exactly one item, just access the top item.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by laserlight View Post
    When the stack has exactly one item, just access the top item.
    LOL, that be great.

    well here is my second code. I modify my second display function since it displays all the values until reaching null. what i want it to do is just print the number that is the bottom number before reaching null. So far I have done a horrible job.

    Code:
    bool Stack::getb() {
    
     if (isEmpty()) cout << "Stack is empty." << endl;
      StackItems *temp = top;
    
      while (temp != NULL) {
    
           if(top->below=NULL){
                  cout << temp->item << endl;
                 //temp = temp->below;
           }//end if
    
        temp = temp->below;
       }
    } // end

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by newbc
    LOL, that be great.
    I'm not joking.

    Quote Originally Posted by newbc
    I modify my second display function since it displays all the values until reaching null. what i want it to do is just print the number that is the bottom number before reaching null.
    Don't do this. If the user wants to print the bottom number, he/she should pop all the ones above it first. This is the notion of a stack.

    If you want to access the bottom item like this, don't create a stack. Create a linked list where you track the tail.

    Oh, and for the code you posted, beware the difference between = and ==.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    ok I think I figured out how to print item on the bottom;

    Any help on how to create the descructor?
    we just converted to c++ like two weeks ago.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by newbc
    ok I think I figured out how to print item on the bottom;
    What did you do?

    To check: if it a requirement that your class provide a member function to access/print the bottom item on the stack, as a special case?

    Quote Originally Posted by newbc
    Any help on how to create the descructor?
    Loop over the items and destroy them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    What do you mean by "Loop over the items and destroy them."

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Well one way would be to do
    Code:
    while (pop());
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  2. Destructor causes compiler error with hard exit?
    By Xanderbeard in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2010, 01:29 PM
  3. Replies: 10
    Last Post: 03-07-2010, 11:20 PM
  4. Stack corruption by class destructor
    By qxcdfg in forum C++ Programming
    Replies: 11
    Last Post: 03-10-2008, 03:03 PM
  5. destructor(): in and out of class differences?
    By xion in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2005, 11:01 PM

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