Thread: classes partial understanding

  1. #1
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89

    classes partial understanding

    hi

    i just read the tutorial on classes on this webpage.. but its unclear to me what the purpose of a constructor and a deconstructor is..could someone elaborate on it please?

    thanx in advance,

    Boomba,

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, the "purpose" is for initialization and de-initialization.

    For instance, you may want to initialize member variables or allocate memory off the heap in the constructor. The destructor would perform any neccessary cleanup like releasing allocated resources back to the OS.

    The absolute best way to understand them is to play with them. Get comfortable in your understanding of when and whey they are called. Here's a little some'n some'n to show what I mean.
    Code:
    #include <iostream>
    using namespace std;
    
    class C
    {
        int m_n;
    
    public:
        C()
        {
            m_n = 0;
            cout << "C default constructor" << endl;
        }//constructor
    
        C(int n) : m_n(n)
        {
            cout << "C constructor, m_n = " << m_n << endl;
        }//constructor
    
        ~C()
        {
            cout << "C destructor, m_n = " << m_n << endl;
        }//destructor
    };//C
    
    int main()
    {
        cout << "Creating c1..." << endl;
        C c1;
    
        cout << "Creating c2(42)" << endl;
        C c2(42);
    
        cout << endl << "returning from main()" << endl << endl;
    
        return 0;
    }//main
    gg

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I just want to add something.
    Get comfortable in your understanding of when and whey they are called.
    You should know that they are called implicitly and ofcourse they can be called explicitly.
    none...

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You should know that they are called implicitly
    Unless of course you tend to the old C ways, and allocate a raw memory block to a pointer:
    Code:
    class C
    {
    public:
        C() {cout << "C constructor called" << endl; }
    };
    
    int main()
    {
        C *c=(C *)malloc(sizeof(C));
        return 0;
    }
    The constructor won't be called here, and neither would a destructor. Of course, that code is horrible and no-one would ever use it. Just putting in my two cents.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  2. i need help understanding classes
    By jmprich in forum C++ Programming
    Replies: 1
    Last Post: 06-10-2003, 07:10 PM
  3. I'm having a really hard time understanding classes.
    By imortal in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2003, 01:02 PM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. Help Understanding Classes
    By Chroder in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2002, 07:53 PM