Where did my destructor go??

Discussion in 'C++' started by psppb, Jan 15, 2008.

  1. psppb

    psppb New Member

    Joined:
    Dec 26, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Here is another small problem possibly brought about by the ancient book I am using to learn or something I did. I am trying to get my destructor to print a few lines but everytime I run the program he destructor simply does not do what its told and display the few lines.

    here is the code:
    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    
    class Stringthings {
          private: 
                   char str[20];
                   char firstletter;
                   char lastletter;
          public:
                 void first(void);
                 void show(void);
                 void last(void);
                 Stringthings(char s[20]);
                 ~Stringthings(void);
                 };
    
    //Functions of the class
    void Stringthings::first(void)
    {
         if(strlen(str)<1)
         firstletter = '?';
         else 
         firstletter = str[0];
    }
    
    void Stringthings::show(void)
    {
         cout << firstletter << endl;
    }
    
    void Stringthings::last(void)
    {
         if(strlen(str) < 1)
         lastletter = '?';
         else
         lastletter = str[strlen(str) - 1];
    }
    //This constructor creates a stringthing object and copies the input string into the private data member str
    // strcpy is in the library string.h
    Stringthings::Stringthings(char s[20])
    {
         strcpy(str, s);
    }
    
    Stringthings::~Stringthings(void)
    {
         cout << endl << "The full string was " << str;
         cout << endl << "That's all folks!" << endl << endl;
    }
    
    int main(void)
    {
        char data[20];
        
        cout << "Enter the string: ";
        cin.getline(data, 20);
        Stringthings test(data);
        test.first();
        test.show();
                    
    cin.ignore(256, '\n');
              cout << "Press ENTER to continue..." << endl;
              cin.get();
              return 0;
              }
    
    no errors pop up the machine simply runs the default destructor and not the one I created

    Thanks much

    Bill
     
  2. psppb

    psppb New Member

    Joined:
    Dec 26, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Sorry I couldn't for a moment get the code in the proper format... :-(
     
  3. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    3
    Trophy Points:
    0
  4. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Dunno what's wrong on your system, it worked fine for me:
    ---START
    Enter the string: hello
    h

    Press ENTER to continue...


    The full string was hello
    That's all folks!
    ---STOP

    I had to press Enter to continue before it said "press enter to continue" (although not if I entered a very long string).
    What string did you enter?
    What output did you get?
    Did the program end or did it appear to hang?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice