Thread: a little help!

  1. #1
    Jasmine
    Guest

    a little help!

    can someone out there explain what is the object,constructors and destrustors? if can,write a simple program that contain three thing that i mentioned up there.i just wanna to know what is it and the functinality of it,thanks!

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Example object myClass.
    Code:
    class myClass
    {
    private:
       int intInt;
       long longLong;
    public:
       myClass();                 //this is the constructor
      ~myClass();               //this is the destructor
    }
    
    myClass::myClass()     //constructor
    {
       intInt=0;
       longLong=0;
    }
    
    myClass::~myClass()  //destructor
    {
    }
    The constructor is a member function of the class that has the same name of the class and can't return a value. It's called when the object is created. ie:
    Code:
    myClass ClassOne;  //constructor is called here.
    The destructor is the opposite of the constructor and has the same name as the constructor except that it starts with a ~. It's called when the object is destroyed.

    Constructors usually set the initial values of the member variables (like setting a pointer to NULL) and destructors do the clean-up (like deleting the pointer and resetting it to NULL).

Popular pages Recent additions subscribe to a feed