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!
This is a discussion on a little help! within the C++ Programming forums, part of the General Programming Boards category; can someone out there explain what is the object,constructors and destrustors? if can,write a simple program that contain three thing ...
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!
Example object myClass.
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: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 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.Code:myClass ClassOne; //constructor is called here.
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).