Like Tree3Likes
  • 2 Post By antred
  • 1 Post By Elkvis

Constructor and deconstructor

This is a discussion on Constructor and deconstructor within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class Walnut { public: int Size; }; class Squirrel { private: Walnut *MyDinner; public: ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Constructor and deconstructor

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Walnut
    {
    public:
        int Size;
    };
    
    class Squirrel
    {
    private:
        Walnut *MyDinner;
    
    public:
        Squirrel();
        ~Squirrel();
    };
    
    Squirrel::Squirrel()
    {
        cout << "Starting!" << endl;
        MyDinner = new Walnut;
        MyDinner->Size = 30;
    }
    
    Squirrel::~Squirrel()
    {
        cout << "Cleaning up my mess!" << endl;
        delete MyDinner;
    }
    
    int main()
    {
        Squirrel *Sam = new Squirrel;
        Squirrel *Sally = new Squirrel;
    
        delete Sam;
        delete Sally;
    
        return 0;
    }
    I dont understand much about this code. Why the output is Starting! Starting Cleaning up my mess! Cleaning up my mess! ?????
    Why it does not (Starting! Cleaning up my mess! Starting! Cleaning up my mess!) Because

    Squirrel();
    ~Squirrel();

    It's executed one by one not together?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    The default constructor for Squirrel is invoked on this line:
    Code:
    Squirrel *Sam = new Squirrel;
    Then again on this line:
    Code:
    Squirrel *Sally = new Squirrel;
    The destructor for Squirrel is invoked on this line:
    Code:
    delete Sam;
    Then on this line:
    Code:
    delete Sally;
    Hence the output since it is as if you are calling two functions named C and D:
    Code:
    C();
    C();
    D();
    D();
    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

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    it's generating the output you are seeing because you are creating both objects first, then deleting both objects. the constructor gets called when you say new squirrel, and the destructor gets called when you say delete. since you are doing two news, followed by two deletes, you're getting exactly the output that you should.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    ya i know. But i just cant get it into my head.
    • I create sam object with squirrel class
    • inside there are Squirrel and ~Squirrel <-- execute follow sequence
    • Create sally object with squirrel class
    • same as 2nd


    U know what I mean. I mean like if it's executed according to sequence, it's quite impossible. srry, i try to turn around the theory.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Okay, here's a test. Consider this program:
    Code:
    #include <iostream>
    
    void bar()
    {
        std::cout << "World!" << std::endl;
    }
    
    void foo()
    {
        std::cout << "Hello!" << std::endl;
    }
    
    int main()
    {
        foo();
        bar();
    }
    Now, I tell you that the output of this program will be:
    Code:
    World!
    Hello!
    because bar is defined before foo. Do you agree? Why or why not?
    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
    Aug 2011
    Posts
    102
    Isn't the output is
    Hello!
    World!

    Because in main() we execute foo() first then later bar(). Am I right?.......

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by ncode
    Because in main() we execute foo() first then later bar(). Am I right?
    Exactly!

    So, why do you say that:
    Quote Originally Posted by ncode
    inside there are Squirrel and ~Squirrel <-- execute follow sequence
    when obviously in main() we execute the constructor first, twice, then later execute the destructor, twice?
    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
    Aug 2011
    Posts
    102
    oh wait. How do the Squirrel() been executed when I didnt perform sam->Squirrel() or sam->~Squirrel(). i will continue 2moro as it get night now.thanks.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by ncode
    How do the Squirrel() been executed when I didnt perform sam->Squirrel() or sam->~Squirrel().
    Refer to my post #2. I explain just what causes the default constructor and destructor to be executed.
    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 2011
    Posts
    96
    Can you pls explain
    Why has he should define the variable "int size" in private: of the class instead of public:
    Have you defined that variable in public: intentionally so that it could be used by the whole program?
    Do you want to use that variable as "global variable"?



    Thanks
    Last edited by student111; 06-11-2012 at 10:18 AM. Reason: asking question

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by ncode View Post
    Code:
        Squirrel *Sam = new Squirrel;
        Squirrel *Sally = new Squirrel;
    
        delete Sam;
        delete Sally;
    I'm going to send PETA on your ass!

    Really just letting you know that, technically speaking, it's called destructor, not deconstructor.
    R41D3N and laserlight like this.

  12. #12
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    To make it simple, A Constructor [Squirrel()] is called when an object to the class is created.

    For example,
    Code:
     Squirrel *Sam = newSquirrel;
     Squirrel *Sally = newSquirrel;

    The destructor [~Squirrel()] is only called when that object is destroyed, which you did in your code by using delete. Till then destructor is just sitting there waiting for your Squirrel to die/delete :P

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    Quote Originally Posted by antred View Post
    it's called destructor, not deconstructor.
    perhaps he's a devin townsend fan
    R41D3N likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a deconstructor for a linked list
    By sigur47 in forum C++ Programming
    Replies: 15
    Last Post: 01-27-2012, 02:48 PM
  2. plz help with deleting class deconstructor.
    By MegaManZZ in forum C++ Programming
    Replies: 6
    Last Post: 08-31-2008, 04:49 PM
  3. Must I always use a deconstructor?
    By markcls in forum C++ Programming
    Replies: 22
    Last Post: 03-25-2007, 04:57 PM
  4. is Recursion ok in a deconstructor?
    By Syneris in forum C++ Programming
    Replies: 14
    Last Post: 01-03-2006, 11:58 PM
  5. Deconstructor throwing exception
    By subdene in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2004, 04:52 AM

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